Sha256: 6052791f7d0256cc3bd4936424ae2a624bec35e4c1aa2469220af50712a82ab2

Contents?: true

Size: 786 Bytes

Versions: 1

Compression:

Stored size: 786 Bytes

Contents

module PacketIO::Test
  class MockServer
    def initialize(read, write)
      @read, @write = read, write
      @queue = Queue.new
      @thread = Thread.new do
        parse_commands
      end
    end

    def write(string)
      @queue.push [:write, string]
      self
    end

    def read_all
      @read.readpartial(4096)
    end

    def wait(seconds = 0.02)
      @queue.push [:wait, seconds]
      self
    end

    def eof
      @queue.push [:close]
    end


    private

    def parse_commands
      loop do
        action, data = @queue.pop

        case action
        when :close
          @write.close
          break
        when :write
          @write.write(data)
          @write.flush
        when :wait
          sleep data
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
packet_io-0.4.0.rc2 lib/packet_io/test/mock_server.rb