Class: PacketIO::Test::MockServer
- Inherits:
-
Object
- Object
- PacketIO::Test::MockServer
- 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
-
.build ⇒ Object
create a new server and wire it up with a bidirectional pipe.
Instance Method Summary collapse
- #eof ⇒ Object
-
#initialize(read, write) ⇒ MockServer
constructor
A new instance of MockServer.
- #read_all ⇒ Object
- #wait(seconds = 0.02) ⇒ Object
- #write(string) ⇒ Object
Constructor Details
#initialize(read, write) ⇒ MockServer
Returns a new instance of MockServer.
19 20 21 22 23 24 25 26 |
# File 'lib/packet_io/test/mock_server.rb', line 19 def initialize(read, write) @read, @write = read, write @write_queue = Queue.new @writer = Thread.new do parse_commands end end |
Class Method Details
.build ⇒ Object
create a new server and wire it up with a bidirectional pipe
11 12 13 14 15 16 17 |
# File 'lib/packet_io/test/mock_server.rb', line 11 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
#eof ⇒ Object
42 43 44 |
# File 'lib/packet_io/test/mock_server.rb', line 42 def eof @write_queue.push [:close] end |
#read_all ⇒ Object
33 34 35 |
# File 'lib/packet_io/test/mock_server.rb', line 33 def read_all @read.readpartial(4096) end |
#wait(seconds = 0.02) ⇒ Object
37 38 39 40 |
# File 'lib/packet_io/test/mock_server.rb', line 37 def wait(seconds = 0.02) @write_queue.push [:wait, seconds] self end |
#write(string) ⇒ Object
28 29 30 31 |
# File 'lib/packet_io/test/mock_server.rb', line 28 def write(string) @write_queue.push [:write, string] self end |