1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
# frozen_string_literal: true
require_relative 'em_test_helper'
require 'em/io_streamer'
# below to stop 'already initialized constant' warning
EM::IOStreamer.__send__ :remove_const, :CHUNK_SIZE
EM::IOStreamer.const_set :CHUNK_SIZE, 2
class TestIOStreamer < Test::Unit::TestCase
class StreamServer < EM::Connection
def initialize(sent)
@sent = sent
end
def post_init
io = StringIO.new @sent
EM::IOStreamer.new(self, io).callback { close_connection_after_writing }
end
end
class StreamClient < EM::Connection
def initialize(received)
@received = received
end
def receive_data data
@received << data
end
def unbind
EM.stop
end
end
def test_io_stream
sent = 'this is a test'
received = ''.dup
EM.run do
port = next_port
EM.start_server 'localhost', port, StreamServer, sent
EM.connect 'localhost', port, StreamClient, received
end
assert_equal sent, received
end
end
|