forked from eventmachine/eventmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_io_streamer.rb
48 lines (39 loc) · 1018 Bytes
/
test_io_streamer.rb
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
48
# frozen_string_literal: true
require_relative 'em_test_helper'
require 'em/io_streamer'
require 'stringio'
# 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 '127.0.0.1', port, StreamServer, sent
EM.connect '127.0.0.1', port, StreamClient, received
end
assert_equal sent, received
end
end