mailbox 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -0
- data/VERSION.yml +1 -1
- data/lib/mailbox.rb +13 -0
- data/mailbox.gemspec +2 -2
- data/test/mailbox_test.rb +83 -17
- metadata +3 -3
data/Rakefile
CHANGED
data/VERSION.yml
CHANGED
data/lib/mailbox.rb
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
# the <tt>:channel</tt> parameter on +mailslot+.
|
9
9
|
|
10
10
|
require 'rubygems'
|
11
|
+
require 'java'
|
11
12
|
require 'jretlang'
|
12
13
|
|
13
14
|
require File.dirname(__FILE__) + '/synchronized'
|
@@ -51,6 +52,10 @@ module Mailbox
|
|
51
52
|
|
52
53
|
end
|
53
54
|
|
55
|
+
def __queue_depth__
|
56
|
+
__queue_counter__.get
|
57
|
+
end
|
58
|
+
|
54
59
|
private
|
55
60
|
def self.included(base)
|
56
61
|
base.extend(Mailbox::ClassMethods)
|
@@ -84,6 +89,12 @@ module Mailbox
|
|
84
89
|
fiber
|
85
90
|
end
|
86
91
|
|
92
|
+
synchronized
|
93
|
+
def __queue_counter__
|
94
|
+
@__queue_counter__ ||= java.util.concurrent.atomic.AtomicInteger.new
|
95
|
+
end
|
96
|
+
|
97
|
+
synchronized
|
87
98
|
def __fiber__
|
88
99
|
@fiber ||= __started_fiber__
|
89
100
|
end
|
@@ -145,6 +156,7 @@ module Mailbox
|
|
145
156
|
define_method method_name do |*args|
|
146
157
|
|
147
158
|
self.send(@__verbose_target__, "enqueued #{method_name}") if defined? @__verbose_target__
|
159
|
+
__queue_counter__.get_and_increment
|
148
160
|
|
149
161
|
result = nil
|
150
162
|
latch = JRL::Concurrent::CountDownLatch.new(1) if replyable
|
@@ -152,6 +164,7 @@ module Mailbox
|
|
152
164
|
__fiber__.execute do
|
153
165
|
begin
|
154
166
|
self.send(@__verbose_target__, "dequeued #{method_name}") if defined? @__verbose_target__
|
167
|
+
__queue_counter__.get_and_decrement
|
155
168
|
result = self.send(:"__#{method_name}__", *args )
|
156
169
|
rescue Exception => ex
|
157
170
|
raise if exception_method.nil? || Mailbox.raise_exceptions_immediately
|
data/mailbox.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mailbox}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Joel Friedman", "Patrick Farley"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-04-19}
|
13
13
|
s.description = %q{Mailbox is a JRuby module that simplifies concurrency and is backed by JVM threads.}
|
14
14
|
s.email = %q{asher.friedman@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/test/mailbox_test.rb
CHANGED
@@ -1,8 +1,21 @@
|
|
1
1
|
require 'test_helper.rb'
|
2
2
|
|
3
|
+
module JRL::Concurrent
|
4
|
+
class Latch
|
5
|
+
def fail_after_sec
|
6
|
+
raise Exception.new("timed out waiting one second for latch countdown - remaining count: #{count}") unless await 1
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
3
11
|
class MailboxTest < Test::Unit::TestCase
|
4
12
|
JThread = java.lang.Thread
|
5
13
|
|
14
|
+
|
15
|
+
def jrl_latch count = 1
|
16
|
+
JRL::Concurrent::Latch.new count
|
17
|
+
end
|
18
|
+
|
6
19
|
def test_mailslot_causes_execution_on_separate_thread
|
7
20
|
klass = Class.new do
|
8
21
|
include Mailbox
|
@@ -15,13 +28,39 @@ class MailboxTest < Test::Unit::TestCase
|
|
15
28
|
end
|
16
29
|
|
17
30
|
thread_info = {}
|
18
|
-
latch =
|
31
|
+
latch = jrl_latch
|
19
32
|
klass.new.test_method(latch, thread_info)
|
20
33
|
|
21
|
-
|
34
|
+
latch.fail_after_sec
|
22
35
|
assert_not_equal JThread.current_thread.name, thread_info[:name]
|
23
36
|
end
|
24
37
|
|
38
|
+
def test_mailbox_supports_concurrent_mailslot_calls_on_new_mailbox_object
|
39
|
+
klass = Class.new do
|
40
|
+
include Mailbox
|
41
|
+
|
42
|
+
attr_reader :failed
|
43
|
+
def initialize; @items = []; @failed = nil; end
|
44
|
+
|
45
|
+
mailslot :exception => :fail_test
|
46
|
+
def one
|
47
|
+
raise Exception.new('items not empty!') unless @items.empty?
|
48
|
+
@items << 1
|
49
|
+
sleep 0.01
|
50
|
+
raise Exception.new("items not exactly [1]!") unless @items == [1]
|
51
|
+
@items.pop
|
52
|
+
raise Exception.new('items not empty') unless @items.empty?
|
53
|
+
end
|
54
|
+
|
55
|
+
def fail_test e; @failed = e; end
|
56
|
+
end
|
57
|
+
|
58
|
+
o = klass.new
|
59
|
+
10.times { Thread.new { o.one } }
|
60
|
+
|
61
|
+
assert_nil o.failed
|
62
|
+
end
|
63
|
+
|
25
64
|
def test_mailslot_supports_threadpool_based_fibers
|
26
65
|
klass = Class.new do
|
27
66
|
include Mailbox
|
@@ -36,10 +75,10 @@ class MailboxTest < Test::Unit::TestCase
|
|
36
75
|
end
|
37
76
|
|
38
77
|
thread_info = {}
|
39
|
-
latch =
|
78
|
+
latch = jrl_latch
|
40
79
|
klass.new.test_method(latch, thread_info)
|
41
80
|
|
42
|
-
|
81
|
+
latch.fail_after_sec
|
43
82
|
assert_not_equal JThread.current_thread.name, thread_info[:name]
|
44
83
|
end
|
45
84
|
|
@@ -64,10 +103,10 @@ class MailboxTest < Test::Unit::TestCase
|
|
64
103
|
end
|
65
104
|
end
|
66
105
|
|
67
|
-
latch =
|
106
|
+
latch = jrl_latch
|
68
107
|
clazz = klass.new(latch)
|
69
108
|
clazz.test_method
|
70
|
-
|
109
|
+
latch.fail_after_sec
|
71
110
|
assert_equal "test exception", clazz.ex.message
|
72
111
|
end
|
73
112
|
|
@@ -163,13 +202,13 @@ class MailboxTest < Test::Unit::TestCase
|
|
163
202
|
end
|
164
203
|
|
165
204
|
thread_info = {}
|
166
|
-
latch =
|
205
|
+
latch = jrl_latch
|
167
206
|
a_channel = JRL::Channel.new
|
168
207
|
|
169
208
|
klass.new(a_channel)
|
170
209
|
a_channel.publish :latch => latch, :thread_info => thread_info
|
171
210
|
|
172
|
-
|
211
|
+
latch.fail_after_sec
|
173
212
|
assert_not_equal JThread.current_thread.name, thread_info[:name]
|
174
213
|
end
|
175
214
|
|
@@ -189,7 +228,7 @@ class MailboxTest < Test::Unit::TestCase
|
|
189
228
|
end
|
190
229
|
|
191
230
|
thread_info = {}
|
192
|
-
latch =
|
231
|
+
latch = jrl_latch
|
193
232
|
request_channel = JRL::Channels::MemoryRequestChannel.new
|
194
233
|
|
195
234
|
klass.new(request_channel)
|
@@ -202,11 +241,11 @@ class MailboxTest < Test::Unit::TestCase
|
|
202
241
|
latch.count_down
|
203
242
|
end
|
204
243
|
|
205
|
-
|
244
|
+
latch.fail_after_sec
|
206
245
|
assert_equal "ya_rly!", response
|
207
246
|
end
|
208
247
|
|
209
|
-
def
|
248
|
+
def test_should_support_replyable_messages
|
210
249
|
|
211
250
|
klass = Class.new do
|
212
251
|
include Mailbox
|
@@ -254,9 +293,35 @@ class MailboxTest < Test::Unit::TestCase
|
|
254
293
|
|
255
294
|
end
|
256
295
|
|
257
|
-
def
|
296
|
+
def test_should_maintain_queue_depth
|
297
|
+
klass = Class.new do
|
298
|
+
include Mailbox
|
299
|
+
include Test::Unit::Assertions
|
300
|
+
|
301
|
+
def initialize latch
|
302
|
+
@latch = latch
|
303
|
+
__fiber__.execute do
|
304
|
+
latch.fail_after_sec
|
305
|
+
end
|
306
|
+
end
|
258
307
|
|
308
|
+
mailslot
|
309
|
+
def test_method latch
|
310
|
+
latch.count_down
|
311
|
+
end
|
312
|
+
end
|
259
313
|
|
314
|
+
hold_latch = jrl_latch
|
315
|
+
agent = klass.new hold_latch
|
316
|
+
method_latch = jrl_latch 10
|
317
|
+
10.times { agent.test_method method_latch }
|
318
|
+
assert_equal 10, agent.__queue_depth__
|
319
|
+
hold_latch.count_down
|
320
|
+
method_latch.fail_after_sec
|
321
|
+
assert_equal 0, agent.__queue_depth__
|
322
|
+
end
|
323
|
+
|
324
|
+
def test_should_expose_hooks_to_message_enqueue_and_dequeue
|
260
325
|
klass = Class.new do
|
261
326
|
include Mailbox
|
262
327
|
attr_accessor :msg_info
|
@@ -279,13 +344,13 @@ class MailboxTest < Test::Unit::TestCase
|
|
279
344
|
|
280
345
|
end
|
281
346
|
|
282
|
-
latch =
|
347
|
+
latch = jrl_latch 2
|
283
348
|
|
284
349
|
test_agent = klass.new latch
|
285
350
|
test_agent.test_method
|
286
351
|
test_agent.test_method
|
287
352
|
|
288
|
-
|
353
|
+
latch.fail_after_sec
|
289
354
|
|
290
355
|
expected = ['enqueued test_method', 'dequeued test_method', 'enqueued test_method', 'dequeued test_method']
|
291
356
|
assert_equal expected, test_agent.msg_info
|
@@ -301,13 +366,14 @@ class MailboxTest < Test::Unit::TestCase
|
|
301
366
|
end
|
302
367
|
|
303
368
|
test_agent = klass.new
|
304
|
-
latch =
|
369
|
+
latch = jrl_latch
|
305
370
|
test_agent.count_down latch
|
306
|
-
|
371
|
+
|
372
|
+
latch.fail_after_sec
|
307
373
|
|
308
374
|
test_agent.dispose
|
309
375
|
|
310
|
-
latch =
|
376
|
+
latch = jrl_latch
|
311
377
|
test_agent.count_down latch
|
312
378
|
assert !latch.await(1), "latch didn't time out after fiber was disposed"
|
313
379
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 6
|
9
|
+
version: 0.2.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joel Friedman
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-04-19 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|