eventmachine-le 1.1.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. data/.gitignore +21 -0
  2. data/.yardopts +7 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +80 -0
  6. data/Rakefile +19 -0
  7. data/eventmachine-le.gemspec +42 -0
  8. data/ext/binder.cpp +124 -0
  9. data/ext/binder.h +46 -0
  10. data/ext/cmain.cpp +841 -0
  11. data/ext/ed.cpp +1995 -0
  12. data/ext/ed.h +424 -0
  13. data/ext/em.cpp +2377 -0
  14. data/ext/em.h +243 -0
  15. data/ext/eventmachine.h +126 -0
  16. data/ext/extconf.rb +166 -0
  17. data/ext/fastfilereader/extconf.rb +94 -0
  18. data/ext/fastfilereader/mapper.cpp +214 -0
  19. data/ext/fastfilereader/mapper.h +59 -0
  20. data/ext/fastfilereader/rubymain.cpp +127 -0
  21. data/ext/kb.cpp +79 -0
  22. data/ext/page.cpp +107 -0
  23. data/ext/page.h +51 -0
  24. data/ext/pipe.cpp +347 -0
  25. data/ext/project.h +155 -0
  26. data/ext/rubymain.cpp +1269 -0
  27. data/ext/ssl.cpp +468 -0
  28. data/ext/ssl.h +94 -0
  29. data/lib/em/buftok.rb +110 -0
  30. data/lib/em/callback.rb +58 -0
  31. data/lib/em/channel.rb +64 -0
  32. data/lib/em/completion.rb +304 -0
  33. data/lib/em/connection.rb +728 -0
  34. data/lib/em/deferrable.rb +210 -0
  35. data/lib/em/deferrable/pool.rb +2 -0
  36. data/lib/em/file_watch.rb +73 -0
  37. data/lib/em/future.rb +61 -0
  38. data/lib/em/iterator.rb +313 -0
  39. data/lib/em/messages.rb +66 -0
  40. data/lib/em/pool.rb +151 -0
  41. data/lib/em/process_watch.rb +45 -0
  42. data/lib/em/processes.rb +123 -0
  43. data/lib/em/protocols.rb +37 -0
  44. data/lib/em/protocols/header_and_content.rb +138 -0
  45. data/lib/em/protocols/httpclient.rb +279 -0
  46. data/lib/em/protocols/httpclient2.rb +600 -0
  47. data/lib/em/protocols/line_and_text.rb +125 -0
  48. data/lib/em/protocols/line_protocol.rb +29 -0
  49. data/lib/em/protocols/linetext2.rb +161 -0
  50. data/lib/em/protocols/memcache.rb +331 -0
  51. data/lib/em/protocols/object_protocol.rb +46 -0
  52. data/lib/em/protocols/postgres3.rb +246 -0
  53. data/lib/em/protocols/saslauth.rb +175 -0
  54. data/lib/em/protocols/smtpclient.rb +365 -0
  55. data/lib/em/protocols/smtpserver.rb +663 -0
  56. data/lib/em/protocols/socks4.rb +66 -0
  57. data/lib/em/protocols/stomp.rb +202 -0
  58. data/lib/em/protocols/tcptest.rb +54 -0
  59. data/lib/em/queue.rb +71 -0
  60. data/lib/em/resolver.rb +195 -0
  61. data/lib/em/spawnable.rb +84 -0
  62. data/lib/em/streamer.rb +118 -0
  63. data/lib/em/threaded_resource.rb +90 -0
  64. data/lib/em/tick_loop.rb +85 -0
  65. data/lib/em/timers.rb +106 -0
  66. data/lib/em/version.rb +3 -0
  67. data/lib/eventmachine-le.rb +10 -0
  68. data/lib/eventmachine.rb +1548 -0
  69. data/rakelib/cpp.rake_example +77 -0
  70. data/rakelib/package.rake +98 -0
  71. data/rakelib/test.rake +8 -0
  72. data/tests/client.crt +31 -0
  73. data/tests/client.key +51 -0
  74. data/tests/em_test_helper.rb +143 -0
  75. data/tests/test_attach.rb +148 -0
  76. data/tests/test_basic.rb +294 -0
  77. data/tests/test_channel.rb +62 -0
  78. data/tests/test_completion.rb +177 -0
  79. data/tests/test_connection_count.rb +33 -0
  80. data/tests/test_defer.rb +18 -0
  81. data/tests/test_deferrable.rb +35 -0
  82. data/tests/test_epoll.rb +134 -0
  83. data/tests/test_error_handler.rb +38 -0
  84. data/tests/test_exc.rb +28 -0
  85. data/tests/test_file_watch.rb +65 -0
  86. data/tests/test_futures.rb +170 -0
  87. data/tests/test_get_sock_opt.rb +37 -0
  88. data/tests/test_handler_check.rb +35 -0
  89. data/tests/test_hc.rb +155 -0
  90. data/tests/test_httpclient.rb +190 -0
  91. data/tests/test_httpclient2.rb +128 -0
  92. data/tests/test_inactivity_timeout.rb +54 -0
  93. data/tests/test_ipv4.rb +125 -0
  94. data/tests/test_ipv6.rb +131 -0
  95. data/tests/test_iterator.rb +110 -0
  96. data/tests/test_kb.rb +34 -0
  97. data/tests/test_line_protocol.rb +33 -0
  98. data/tests/test_ltp.rb +138 -0
  99. data/tests/test_ltp2.rb +288 -0
  100. data/tests/test_next_tick.rb +104 -0
  101. data/tests/test_object_protocol.rb +36 -0
  102. data/tests/test_pause.rb +78 -0
  103. data/tests/test_pending_connect_timeout.rb +52 -0
  104. data/tests/test_pool.rb +196 -0
  105. data/tests/test_process_watch.rb +48 -0
  106. data/tests/test_processes.rb +133 -0
  107. data/tests/test_proxy_connection.rb +168 -0
  108. data/tests/test_pure.rb +88 -0
  109. data/tests/test_queue.rb +50 -0
  110. data/tests/test_resolver.rb +55 -0
  111. data/tests/test_running.rb +14 -0
  112. data/tests/test_sasl.rb +47 -0
  113. data/tests/test_send_file.rb +217 -0
  114. data/tests/test_servers.rb +33 -0
  115. data/tests/test_set_sock_opt.rb +41 -0
  116. data/tests/test_shutdown_hooks.rb +23 -0
  117. data/tests/test_smtpclient.rb +55 -0
  118. data/tests/test_smtpserver.rb +120 -0
  119. data/tests/test_spawn.rb +293 -0
  120. data/tests/test_ssl_args.rb +78 -0
  121. data/tests/test_ssl_methods.rb +48 -0
  122. data/tests/test_ssl_verify.rb +82 -0
  123. data/tests/test_threaded_resource.rb +55 -0
  124. data/tests/test_tick_loop.rb +59 -0
  125. data/tests/test_timers.rb +180 -0
  126. data/tests/test_ud.rb +8 -0
  127. data/tests/test_udp46.rb +53 -0
  128. data/tests/test_unbind_reason.rb +48 -0
  129. metadata +390 -0
@@ -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,125 @@
1
+ require 'em_test_helper'
2
+ require 'socket'
3
+
4
+ class TestIPv4 < Test::Unit::TestCase
5
+
6
+ if Test::Unit::TestCase.public_ipv4?
7
+
8
+ # Tries to connect to www.google.com port 80 via TCP.
9
+ # Timeout in 2 seconds.
10
+ def test_ipv4_tcp_client
11
+ conn = nil
12
+ setup_timeout(2)
13
+
14
+ EM.run do
15
+ conn = EM::connect("www.google.es", 80) do |c|
16
+ def c.connected
17
+ @connected
18
+ end
19
+
20
+ def c.connection_completed
21
+ @connected = true
22
+ EM.stop
23
+ end
24
+ end
25
+ end
26
+
27
+ assert conn.connected
28
+ end
29
+
30
+ # Runs a TCP server in the local IPv4 address, connects to it and sends a specific data.
31
+ # Timeout in 2 seconds.
32
+ def test_ipv4_tcp_local_server
33
+ @@received_data = nil
34
+ @local_port = next_port
35
+ setup_timeout(2)
36
+
37
+ EM.run do
38
+ EM::start_server(@@public_ipv4, @local_port) do |s|
39
+ def s.receive_data data
40
+ @@received_data = data
41
+ EM.stop
42
+ end
43
+ end
44
+
45
+ EM::connect(@@public_ipv4, @local_port) do |c|
46
+ c.send_data "ipv4/tcp"
47
+ end
48
+ end
49
+
50
+ assert_equal "ipv4/tcp", @@received_data
51
+ end
52
+
53
+ # Runs a UDP server in the local IPv4 address, connects to it and sends a specific data.
54
+ # Timeout in 2 seconds.
55
+ def test_ipv4_udp_local_server
56
+ @@received_data = nil
57
+ @local_port = next_port
58
+ setup_timeout(2)
59
+
60
+ EM.run do
61
+ EM::open_datagram_socket(@@public_ipv4, @local_port) do |s|
62
+ def s.receive_data data
63
+ @@received_data = data
64
+ EM.stop
65
+ end
66
+ end
67
+
68
+ EM::open_datagram_socket(@@public_ipv4, next_port) do |c|
69
+ c.send_datagram "ipv4/udp", @@public_ipv4, @local_port
70
+ end
71
+ end
72
+
73
+ assert_equal "ipv4/udp", @@received_data
74
+ end
75
+
76
+ # Try to connect via TCP to an invalid IPv4. EM.connect should raise
77
+ # EM::ConnectionError.
78
+ def test_tcp_connect_to_invalid_ipv4
79
+ invalid_ipv4 = "9.9:9"
80
+
81
+ EM.run do
82
+ begin
83
+ error = nil
84
+ EM.connect(invalid_ipv4, 1234)
85
+ rescue => e
86
+ error = e
87
+ ensure
88
+ EM.stop
89
+ assert_equal EM::ConnectionError, (error && error.class)
90
+ end
91
+ end
92
+ end
93
+
94
+ # Try to send a UDP datagram to an invalid IPv4. EM.send_datagram should raise
95
+ # EM::ConnectionError.
96
+ def test_udp_send_datagram_to_invalid_ipv4
97
+ invalid_ipv4 = "9.9:9"
98
+
99
+ EM.run do
100
+ begin
101
+ error = nil
102
+ EM.open_datagram_socket(@@public_ipv4, next_port) do |c|
103
+ c.send_datagram "hello", invalid_ipv4, 1234
104
+ end
105
+ rescue => e
106
+ error = e
107
+ ensure
108
+ EM.stop
109
+ assert_equal EM::ConnectionError, (error && error.class)
110
+ end
111
+ end
112
+ end
113
+
114
+
115
+ else
116
+ warn "no IPv4 in this host, skipping tests in #{__FILE__}"
117
+
118
+ # Because some rubies will complain if a TestCase class has no tests
119
+ def test_ipv4_unavailable
120
+ assert true
121
+ end
122
+
123
+ end
124
+
125
+ end
@@ -0,0 +1,131 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestIPv6 < Test::Unit::TestCase
4
+
5
+ if Test::Unit::TestCase.public_ipv6?
6
+
7
+ # Tries to connect to ipv6.google.com (2a00:1450:4001:c01::93) port 80 via TCP.
8
+ # Timeout in 6 seconds.
9
+ def test_ipv6_tcp_client_with_ipv6_google_com
10
+ conn = nil
11
+ setup_timeout(6)
12
+
13
+ EM.run do
14
+ conn = EM::connect("2a00:1450:4001:c01::93", 80) do |c|
15
+ def c.connected
16
+ @connected
17
+ end
18
+
19
+ def c.unbind(reason)
20
+ warn "unbind: #{reason.inspect}" if reason # XXX at least find out why it failed
21
+ end
22
+
23
+ def c.connection_completed
24
+ @connected = true
25
+ EM.stop
26
+ end
27
+ end
28
+ end
29
+
30
+ assert conn.connected
31
+ end
32
+
33
+ # Runs a TCP server in the local IPv6 address, connects to it and sends a specific data.
34
+ # Timeout in 2 seconds.
35
+ def test_ipv6_tcp_local_server
36
+ @@received_data = nil
37
+ @local_port = next_port
38
+ setup_timeout(2)
39
+
40
+ EM.run do
41
+ EM.start_server(@@public_ipv6, @local_port) do |s|
42
+ def s.receive_data data
43
+ @@received_data = data
44
+ EM.stop
45
+ end
46
+ end
47
+
48
+ EM::connect(@@public_ipv6, @local_port) do |c|
49
+ def c.unbind(reason)
50
+ warn "unbind: #{reason.inspect}" if reason # XXX at least find out why it failed
51
+ end
52
+ c.send_data "ipv6/tcp"
53
+ end
54
+ end
55
+
56
+ assert_equal "ipv6/tcp", @@received_data
57
+ end
58
+
59
+ # Runs a UDP server in the local IPv6 address, connects to it and sends a specific data.
60
+ # Timeout in 2 seconds.
61
+ def test_ipv6_udp_local_server
62
+ @@received_data = nil
63
+ @local_port = next_port
64
+ setup_timeout(2)
65
+
66
+ EM.run do
67
+ EM.open_datagram_socket(@@public_ipv6, @local_port) do |s|
68
+ def s.receive_data data
69
+ @@received_data = data
70
+ EM.stop
71
+ end
72
+ end
73
+
74
+ EM.open_datagram_socket(@@public_ipv6, next_port) do |c|
75
+ c.send_datagram "ipv6/udp", @@public_ipv6, @local_port
76
+ end
77
+ end
78
+
79
+ assert_equal "ipv6/udp", @@received_data
80
+ end
81
+
82
+ # Try to connect via TCP to an invalid IPv6. EM.connect should raise
83
+ # EM::ConnectionError.
84
+ def test_tcp_connect_to_invalid_ipv6
85
+ invalid_ipv6 = "1:A"
86
+
87
+ EM.run do
88
+ begin
89
+ error = nil
90
+ EM.connect(invalid_ipv6, 1234)
91
+ rescue => e
92
+ error = e
93
+ ensure
94
+ EM.stop
95
+ assert_equal EM::ConnectionError, (error && error.class)
96
+ end
97
+ end
98
+ end
99
+
100
+ # Try to send a UDP datagram to an invalid IPv6. EM.send_datagram should raise
101
+ # EM::ConnectionError.
102
+ def test_udp_send_datagram_to_invalid_ipv6
103
+ invalid_ipv6 = "1:A"
104
+
105
+ EM.run do
106
+ begin
107
+ error = nil
108
+ EM.open_datagram_socket(@@public_ipv6, next_port) do |c|
109
+ c.send_datagram "hello", invalid_ipv6, 1234
110
+ end
111
+ rescue => e
112
+ error = e
113
+ ensure
114
+ EM.stop
115
+ assert_equal EM::ConnectionError, (error && error.class)
116
+ end
117
+ end
118
+ end
119
+
120
+
121
+ else
122
+ warn "no IPv6 in this host, skipping tests in #{__FILE__}"
123
+
124
+ # Because some rubies will complain if a TestCase class has no tests.
125
+ def test_ipv6_unavailable
126
+ assert true
127
+ end
128
+
129
+ end
130
+
131
+ end
@@ -0,0 +1,110 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestEnumerable
4
+ include Enumerable
5
+
6
+ def each
7
+ while (num = rand(20)) != 10 do
8
+ yield num
9
+ end
10
+ end
11
+ end
12
+
13
+ class TestEnumerable2
14
+ include Enumerable
15
+
16
+ def each
17
+ arr = ('a'..'g').to_a
18
+ while it = arr.shift do
19
+ yield it
20
+ end
21
+ end
22
+ end
23
+
24
+ class TestEnumerable3
25
+ include Enumerable
26
+
27
+ def each
28
+ ('a'..'f').each.with_index do |ltr, index|
29
+ break if ltr == 'e'
30
+ eval("@#{ltr} = #{index}")
31
+ yield ltr
32
+ end
33
+ end
34
+ end
35
+
36
+ class TestIterator < Test::Unit::TestCase
37
+
38
+ def test_iterator_with_array
39
+ assert_nothing_raised do
40
+ EM.run {
41
+ after = proc{ EM.stop }
42
+ EM::Iterator.new(0..10, 10).each(nil, after){ |num,iter| iter.next }
43
+ }
44
+ end
45
+ end
46
+
47
+ def test_iterator_with_array_with_result
48
+ nums = []
49
+ EM.run {
50
+ after = proc{ EM.stop }
51
+ EM::Iterator.new((0..10)).each(nil, after){ |num,iter|
52
+ nums << num
53
+ iter.next
54
+ }
55
+ }
56
+ res = (0..10).to_a
57
+ assert_equal res, nums
58
+ end
59
+
60
+ def test_iterator_with_enumerable
61
+ assert_nothing_raised do
62
+ EM.run {
63
+ en = TestEnumerable.new
64
+ after = proc{ EM.stop }
65
+ EM::Iterator.new(en, 10).each(nil, after){ |num, iter| iter.next }
66
+ }
67
+ end
68
+ end
69
+
70
+ def test_iterator_with_enumerable_with_result
71
+ letters = []
72
+ EM.run {
73
+ en = TestEnumerable2.new
74
+ after = proc{ EM.stop }
75
+ EM::Iterator.new(en, 10).each(nil, after){ |ltr,iter|
76
+ letters << ltr
77
+ iter.next
78
+ }
79
+ }
80
+ res = ('a'..'g').to_a
81
+ assert_equal res, letters
82
+ end
83
+
84
+ def test_iterator_with_array_with_nils
85
+ nums = []
86
+ EM.run {
87
+ after = proc{ EM.stop }
88
+ EM::Iterator.new(["Hello", nil, "World", nil]).each(nil, after){ |num,iter|
89
+ nums << num
90
+ iter.next
91
+ }
92
+ }
93
+ res = "Hello World"
94
+ assert_equal res, nums.compact.join(" ")
95
+ end
96
+
97
+ def test_iterator_for_lazyness
98
+ enumerable = TestEnumerable3.new
99
+ enumerator = enumerable.to_enum
100
+ EM.run {
101
+ after = proc{ EM.stop }
102
+ EM::Iterator.new(enumerator).each(nil, after){ |num,iter|
103
+ iter.next
104
+ }
105
+ }
106
+ assert_equal 0, enumerable.instance_variable_get(:@a)
107
+ assert_equal 3, enumerable.instance_variable_get(:@d)
108
+ assert_equal false, enumerable.instance_variable_defined?(:@e)
109
+ end
110
+ 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