Class: PacketIO::Test::MockServer

Inherits:
Object
  • Object
show all
Defined in:
lib/packet_io/test/mock_server.rb

Overview

a threaded reader that simulates a remote server to help testing io_listener

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read, write) ⇒ MockServer

Returns a new instance of MockServer.



17
18
19
20
21
22
23
24
# File 'lib/packet_io/test/mock_server.rb', line 17

def initialize(read, write)
  @read, @write = read, write
  @write_queue = Queue.new

  @writer = Thread.new do
    parse_commands
  end
end

Class Method Details

.buildObject

create a new server and wire it up with a bidirectional pipe



9
10
11
12
13
14
15
# File 'lib/packet_io/test/mock_server.rb', line 9

def self.build
  client_read, server_write = IO.pipe # Server -> Client
  server_read, client_write = IO.pipe # Client -> Server

  @device = new(server_read, server_write)
  return @device, client_read, client_write
end

Instance Method Details

#eofObject



40
41
42
# File 'lib/packet_io/test/mock_server.rb', line 40

def eof
  @write_queue.push [:close]
end

#read_allObject



31
32
33
# File 'lib/packet_io/test/mock_server.rb', line 31

def read_all
  @read.readpartial(4096)
end

#wait(seconds = 0.02) ⇒ Object



35
36
37
38
# File 'lib/packet_io/test/mock_server.rb', line 35

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

#write(string) ⇒ Object



26
27
28
29
# File 'lib/packet_io/test/mock_server.rb', line 26

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