sensu-em 2.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +12 -0
  4. data/.yardopts +7 -0
  5. data/CHANGELOG.md +33 -0
  6. data/GNU +281 -0
  7. data/Gemfile +2 -0
  8. data/LICENSE +60 -0
  9. data/README.md +109 -0
  10. data/Rakefile +20 -0
  11. data/docs/DocumentationGuidesIndex.md +27 -0
  12. data/docs/GettingStarted.md +521 -0
  13. data/docs/old/ChangeLog +211 -0
  14. data/docs/old/DEFERRABLES +246 -0
  15. data/docs/old/EPOLL +141 -0
  16. data/docs/old/INSTALL +13 -0
  17. data/docs/old/KEYBOARD +42 -0
  18. data/docs/old/LEGAL +25 -0
  19. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  20. data/docs/old/PURE_RUBY +75 -0
  21. data/docs/old/RELEASE_NOTES +94 -0
  22. data/docs/old/SMTP +4 -0
  23. data/docs/old/SPAWNED_PROCESSES +148 -0
  24. data/docs/old/TODO +8 -0
  25. data/eventmachine.gemspec +38 -0
  26. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  27. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  28. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  29. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  30. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  31. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  32. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  33. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  34. data/examples/old/ex_channel.rb +43 -0
  35. data/examples/old/ex_queue.rb +2 -0
  36. data/examples/old/ex_tick_loop_array.rb +15 -0
  37. data/examples/old/ex_tick_loop_counter.rb +32 -0
  38. data/examples/old/helper.rb +2 -0
  39. data/ext/binder.cpp +124 -0
  40. data/ext/binder.h +46 -0
  41. data/ext/cmain.cpp +887 -0
  42. data/ext/ed.cpp +1988 -0
  43. data/ext/ed.h +422 -0
  44. data/ext/em.cpp +2352 -0
  45. data/ext/em.h +253 -0
  46. data/ext/eventmachine.h +128 -0
  47. data/ext/extconf.rb +179 -0
  48. data/ext/fastfilereader/extconf.rb +103 -0
  49. data/ext/fastfilereader/mapper.cpp +214 -0
  50. data/ext/fastfilereader/mapper.h +59 -0
  51. data/ext/fastfilereader/rubymain.cpp +127 -0
  52. data/ext/kb.cpp +79 -0
  53. data/ext/page.cpp +107 -0
  54. data/ext/page.h +51 -0
  55. data/ext/pipe.cpp +347 -0
  56. data/ext/project.h +161 -0
  57. data/ext/rubymain.cpp +1318 -0
  58. data/ext/ssl.cpp +468 -0
  59. data/ext/ssl.h +94 -0
  60. data/java/.classpath +6 -0
  61. data/java/.gitignore +1 -0
  62. data/java/.project +17 -0
  63. data/java/src/com/rubyeventmachine/DatagramPacket.java +13 -0
  64. data/java/src/com/rubyeventmachine/EmReactor.java +529 -0
  65. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  66. data/java/src/com/rubyeventmachine/EventCallback.java +7 -0
  67. data/java/src/com/rubyeventmachine/EventCode.java +26 -0
  68. data/java/src/com/rubyeventmachine/EventableChannel.java +130 -0
  69. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +180 -0
  70. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +405 -0
  71. data/java/src/com/rubyeventmachine/SslBox.java +310 -0
  72. data/lib/em/buftok.rb +110 -0
  73. data/lib/em/callback.rb +58 -0
  74. data/lib/em/channel.rb +64 -0
  75. data/lib/em/completion.rb +304 -0
  76. data/lib/em/connection.rb +712 -0
  77. data/lib/em/deferrable/pool.rb +2 -0
  78. data/lib/em/deferrable.rb +210 -0
  79. data/lib/em/file_watch.rb +73 -0
  80. data/lib/em/future.rb +61 -0
  81. data/lib/em/iterator.rb +231 -0
  82. data/lib/em/messages.rb +66 -0
  83. data/lib/em/pool.rb +151 -0
  84. data/lib/em/process_watch.rb +45 -0
  85. data/lib/em/processes.rb +123 -0
  86. data/lib/em/protocols/header_and_content.rb +138 -0
  87. data/lib/em/protocols/httpclient.rb +279 -0
  88. data/lib/em/protocols/httpclient2.rb +600 -0
  89. data/lib/em/protocols/line_and_text.rb +125 -0
  90. data/lib/em/protocols/line_protocol.rb +29 -0
  91. data/lib/em/protocols/linetext2.rb +161 -0
  92. data/lib/em/protocols/memcache.rb +331 -0
  93. data/lib/em/protocols/object_protocol.rb +46 -0
  94. data/lib/em/protocols/postgres3.rb +246 -0
  95. data/lib/em/protocols/saslauth.rb +175 -0
  96. data/lib/em/protocols/smtpclient.rb +365 -0
  97. data/lib/em/protocols/smtpserver.rb +643 -0
  98. data/lib/em/protocols/socks4.rb +66 -0
  99. data/lib/em/protocols/stomp.rb +205 -0
  100. data/lib/em/protocols/tcptest.rb +54 -0
  101. data/lib/em/protocols.rb +37 -0
  102. data/lib/em/pure_ruby.rb +1017 -0
  103. data/lib/em/queue.rb +71 -0
  104. data/lib/em/resolver.rb +209 -0
  105. data/lib/em/spawnable.rb +84 -0
  106. data/lib/em/streamer.rb +118 -0
  107. data/lib/em/threaded_resource.rb +90 -0
  108. data/lib/em/tick_loop.rb +85 -0
  109. data/lib/em/timers.rb +61 -0
  110. data/lib/em/version.rb +3 -0
  111. data/lib/eventmachine.rb +1553 -0
  112. data/lib/jeventmachine.rb +321 -0
  113. data/lib/rubyeventmachine.jar +0 -0
  114. data/rakelib/cpp.rake_example +77 -0
  115. data/rakelib/package.rake +98 -0
  116. data/rakelib/test.rake +8 -0
  117. data/tests/client.crt +31 -0
  118. data/tests/client.key +51 -0
  119. data/tests/em_test_helper.rb +64 -0
  120. data/tests/server.crt +36 -0
  121. data/tests/server.key +51 -0
  122. data/tests/test_attach.rb +150 -0
  123. data/tests/test_basic.rb +294 -0
  124. data/tests/test_channel.rb +62 -0
  125. data/tests/test_completion.rb +177 -0
  126. data/tests/test_connection_count.rb +53 -0
  127. data/tests/test_defer.rb +18 -0
  128. data/tests/test_deferrable.rb +35 -0
  129. data/tests/test_epoll.rb +145 -0
  130. data/tests/test_error_handler.rb +38 -0
  131. data/tests/test_exc.rb +28 -0
  132. data/tests/test_file_watch.rb +65 -0
  133. data/tests/test_futures.rb +170 -0
  134. data/tests/test_get_sock_opt.rb +37 -0
  135. data/tests/test_handler_check.rb +35 -0
  136. data/tests/test_hc.rb +155 -0
  137. data/tests/test_httpclient.rb +190 -0
  138. data/tests/test_httpclient2.rb +133 -0
  139. data/tests/test_idle_connection.rb +25 -0
  140. data/tests/test_inactivity_timeout.rb +54 -0
  141. data/tests/test_iterator.rb +97 -0
  142. data/tests/test_kb.rb +34 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +138 -0
  145. data/tests/test_ltp2.rb +288 -0
  146. data/tests/test_next_tick.rb +104 -0
  147. data/tests/test_object_protocol.rb +36 -0
  148. data/tests/test_pause.rb +102 -0
  149. data/tests/test_pending_connect_timeout.rb +52 -0
  150. data/tests/test_pool.rb +194 -0
  151. data/tests/test_process_watch.rb +48 -0
  152. data/tests/test_processes.rb +128 -0
  153. data/tests/test_proxy_connection.rb +180 -0
  154. data/tests/test_pure.rb +88 -0
  155. data/tests/test_queue.rb +50 -0
  156. data/tests/test_resolver.rb +55 -0
  157. data/tests/test_running.rb +14 -0
  158. data/tests/test_sasl.rb +47 -0
  159. data/tests/test_send_file.rb +217 -0
  160. data/tests/test_servers.rb +33 -0
  161. data/tests/test_set_sock_opt.rb +37 -0
  162. data/tests/test_shutdown_hooks.rb +23 -0
  163. data/tests/test_smtpclient.rb +55 -0
  164. data/tests/test_smtpserver.rb +57 -0
  165. data/tests/test_spawn.rb +293 -0
  166. data/tests/test_ssl_args.rb +78 -0
  167. data/tests/test_ssl_echo_data.rb +60 -0
  168. data/tests/test_ssl_methods.rb +56 -0
  169. data/tests/test_ssl_verify.rb +82 -0
  170. data/tests/test_stomp.rb +37 -0
  171. data/tests/test_system.rb +42 -0
  172. data/tests/test_threaded_resource.rb +53 -0
  173. data/tests/test_tick_loop.rb +59 -0
  174. data/tests/test_timers.rb +123 -0
  175. data/tests/test_ud.rb +8 -0
  176. data/tests/test_unbind_reason.rb +48 -0
  177. metadata +297 -0
@@ -0,0 +1,25 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestIdleConnection < Test::Unit::TestCase
4
+ if EM.respond_to?(:get_idle_time)
5
+ def test_idle_time
6
+ EM.run{
7
+ conn = EM.connect 'www.google.com', 80
8
+ EM.add_timer(3){
9
+ $idle_time = conn.get_idle_time
10
+ conn.send_data "GET / HTTP/1.0\r\n\r\n"
11
+ EM.next_tick{
12
+ EM.next_tick{
13
+ $idle_time_after_send = conn.get_idle_time
14
+ conn.close_connection
15
+ EM.stop
16
+ }
17
+ }
18
+ }
19
+ }
20
+
21
+ assert_in_delta 3, $idle_time, 0.2
22
+ assert_in_delta 0, $idle_time_after_send, 0.1
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestInactivityTimeout < Test::Unit::TestCase
4
+
5
+ if EM.respond_to? :get_comm_inactivity_timeout
6
+ def test_default
7
+ EM.run {
8
+ c = EM.connect("127.0.0.1", 54321)
9
+ assert_equal 0.0, c.comm_inactivity_timeout
10
+ EM.stop
11
+ }
12
+ end
13
+
14
+ def test_set_and_get
15
+ EM.run {
16
+ c = EM.connect("127.0.0.1", 54321)
17
+ c.comm_inactivity_timeout = 2.5
18
+ assert_equal 2.5, c.comm_inactivity_timeout
19
+ EM.stop
20
+ }
21
+ end
22
+
23
+ def test_for_real
24
+ start, finish = nil
25
+
26
+ timeout_handler = Module.new do
27
+ define_method :unbind do
28
+ finish = Time.now
29
+ EM.stop
30
+ end
31
+ end
32
+
33
+ EM.run {
34
+ setup_timeout
35
+ EM.heartbeat_interval = 0.01
36
+ EM.start_server("127.0.0.1", 12345)
37
+ EM.add_timer(0.01) {
38
+ start = Time.now
39
+ c = EM.connect("127.0.0.1", 12345, timeout_handler)
40
+ c.comm_inactivity_timeout = 0.02
41
+ }
42
+ }
43
+
44
+ assert_in_delta(0.02, (finish - start), 0.02)
45
+ end
46
+ else
47
+ warn "EM.comm_inactivity_timeout not implemented, skipping tests in #{__FILE__}"
48
+
49
+ # Because some rubies will complain if a TestCase class has no tests
50
+ def test_em_comm_inactivity_timeout_not_implemented
51
+ assert true
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,97 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestIterator < Test::Unit::TestCase
4
+
5
+ def get_time
6
+ EM.current_time.strftime('%H:%M:%S')
7
+ end
8
+
9
+ def test_default_concurrency
10
+ items = {}
11
+ list = 1..10
12
+ EM.run {
13
+ EM::Iterator.new(list).each( proc {|num,iter|
14
+ time = get_time
15
+ items[time] ||= []
16
+ items[time] << num
17
+ EM::Timer.new(1) {iter.next}
18
+ }, proc {EM.stop})
19
+ }
20
+ assert_equal(10, items.keys.size)
21
+ assert_equal((list).to_a, items.values.flatten)
22
+ end
23
+
24
+ def test_concurrency_bigger_than_list_size
25
+ items = {}
26
+ list = [1,2,3]
27
+ EM.run {
28
+ EM::Iterator.new(list,10).each(proc {|num,iter|
29
+ time = get_time
30
+ items[time] ||= []
31
+ items[time] << num
32
+ EM::Timer.new(1) {iter.next}
33
+ }, proc {EM.stop})
34
+ }
35
+ assert_equal(1, items.keys.size)
36
+ assert_equal((list).to_a, items.values.flatten)
37
+ end
38
+
39
+
40
+ def test_changing_concurrency_affects_active_iteration
41
+ items = {}
42
+ list = 1..25
43
+ EM.run {
44
+ i = EM::Iterator.new(list,5)
45
+ i.each(proc {|num,iter|
46
+ time = get_time
47
+ items[time] ||= []
48
+ items[time] << num
49
+ EM::Timer.new(1) {iter.next}
50
+ }, proc {EM.stop})
51
+ EM.add_timer(1){
52
+ i.concurrency = 1
53
+ }
54
+ EM.add_timer(3){
55
+ i.concurrency = 3
56
+ }
57
+ }
58
+ assert_equal(9, items.keys.size)
59
+ assert_equal((list).to_a, items.values.flatten)
60
+ end
61
+
62
+ def test_map
63
+ list = 100..150
64
+ EM.run {
65
+ EM::Iterator.new(list).map(proc{ |num,iter|
66
+ EM.add_timer(0.01){ iter.return(num) }
67
+ }, proc{ |results|
68
+ assert_equal((list).to_a.size, results.size)
69
+ EM.stop
70
+ })
71
+ }
72
+ end
73
+
74
+ def test_inject
75
+ list = %w[ pwd uptime uname date ]
76
+ EM.run {
77
+ EM::Iterator.new(list, 2).inject({}, proc{ |hash,cmd,iter|
78
+ EM.system(cmd){ |output,status|
79
+ hash[cmd] = status.exitstatus == 0 ? output.strip : nil
80
+ iter.return(hash)
81
+ }
82
+ }, proc{ |results|
83
+ assert_equal(results.keys, list)
84
+ EM.stop
85
+ })
86
+ }
87
+ end
88
+
89
+ def test_concurrency_is_0
90
+ EM.run {
91
+ assert_raise ArgumentError do
92
+ EM::Iterator.new(1..5,0)
93
+ end
94
+ EM.stop
95
+ }
96
+ end
97
+ end
data/tests/test_kb.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestKeyboardEvents < Test::Unit::TestCase
4
+
5
+ if !jruby?
6
+ module KbHandler
7
+ include EM::Protocols::LineText2
8
+ def receive_line d
9
+ EM::stop if d == "STOP"
10
+ end
11
+ end
12
+
13
+ # This test doesn't actually do anything useful but is here to
14
+ # illustrate the usage. If you removed the timer and ran this test
15
+ # by itself on a console, and then typed into the console, it would
16
+ # work.
17
+ # I don't know how to get the test harness to simulate actual keystrokes.
18
+ # When someone figures that out, then we can make this a real test.
19
+ #
20
+ def test_kb
21
+ EM.run {
22
+ EM.open_keyboard KbHandler
23
+ EM::Timer.new(1) { EM.stop }
24
+ } if $stdout.tty? # don't run the test unless it stands a chance of validity.
25
+ end
26
+ else
27
+ warn "EM.open_keyboard not implemented, skipping tests in #{__FILE__}"
28
+
29
+ # Because some rubies will complain if a TestCase class has no tests
30
+ def test_em_open_keyboard_unsupported
31
+ assert true
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestLineProtocol < Test::Unit::TestCase
4
+ class LineProtocolTestClass
5
+ include EM::Protocols::LineProtocol
6
+
7
+ def lines
8
+ @lines ||= []
9
+ end
10
+
11
+ def receive_line(line)
12
+ lines << line
13
+ end
14
+ end
15
+
16
+ def setup
17
+ @proto = LineProtocolTestClass.new
18
+ end
19
+
20
+ def test_simple_split_line
21
+ @proto.receive_data("this is")
22
+ assert_equal([], @proto.lines)
23
+
24
+ @proto.receive_data(" a test\n")
25
+ assert_equal(["this is a test"], @proto.lines)
26
+ end
27
+
28
+ def test_simple_lines
29
+ @proto.receive_data("aaa\nbbb\r\nccc\nddd")
30
+ assert_equal(%w(aaa bbb ccc), @proto.lines)
31
+ end
32
+
33
+ end
data/tests/test_ltp.rb ADDED
@@ -0,0 +1,138 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestLineAndTextProtocol < Test::Unit::TestCase
4
+
5
+ class SimpleLineTest < EM::P::LineAndTextProtocol
6
+ def receive_line line
7
+ @line_buffer << line
8
+ end
9
+ end
10
+
11
+ module StopClient
12
+ def set_receive_data(&blk)
13
+ @rdb = blk
14
+ end
15
+
16
+ def receive_data data
17
+ @rdb.call(data) if @rdb
18
+ end
19
+
20
+ def unbind
21
+ EM.add_timer(0.1) { EM.stop }
22
+ end
23
+ end
24
+
25
+ def setup
26
+ @port = next_port
27
+ end
28
+
29
+ def test_simple_lines
30
+ lines_received = []
31
+ EM.run {
32
+ EM.start_server( "127.0.0.1", @port, SimpleLineTest ) do |conn|
33
+ conn.instance_eval "@line_buffer = lines_received"
34
+ end
35
+ setup_timeout
36
+
37
+ EM.connect "127.0.0.1", @port, StopClient do |c|
38
+ c.send_data "aaa\nbbb\r\nccc\n"
39
+ c.close_connection_after_writing
40
+ end
41
+ }
42
+ assert_equal( %w(aaa bbb ccc), lines_received )
43
+ end
44
+
45
+ #--------------------------------------------------------------------
46
+
47
+ class SimpleLineTest < EM::P::LineAndTextProtocol
48
+ def receive_error text
49
+ @error_message << text
50
+ end
51
+ end
52
+
53
+ def test_overlength_lines
54
+ lines_received = []
55
+ EM.run {
56
+ EM.start_server( "127.0.0.1", @port, SimpleLineTest ) do |conn|
57
+ conn.instance_eval "@error_message = lines_received"
58
+ end
59
+ setup_timeout
60
+
61
+ EM.connect "127.0.0.1", @port, StopClient do |c|
62
+ c.send_data "a" * (16*1024 + 1)
63
+ c.send_data "\n"
64
+ c.close_connection_after_writing
65
+ end
66
+
67
+ }
68
+ assert_equal( ["overlength line"], lines_received )
69
+ end
70
+
71
+
72
+ #--------------------------------------------------------------------
73
+
74
+ class LineAndTextTest < EM::P::LineAndTextProtocol
75
+ def receive_line line
76
+ if line =~ /content-length:\s*(\d+)/i
77
+ @content_length = $1.to_i
78
+ elsif line.length == 0
79
+ set_binary_mode @content_length
80
+ end
81
+ end
82
+ def receive_binary_data text
83
+ send_data "received #{text.length} bytes"
84
+ close_connection_after_writing
85
+ end
86
+ end
87
+
88
+ def test_lines_and_text
89
+ output = ''
90
+ EM.run {
91
+ EM.start_server( "127.0.0.1", @port, LineAndTextTest )
92
+ setup_timeout
93
+
94
+ EM.connect "127.0.0.1", @port, StopClient do |c|
95
+ c.set_receive_data { |data| output << data }
96
+ c.send_data "Content-length: 400\n"
97
+ c.send_data "\n"
98
+ c.send_data "A" * 400
99
+ EM.add_timer(0.1) { c.close_connection_after_writing }
100
+ end
101
+ }
102
+ assert_equal( "received 400 bytes", output )
103
+ end
104
+
105
+ #--------------------------------------------------------------------
106
+
107
+
108
+ class BinaryTextTest < EM::P::LineAndTextProtocol
109
+ def receive_line line
110
+ if line =~ /content-length:\s*(\d+)/i
111
+ set_binary_mode $1.to_i
112
+ else
113
+ raise "protocol error"
114
+ end
115
+ end
116
+ def receive_binary_data text
117
+ send_data "received #{text.length} bytes"
118
+ close_connection_after_writing
119
+ end
120
+ end
121
+
122
+ def test_binary_text
123
+ output = ''
124
+ EM.run {
125
+ EM.start_server( "127.0.0.1", @port, BinaryTextTest )
126
+ setup_timeout
127
+
128
+ EM.connect "127.0.0.1", @port, StopClient do |c|
129
+ c.set_receive_data { |data| output << data }
130
+ c.send_data "Content-length: 10000\n"
131
+ c.send_data "A" * 10000
132
+ EM.add_timer(0.1) { c.close_connection_after_writing }
133
+ end
134
+ }
135
+ assert_equal( "received 10000 bytes", output )
136
+ end
137
+
138
+ end
@@ -0,0 +1,288 @@
1
+ require 'em_test_helper'
2
+
3
+ # TODO!!! Need tests for overlength headers and text bodies.
4
+
5
+ class TestLineText2 < Test::Unit::TestCase
6
+
7
+ # Run each of these tests two ways: passing in the whole test-dataset in one chunk,
8
+ # and passing it in one character at a time.
9
+
10
+ class Basic
11
+ include EM::Protocols::LineText2
12
+ attr_reader :lines
13
+ def receive_line line
14
+ (@lines ||= []) << line
15
+ end
16
+ end
17
+ def test_basic
18
+ testdata = "Line 1\nLine 2\r\nLine 3\n"
19
+
20
+ a = Basic.new
21
+ a.receive_data testdata
22
+ assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
23
+
24
+ a = Basic.new
25
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
26
+ assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
27
+ end
28
+
29
+ class ChangeDelimiter
30
+ include EM::Protocols::LineText2
31
+ attr_reader :lines
32
+ def initialize *args
33
+ super
34
+ @delim = "A"
35
+ set_delimiter @delim
36
+ end
37
+ def receive_line line
38
+ (@lines ||= []) << line
39
+ set_delimiter( @delim.succ! )
40
+ end
41
+ end
42
+
43
+ def test_change_delimiter
44
+ testdata = %Q(LineaALinebBLinecCLinedD)
45
+
46
+ a = ChangeDelimiter.new
47
+ a.receive_data testdata
48
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
49
+
50
+ a = ChangeDelimiter.new
51
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
52
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
53
+ end
54
+
55
+
56
+ #--
57
+ # Test two lines followed by an empty line, ten bytes of binary data, then
58
+ # two more lines.
59
+
60
+ class Binary
61
+ include EM::Protocols::LineText2
62
+ attr_reader :lines, :body
63
+ def initialize *args
64
+ super
65
+ @lines = []
66
+ @body = nil
67
+ end
68
+ def receive_line ln
69
+ if ln == ""
70
+ set_text_mode 10
71
+ else
72
+ @lines << ln
73
+ end
74
+ end
75
+ def receive_binary_data data
76
+ @body = data
77
+ end
78
+ end
79
+
80
+ def test_binary
81
+ testdata = %Q(Line 1
82
+ Line 2
83
+
84
+ 0000000000Line 3
85
+ Line 4
86
+ )
87
+
88
+ a = Binary.new
89
+ a.receive_data testdata
90
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
91
+ assert_equal( "0000000000", a.body )
92
+
93
+ a = Binary.new
94
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
95
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
96
+ assert_equal( "0000000000", a.body )
97
+ end
98
+
99
+
100
+ # Test unsized binary data. The expectation is that each chunk of it
101
+ # will be passed to us as it it received.
102
+ class UnsizedBinary
103
+ include EM::Protocols::LineText2
104
+ attr_reader :n_calls, :body
105
+ def initialize *args
106
+ super
107
+ set_text_mode
108
+ end
109
+ def receive_binary_data data
110
+ @n_calls ||= 0
111
+ @n_calls += 1
112
+ (@body ||= "") << data
113
+ end
114
+ end
115
+
116
+ def test_unsized_binary
117
+ testdata = "X\0" * 1000
118
+
119
+ a = UnsizedBinary.new
120
+ a.receive_data testdata
121
+ assert_equal( 1, a.n_calls )
122
+ assert_equal( testdata, a.body )
123
+
124
+ a = UnsizedBinary.new
125
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
126
+ assert_equal( 2000, a.n_calls )
127
+ assert_equal( testdata, a.body )
128
+ end
129
+
130
+
131
+ # Test binary data with a "throw back" into line-mode.
132
+ class ThrowBack
133
+ include EM::Protocols::LineText2
134
+ attr_reader :headers
135
+ def initialize *args
136
+ super
137
+ @headers = []
138
+ @n_bytes = 0
139
+ set_text_mode
140
+ end
141
+ def receive_binary_data data
142
+ wanted = 25 - @n_bytes
143
+ will_take = if data.length > wanted
144
+ data.length - wanted
145
+ else
146
+ data.length
147
+ end
148
+ @n_bytes += will_take
149
+
150
+ if @n_bytes == 25
151
+ set_line_mode( data[will_take..-1] )
152
+ end
153
+ end
154
+ def receive_line ln
155
+ @headers << ln
156
+ end
157
+ end
158
+ def test_throw_back
159
+ testdata = "Line\n" * 10
160
+
161
+ a = ThrowBack.new
162
+ a.receive_data testdata
163
+ assert_equal( ["Line"] * 5, a.headers )
164
+
165
+ a = ThrowBack.new
166
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
167
+ assert_equal( ["Line"] * 5, a.headers )
168
+ end
169
+
170
+ # Test multi-character line delimiters.
171
+ # Also note that the test data has a "tail" with no delimiter, that will be
172
+ # discarded, but cf. the BinaryTail test.
173
+ # TODO!!! This test doesn't work in the byte-by-byte case.
174
+ class Multichar
175
+ include EM::Protocols::LineText2
176
+ attr_reader :lines
177
+ def initialize *args
178
+ super
179
+ @lines = []
180
+ set_delimiter "012"
181
+ end
182
+ def receive_line ln
183
+ @lines << ln
184
+ end
185
+ end
186
+ def test_multichar
187
+ testdata = "Line012Line012Line012Line"
188
+
189
+ a = Multichar.new
190
+ a.receive_data testdata
191
+ assert_equal( ["Line"]*3, a.lines )
192
+
193
+ a = Multichar.new
194
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
195
+ # DOESN'T WORK in this case. Multi-character delimiters are broken.
196
+ #assert_equal( ["Line"]*3, a.lines )
197
+ end
198
+
199
+ # Test a binary "tail," when a sized binary transfer doesn't complete because
200
+ # of an unbind. We get a partial result.
201
+ class BinaryTail
202
+ include EM::Protocols::LineText2
203
+ attr_reader :data
204
+ def initialize *args
205
+ super
206
+ @data = ""
207
+ set_text_mode 1000
208
+ end
209
+ def receive_binary_data data
210
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
211
+ # because sized transfers by definition give us exactly one call to
212
+ # #receive_binary_data.
213
+ @data = data
214
+ end
215
+ end
216
+ def test_binary_tail
217
+ testdata = "0" * 500
218
+
219
+ a = BinaryTail.new
220
+ a.receive_data testdata
221
+ a.unbind
222
+ assert_equal( "0" * 500, a.data )
223
+
224
+ a = BinaryTail.new
225
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
226
+ a.unbind
227
+ assert_equal( "0" * 500, a.data )
228
+ end
229
+
230
+
231
+ # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it
232
+ # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to
233
+ # line mode.
234
+ # At the present time, this isn't strictly necessary with sized binary chunks because by
235
+ # definition we accumulate them and make exactly one call to receive_binary_data, but
236
+ # we may want to support a mode in the future that would break up large chunks into multiple
237
+ # calls.
238
+ class LazyBinary
239
+ include EM::Protocols::LineText2
240
+ attr_reader :data, :end
241
+ def initialize *args
242
+ super
243
+ @data = ""
244
+ set_text_mode 1000
245
+ end
246
+ def receive_binary_data data
247
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
248
+ # because sized transfers by definition give us exactly one call to
249
+ # #receive_binary_data.
250
+ @data = data
251
+ end
252
+ def receive_end_of_binary_data
253
+ @end = true
254
+ end
255
+ end
256
+ def test_receive_end_of_binary_data
257
+ testdata = "_" * 1000
258
+ a = LazyBinary.new
259
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
260
+ assert_equal( "_" * 1000, a.data )
261
+ assert( a.end )
262
+ end
263
+
264
+
265
+ # This tests a bug fix in which calling set_text_mode failed when called
266
+ # inside receive_binary_data.
267
+ #
268
+ class BinaryPair
269
+ include EM::Protocols::LineText2
270
+ attr_reader :sizes
271
+ def initialize *args
272
+ super
273
+ set_text_mode 1
274
+ @sizes = []
275
+ end
276
+ def receive_binary_data dt
277
+ @sizes << dt.length
278
+ set_text_mode( (dt.length == 1) ? 2 : 1 )
279
+ end
280
+ end
281
+ def test_binary_pairs
282
+ test_data = "123" * 5
283
+ a = BinaryPair.new
284
+ a.receive_data test_data
285
+ assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )
286
+ end
287
+
288
+ end