rbczmq 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/.gitignore +23 -0
  2. data/.travis.yml +19 -0
  3. data/Gemfile +5 -0
  4. data/Gemfile.lock +19 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +247 -0
  7. data/Rakefile +67 -0
  8. data/examples/loop.rb +109 -0
  9. data/examples/poller.rb +37 -0
  10. data/examples/pub_sub.rb +101 -0
  11. data/examples/push_pull.rb +104 -0
  12. data/examples/req_rep.rb +100 -0
  13. data/ext/czmq.tar.gz +0 -0
  14. data/ext/rbczmq/context.c +280 -0
  15. data/ext/rbczmq/context.h +26 -0
  16. data/ext/rbczmq/extconf.rb +138 -0
  17. data/ext/rbczmq/frame.c +401 -0
  18. data/ext/rbczmq/frame.h +24 -0
  19. data/ext/rbczmq/jruby.h +22 -0
  20. data/ext/rbczmq/loop.c +413 -0
  21. data/ext/rbczmq/loop.h +24 -0
  22. data/ext/rbczmq/message.c +620 -0
  23. data/ext/rbczmq/message.h +24 -0
  24. data/ext/rbczmq/poller.c +308 -0
  25. data/ext/rbczmq/poller.h +29 -0
  26. data/ext/rbczmq/pollitem.c +251 -0
  27. data/ext/rbczmq/pollitem.h +25 -0
  28. data/ext/rbczmq/rbczmq_ext.c +198 -0
  29. data/ext/rbczmq/rbczmq_ext.h +94 -0
  30. data/ext/rbczmq/rbczmq_prelude.h +22 -0
  31. data/ext/rbczmq/rubinius.h +24 -0
  32. data/ext/rbczmq/ruby18.h +43 -0
  33. data/ext/rbczmq/ruby19.h +15 -0
  34. data/ext/rbczmq/socket.c +1570 -0
  35. data/ext/rbczmq/socket.h +136 -0
  36. data/ext/rbczmq/timer.c +110 -0
  37. data/ext/rbczmq/timer.h +23 -0
  38. data/ext/zeromq.tar.gz +0 -0
  39. data/lib/rbczmq.rb +3 -0
  40. data/lib/zmq.rb +77 -0
  41. data/lib/zmq/context.rb +50 -0
  42. data/lib/zmq/default_handler.rb +16 -0
  43. data/lib/zmq/frame.rb +11 -0
  44. data/lib/zmq/handler.rb +76 -0
  45. data/lib/zmq/loop.rb +131 -0
  46. data/lib/zmq/message.rb +9 -0
  47. data/lib/zmq/poller.rb +22 -0
  48. data/lib/zmq/pollitem.rb +31 -0
  49. data/lib/zmq/socket.rb +125 -0
  50. data/lib/zmq/socket/dealer.rb +33 -0
  51. data/lib/zmq/socket/pair.rb +39 -0
  52. data/lib/zmq/socket/pub.rb +30 -0
  53. data/lib/zmq/socket/pull.rb +29 -0
  54. data/lib/zmq/socket/push.rb +32 -0
  55. data/lib/zmq/socket/rep.rb +37 -0
  56. data/lib/zmq/socket/req.rb +37 -0
  57. data/lib/zmq/socket/router.rb +38 -0
  58. data/lib/zmq/socket/sub.rb +27 -0
  59. data/lib/zmq/timer.rb +12 -0
  60. data/lib/zmq/version.rb +5 -0
  61. data/perf/pair.rb +7 -0
  62. data/perf/pair/local.rb +22 -0
  63. data/perf/pair/remote.rb +25 -0
  64. data/perf/pub_sub.rb +7 -0
  65. data/perf/pub_sub/local.rb +22 -0
  66. data/perf/pub_sub/remote.rb +25 -0
  67. data/perf/push_pull.rb +7 -0
  68. data/perf/push_pull/local.rb +21 -0
  69. data/perf/push_pull/remote.rb +25 -0
  70. data/perf/req_rep.rb +7 -0
  71. data/perf/req_rep/local.rb +35 -0
  72. data/perf/req_rep/remote.rb +28 -0
  73. data/perf/runner.rb +142 -0
  74. data/rbczmq.gemspec +22 -0
  75. data/test/helper.rb +21 -0
  76. data/test/socket/test_dealer_socket.rb +14 -0
  77. data/test/socket/test_pair_socket.rb +24 -0
  78. data/test/socket/test_pair_sockets.rb +74 -0
  79. data/test/socket/test_pub_socket.rb +17 -0
  80. data/test/socket/test_pub_sub_sockets.rb +87 -0
  81. data/test/socket/test_pull_socket.rb +17 -0
  82. data/test/socket/test_push_pull_sockets.rb +81 -0
  83. data/test/socket/test_push_socket.rb +17 -0
  84. data/test/socket/test_rep_socket.rb +25 -0
  85. data/test/socket/test_req_rep_sockets.rb +42 -0
  86. data/test/socket/test_req_socket.rb +27 -0
  87. data/test/socket/test_router_socket.rb +14 -0
  88. data/test/socket/test_routing.rb +66 -0
  89. data/test/socket/test_sub_socket.rb +17 -0
  90. data/test/test_context.rb +86 -0
  91. data/test/test_frame.rb +78 -0
  92. data/test/test_handler.rb +28 -0
  93. data/test/test_loop.rb +252 -0
  94. data/test/test_message.rb +201 -0
  95. data/test/test_poller.rb +154 -0
  96. data/test/test_pollitem.rb +78 -0
  97. data/test/test_socket.rb +403 -0
  98. data/test/test_threading.rb +34 -0
  99. data/test/test_timer.rb +37 -0
  100. data/test/test_zmq.rb +62 -0
  101. metadata +208 -0
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class TestPushSocket < ZmqTestCase
6
+ def test_behavior
7
+ ctx = ZMQ::Context.new
8
+ sock = ctx.socket(:PUSH)
9
+ assert_equal ZMQ::PUSH, sock.type
10
+ assert_equal "PUSH socket", sock.to_s
11
+ assert_raises ZMQ::Error do
12
+ sock.recv
13
+ end
14
+ ensure
15
+ ctx.destroy
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class TestRepSocket < ZmqTestCase
6
+ def test_behavior
7
+ ctx = ZMQ::Context.new
8
+ sock = ctx.socket(:REP)
9
+ assert_equal ZMQ::REP, sock.type
10
+ assert_equal "REP socket", sock.to_s
11
+ ensure
12
+ ctx.destroy
13
+ end
14
+
15
+ def test_flow
16
+ ctx = ZMQ::Context.new
17
+ sock = ctx.socket(:REP)
18
+ sock.bind("inproc://test.rep-sock-flow")
19
+ assert_raises ZMQ::Error do
20
+ sock.send_frame(ZMQ::Frame("frame"), ZMQ::Frame::MORE)
21
+ end
22
+ ensure
23
+ ctx.destroy
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class TestReqRepSockets < ZmqTestCase
6
+ def test_flow
7
+ ctx = ZMQ::Context.new
8
+ rep = ctx.bind(:REP, "inproc://test.req-rep-flow")
9
+ ctx.connect(:REQ, "inproc://test.req-rep-flow")
10
+ begin
11
+ rep.send("message")
12
+ rescue ZMQ::Error => e
13
+ assert_match(/REP sockets allows only an alternating sequence of receive and subsequent send calls. Please assert that you're not sending \/ receiving out of band data when using the REQ \/ REP socket pairs./, e.message)
14
+ end
15
+ ensure
16
+ ctx.destroy
17
+ end
18
+
19
+ def test_transfer
20
+ ctx = ZMQ::Context.new
21
+ rep = ctx.bind(:REP, "inproc://test.req-rep-transfer")
22
+ req = ctx.connect(:REQ, "inproc://test.req-rep-transfer")
23
+ req.send("message")
24
+ assert_equal "message", rep.recv
25
+ ensure
26
+ ctx.destroy
27
+ end
28
+
29
+ def test_distribution
30
+ ctx = ZMQ::Context.new
31
+ rep = ctx.bind(:REP, "inproc://test.req-rep-distribution")
32
+ thread = Thread.new do
33
+ req = ctx.connect(:REQ, "inproc://test.req-rep-distribution")
34
+ req.send_frame ZMQ::Frame("message")
35
+ req.close
36
+ end
37
+ thread.join
38
+ assert_equal ZMQ::Frame("message"), rep.recv_frame
39
+ ensure
40
+ ctx.destroy
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class TestReqSocket < ZmqTestCase
6
+ def test_behavior
7
+ ctx = ZMQ::Context.new
8
+ sock = ctx.socket(:REQ)
9
+ assert_equal ZMQ::REQ, sock.type
10
+ assert_equal "REQ socket", sock.to_s
11
+ ensure
12
+ ctx.destroy
13
+ end
14
+
15
+ def test_flow
16
+ ctx = ZMQ::Context.new
17
+ rep = ctx.socket(:REP)
18
+ rep.bind("inproc://test.req-sock-flow")
19
+ sock = ctx.socket(:REQ)
20
+ sock.connect("inproc://test.req-sock-flow")
21
+ assert_raises ZMQ::Error do
22
+ sock.send_frame(ZMQ::Frame("frame"), ZMQ::Frame::MORE)
23
+ end
24
+ ensure
25
+ ctx.destroy
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class TestRouterSocket < ZmqTestCase
6
+ def test_behavior
7
+ ctx = ZMQ::Context.new
8
+ sock = ctx.socket(:ROUTER)
9
+ assert_equal ZMQ::ROUTER, sock.type
10
+ assert_equal "ROUTER socket", sock.to_s
11
+ ensure
12
+ ctx.destroy
13
+ end
14
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class TestRouting < ZmqTestCase
6
+ def test_flow
7
+ ctx = ZMQ::Context.new
8
+ router = ctx.bind(:ROUTER, "inproc://test.routing-flow")
9
+ dealer = ctx.socket(:DEALER)
10
+ dealer.identity = "xyz"
11
+ dealer.connect("inproc://test.routing-flow")
12
+
13
+ router.sendm("xyz")
14
+ router.send("request")
15
+ assert_equal "request", dealer.recv
16
+
17
+ dealer.send("reply")
18
+ assert_equal "xyz", router.recv
19
+ assert_equal "reply", router.recv
20
+ ensure
21
+ ctx.destroy
22
+ end
23
+
24
+ def test_transfer
25
+ ctx = ZMQ::Context.new
26
+ router = ctx.bind(:ROUTER, "inproc://test.routing-transfer")
27
+ dealer = ctx.socket(:DEALER)
28
+ dealer.identity = "xyz"
29
+ dealer.connect("inproc://test.routing-transfer")
30
+
31
+ router.send_frame ZMQ::Frame("xyz"), ZMQ::Frame::MORE
32
+ router.send_frame ZMQ::Frame("request")
33
+
34
+ assert_equal ZMQ::Frame("request"), dealer.recv_frame
35
+
36
+ req = ZMQ::Message.new
37
+ req.push ZMQ::Frame("request")
38
+ req.push ZMQ::Frame("xyz")
39
+ router.send_message req
40
+
41
+ msg = dealer.recv_message
42
+ assert_equal ZMQ::Frame("request"), msg.pop
43
+ ensure
44
+ ctx.destroy
45
+ end
46
+
47
+ def test_distribution
48
+ ctx = ZMQ::Context.new
49
+ router = ctx.bind(:ROUTER, "inproc://test.routing-distribution")
50
+ thread = Thread.new do
51
+ dealer = ctx.socket(:DEALER)
52
+ dealer.identity = "xyz"
53
+ dealer.connect("inproc://test.routing-distribution")
54
+ frame = dealer.recv_frame
55
+ dealer.close
56
+ frame
57
+ end
58
+
59
+ sleep 0.5 # "slow joiner" syndrome
60
+ router.send_frame ZMQ::Frame("xyz"), ZMQ::Frame::MORE
61
+ router.send_frame ZMQ::Frame("message")
62
+ assert_equal ZMQ::Frame("message"), thread.value
63
+ ensure
64
+ ctx.destroy
65
+ end
66
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'helper')
4
+
5
+ class TestSubSocket < ZmqTestCase
6
+ def test_behavior
7
+ ctx = ZMQ::Context.new
8
+ sock = ctx.socket(:SUB)
9
+ assert_equal ZMQ::SUB, sock.type
10
+ assert_equal "SUB socket", sock.to_s
11
+ assert_raises ZMQ::Error do
12
+ sock.send("message")
13
+ end
14
+ ensure
15
+ ctx.destroy
16
+ end
17
+ end
@@ -0,0 +1,86 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestZmqContext < ZmqTestCase
6
+ def test_context
7
+ ctx = ZMQ::Context.new
8
+ assert_instance_of ZMQ::Context, ctx
9
+ assert_raises(ZMQ::Error) do
10
+ ZMQ::Context.new
11
+ end
12
+ assert_equal ctx, ZMQ.context
13
+ ensure
14
+ assert_nil ctx.destroy
15
+ end
16
+
17
+ def test_destroyed_context
18
+ ctx = ZMQ::Context.new
19
+ ctx.destroy
20
+ assert_raises ZMQ::Error do
21
+ ctx.iothreads = 2
22
+ end
23
+ end
24
+
25
+ def test_context_with_iothreads
26
+ ctx = ZMQ::Context.new(2)
27
+ assert_instance_of ZMQ::Context, ctx
28
+ assert_raises(ZMQ::Error) do
29
+ ZMQ::Context.new
30
+ end
31
+ assert_equal ctx, ZMQ.context
32
+ ensure
33
+ assert_nil ctx.destroy
34
+ end
35
+
36
+ def test_iothreads
37
+ ctx = ZMQ::Context.new
38
+ assert_raises TypeError do
39
+ ctx.iothreads = :invalid
40
+ end
41
+ ctx.iothreads = 2
42
+ assert_raises ZMQ::Error do
43
+ ctx.iothreads = -2
44
+ end
45
+ ensure
46
+ ctx.destroy
47
+ end
48
+
49
+ def test_linger
50
+ ctx = ZMQ::Context.new
51
+ assert_raises TypeError do
52
+ ctx.linger = :invalid
53
+ end
54
+ ctx.linger = 10
55
+ assert_raises ZMQ::Error do
56
+ ctx.linger = -2
57
+ end
58
+ ensure
59
+ ctx.destroy
60
+ end
61
+
62
+ def test_bind_connect
63
+ ctx = ZMQ::Context.new
64
+ rep = ctx.bind(:REP, "inproc://test.bind_connect")
65
+ req = ctx.connect(:REQ, "inproc://test.bind_connect")
66
+ req.send('success')
67
+ assert_equal 'success', rep.recv
68
+ ensure
69
+ ctx.destroy
70
+ end
71
+
72
+ def test_socket
73
+ ctx = ZMQ::Context.new
74
+ assert_raises TypeError do
75
+ ctx.socket("invalid")
76
+ end
77
+ socket = ctx.socket(ZMQ::REP)
78
+ assert_instance_of ZMQ::Socket::Rep, socket
79
+ assert_nil socket.close
80
+ socket = ctx.socket(:REP)
81
+ assert_instance_of ZMQ::Socket::Rep, socket
82
+ assert_nil socket.close
83
+ ensure
84
+ ctx.destroy
85
+ end
86
+ end
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestZmqFrame < ZmqTestCase
6
+ def test_alloc
7
+ frame = ZMQ::Frame("message")
8
+ assert_instance_of ZMQ::Frame, frame
9
+ assert_nil frame.destroy
10
+ end
11
+
12
+ def test_destroyed_frame
13
+ frame = ZMQ::Frame("message")
14
+ frame.destroy
15
+ assert_raises ZMQ::Error do
16
+ frame.data
17
+ end
18
+ end
19
+
20
+ def test_alloc_empty
21
+ frame = ZMQ::Frame.new
22
+ assert_equal 0, frame.size
23
+ frame.destroy
24
+ end
25
+
26
+ def test_size
27
+ assert_equal 7, ZMQ::Frame("message").size
28
+ end
29
+
30
+ def test_data
31
+ frame = ZMQ::Frame("message")
32
+ assert_equal "message", frame.data
33
+ assert_equal "message", frame.to_s
34
+ assert_equal "message", frame.to_str
35
+ end
36
+
37
+ def test_dup
38
+ frame = ZMQ::Frame("message")
39
+ dup_frame = frame.dup
40
+ assert_not_equal dup_frame.object_id, frame.object_id
41
+ assert_equal frame.data, dup_frame.data
42
+ end
43
+
44
+ def test_strhex
45
+ frame = ZMQ::Frame("message")
46
+ assert_equal "6D657373616765", frame.strhex
47
+ end
48
+
49
+ def test_data_equals
50
+ frame = ZMQ::Frame("message")
51
+ assert frame.data_equals?("message")
52
+ assert !frame.data_equals?("msg")
53
+ end
54
+
55
+ def test_equals
56
+ frame = ZMQ::Frame("message")
57
+ assert_equal frame, ZMQ::Frame("message")
58
+ assert_not_equal frame, ZMQ::Frame("msg")
59
+ end
60
+
61
+ def test_compare
62
+ frame = ZMQ::Frame("message")
63
+ assert frame > ZMQ::Frame("msg")
64
+ assert ZMQ::Frame("msg") < ZMQ::Frame("message")
65
+ end
66
+
67
+ def test_print
68
+ frame = ZMQ::Frame("message")
69
+ assert_nil frame.print
70
+ assert_nil frame.print("prefix")
71
+ end
72
+
73
+ def test_reset
74
+ frame = ZMQ::Frame("message")
75
+ frame.reset("msg")
76
+ assert_equal "msg", frame.data
77
+ end
78
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestZmqHandler < ZmqTestCase
6
+ def test_expects_pollitem
7
+ assert_raises TypeError do
8
+ ZMQ::Handler.new(:socket)
9
+ end
10
+ end
11
+
12
+ def test_readable_writable_contracts
13
+ handler = ZMQ::Handler.new(ZMQ::Pollitem(STDIN))
14
+ assert_raises NotImplementedError do
15
+ handler.on_readable
16
+ end
17
+ assert_raises NotImplementedError do
18
+ handler.on_writable
19
+ end
20
+ end
21
+
22
+ def test_error
23
+ handler = ZMQ::Handler.new(ZMQ::Pollitem(STDIN))
24
+ assert_raises StandardError do
25
+ handler.on_error(StandardError.new)
26
+ end
27
+ end
28
+ end
data/test/test_loop.rb ADDED
@@ -0,0 +1,252 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'helper')
4
+
5
+ class TestZmqLoop < ZmqTestCase
6
+ def test_alloc
7
+ lp = ZMQ::Loop.new
8
+ assert_instance_of ZMQ::Loop, lp
9
+ end
10
+
11
+ def test_destroyed
12
+ lp = ZMQ::Loop.new
13
+ lp.destroy
14
+ assert_raises ZMQ::Error do
15
+ lp.start
16
+ end
17
+ end
18
+
19
+ def test_run_and_stop
20
+ ctx = ZMQ::Context.new
21
+ ZMQ::Loop.run do
22
+ assert !ZL.running?
23
+ ZL.add_oneshot_timer(0.2) do
24
+ ZL.stop
25
+ end
26
+ ZL.add_oneshot_timer(0.1) do
27
+ assert ZL.running?
28
+ end
29
+ end
30
+ ensure
31
+ ctx.destroy
32
+ end
33
+
34
+ def test_callback_stops_event_loop
35
+ ctx = ZMQ::Context.new
36
+ ret = ZMQ::Loop.run do
37
+ ZL.add_oneshot_timer(0.2){ false }
38
+ end
39
+ assert_equal(-1, ret)
40
+ ensure
41
+ ctx.destroy
42
+ end
43
+
44
+ def test_callback_error_stops_event_loop
45
+ ctx = ZMQ::Context.new
46
+ assert_raises RuntimeError do
47
+ ret = ZMQ::Loop.run do
48
+ ZL.add_oneshot_timer(0.2){ raise("stop!") }
49
+ end
50
+ assert_equal(-1, ret)
51
+ end
52
+ ensure
53
+ ctx.destroy
54
+ end
55
+
56
+ def test_add_periodic_timer
57
+ ctx = ZMQ::Context.new
58
+ fired = 0
59
+ ZMQ::Loop.run do
60
+ ZL.add_periodic_timer(0.1) do
61
+ fired += 1
62
+ fired < 3
63
+ end
64
+ end
65
+ assert_equal 3, fired
66
+ ensure
67
+ ctx.destroy
68
+ end
69
+
70
+ def test_add_timer
71
+ ctx = ZMQ::Context.new
72
+ fired, timer = 0, nil
73
+ ZMQ::Loop.run do
74
+ timer = ZL.add_timer(0.1, 5) do
75
+ fired += 1
76
+ fired < 5
77
+ end
78
+ end
79
+ assert_instance_of ZMQ::Timer, timer
80
+ assert_equal 5, fired
81
+ ensure
82
+ ctx.destroy
83
+ end
84
+
85
+ def test_cancel_timer
86
+ ctx = ZMQ::Context.new
87
+ fired = 0
88
+ ZMQ::Loop.run do
89
+ timer = ZL.add_timer(0.1, 5) do
90
+ fired += 1
91
+ fired < 5
92
+ end
93
+ assert ZL.cancel_timer(timer)
94
+ ZL.add_oneshot_timer(0.1){ false }
95
+ end
96
+ assert_equal 0, fired
97
+ ensure
98
+ ctx.destroy
99
+ end
100
+
101
+ def test_cancel_timer_instance
102
+ ctx = ZMQ::Context.new
103
+ fired = 0
104
+ timer = ZMQ::Timer.new(0.1, 2){ fired += 1 }
105
+ ZMQ::Loop.run do
106
+ ZL.register_timer(timer)
107
+ ZL.add_oneshot_timer(0.3){ false }
108
+ timer.cancel
109
+ end
110
+ assert_equal 0, fired
111
+ ensure
112
+ ctx.destroy
113
+ end
114
+
115
+ def test_add_oneshot_timer
116
+ ctx = ZMQ::Context.new
117
+ ret = ZMQ::Loop.run do
118
+ ZL.add_oneshot_timer(0.2){ false }
119
+ end
120
+ assert_equal(-1, ret)
121
+ ensure
122
+ ctx.destroy
123
+ end
124
+
125
+ def test_run_in_thread
126
+ ctx = ZMQ::Context.new
127
+ t = Thread.new do
128
+ ZMQ::Loop.run do
129
+ ZL.add_oneshot_timer(0.2){ false }
130
+ end
131
+ :done
132
+ end
133
+ assert_equal :done, t.value
134
+ ensure
135
+ ctx.destroy
136
+ end
137
+
138
+ class LoopBreaker < ZMQ::Handler
139
+ def on_readable
140
+ p :on_readable
141
+ false
142
+ end
143
+
144
+ def on_writable
145
+ end
146
+ end
147
+
148
+ def test_register_sockets
149
+ ctx = ZMQ::Context.new
150
+ ret = ZMQ::Loop.run do
151
+ s1 = ctx.socket(:PAIR)
152
+ s2 = ctx.socket(:PAIR)
153
+ s3 = ctx.socket(:PAIR)
154
+ ZL.bind(s1, "inproc://test.loop-register", LoopBreaker)
155
+ ZL.connect(s2, "inproc://test.loop-register")
156
+ s2.send("message")
157
+ assert_raises ZMQ::Error do
158
+ ZL.register(s3)
159
+ end
160
+ end
161
+ assert_equal(-1, ret)
162
+ ensure
163
+ ctx.destroy
164
+ end
165
+
166
+ def test_register_ios
167
+ r, w = IO.pipe
168
+ ret = ZMQ::Loop.run do
169
+ ZL.register_readable(r, LoopBreaker)
170
+ ZL.register_writable(w)
171
+ w.write("message")
172
+ end
173
+ assert_equal(-1, ret)
174
+ end
175
+
176
+ def test_register_ruby_sockets
177
+ server = TCPServer.new("127.0.0.1", 0)
178
+ f, port, host, addr = server.addr
179
+ client = TCPSocket.new("127.0.0.1", port)
180
+ s = server.accept
181
+ ret = ZMQ::Loop.run do
182
+ ZL.register_readable(s, LoopBreaker)
183
+ ZL.register_writable(client)
184
+ client.send("message", 0)
185
+ end
186
+ assert_equal(-1, ret)
187
+ end
188
+
189
+ class FailHandler < ZMQ::Handler
190
+ def on_readable
191
+ p :on_readable
192
+ raise "fail"
193
+ end
194
+
195
+ def on_writable
196
+ end
197
+ end
198
+
199
+ def test_raise_from_socket_callback
200
+ ctx = ZMQ::Context.new
201
+ assert_raises RuntimeError do
202
+ ret = ZMQ::Loop.run do
203
+ s1 = ctx.socket(:PAIR)
204
+ s2 = ctx.socket(:PAIR)
205
+ ZL.bind(s1, "inproc://test.loop-raise_from_socket_callback", FailHandler)
206
+ ZL.connect(s2, "inproc://test.loop-raise_from_socket_callback")
207
+ s2.send("message")
208
+ end
209
+ assert_equal(-1, ret)
210
+ end
211
+ ensure
212
+ ctx.destroy
213
+ end
214
+
215
+ def test_raise_from_io_callback
216
+ assert_raises RuntimeError do
217
+ ret = ZMQ::Loop.run do
218
+ r, w = IO.pipe
219
+ ZL.register_readable(r, FailHandler)
220
+ ZL.register_writable(w)
221
+ w.write("message")
222
+ end
223
+ assert_equal(-1, ret)
224
+ end
225
+ end
226
+
227
+ def test_raise_on_invalid_handler
228
+ ctx = ZMQ::Context.new
229
+ assert_raises RuntimeError do
230
+ ret = ZMQ::Loop.run do
231
+ s1 = ctx.socket(:PAIR)
232
+ s2 = ctx.socket(:PAIR)
233
+ ZL.bind(s1, "inproc://test.loop-raise_on_invalid_handler", FailHandler)
234
+ ZL.connect(s2, "inproc://test.loop-raise_on_invalid_handler")
235
+ s2.send("message")
236
+ end
237
+ assert_equal(-1, ret)
238
+ end
239
+ ensure
240
+ ctx.destroy
241
+ end
242
+
243
+ def test_double_stop
244
+ ctx = ZMQ::Context.new
245
+ assert_raises ZMQ::Error do
246
+ lp = ZMQ::Loop.new
247
+ lp.stop
248
+ end
249
+ ensure
250
+ ctx.destroy
251
+ end
252
+ end