celluloid-promise 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/celluloid-promise/q.rb +15 -15
- data/lib/celluloid-promise/version.rb +1 -1
- data/spec/promise_spec.rb +23 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Y2NhOWI2MjI1NTU4ZWZhZDQ2ZTgwOTdmNWRjNzUxMDUwODgyY2ExNQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmJiYjA3MTY3ZGY3MDNmNjJiOTQ5NjNlMjIyMTcwYjg5N2QzN2NkZA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ODFmYzJhOTg1M2Y5ZTViNjhlNTExYTQ5YjhmNmM4ZWFmNTU5YTNhNDA0ZmYz
|
10
|
+
ZDQzOTMwMWQ1NzJhOTIyYmRlMzQ2NjAxMzQyMDc5Njc2ODk1N2ZjMWJkZDc2
|
11
|
+
N2Y5MjI4ODkzOTkwZDY3YmNiN2ZhOTFhYzc1OTYxOGU2M2ZiMDg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
N2Q4NDFmYTcwMWUxZjAwZTgzMjJiMDhmOWExYTg3YzkwMmJiY2ZhYWExZWVh
|
14
|
+
MWIxOTBmZmI5MGZkNGM4ZGI1NjU0MWNjOGFlOWI2NWU0NTUyMzE2ZDk5YTgy
|
15
|
+
NTQzZTUyMDEyM2ExZmVlZWUwNTgwYjFhYjhmYzZmNjNkNzhmZGQ=
|
data/lib/celluloid-promise/q.rb
CHANGED
@@ -8,7 +8,7 @@ module Celluloid
|
|
8
8
|
class Reactor
|
9
9
|
include ::Celluloid
|
10
10
|
|
11
|
-
def
|
11
|
+
def perform
|
12
12
|
yield if block_given?
|
13
13
|
end
|
14
14
|
end
|
@@ -44,7 +44,7 @@ module Celluloid
|
|
44
44
|
# @param [Proc, Proc, &blk] callbacks success, error, success_block
|
45
45
|
# @return [Promise] Returns an unresolved promise for chaining
|
46
46
|
def then(callback = nil, errback = nil, &blk)
|
47
|
-
result = Q.defer
|
47
|
+
result = ::Celluloid::Actor[:Q].defer(@reactor)
|
48
48
|
|
49
49
|
callback ||= blk
|
50
50
|
|
@@ -78,7 +78,7 @@ module Celluloid
|
|
78
78
|
# Schedule as we are touching shared state
|
79
79
|
# Everything else is locally scoped
|
80
80
|
#
|
81
|
-
@reactor.async.
|
81
|
+
@reactor.async.perform do
|
82
82
|
pending_array = pending
|
83
83
|
|
84
84
|
if pending_array.nil?
|
@@ -118,11 +118,11 @@ module Celluloid
|
|
118
118
|
end
|
119
119
|
|
120
120
|
def then(callback = nil, errback = nil, &blk)
|
121
|
-
result = Q.defer
|
121
|
+
result = ::Celluloid::Actor[:Q].defer(@reactor)
|
122
122
|
|
123
123
|
callback ||= blk
|
124
124
|
|
125
|
-
@reactor.async.
|
125
|
+
@reactor.async.perform do
|
126
126
|
if @error
|
127
127
|
if errback.nil?
|
128
128
|
result.resolve(ResolvedPromise.new(@reactor, @response, true))
|
@@ -164,7 +164,7 @@ module Celluloid
|
|
164
164
|
#
|
165
165
|
# @param [Object] val constant, message or an object representing the result.
|
166
166
|
def resolve(val = nil)
|
167
|
-
@reactor.async.
|
167
|
+
@reactor.async.perform do
|
168
168
|
if not @pending.nil?
|
169
169
|
callbacks = @pending
|
170
170
|
@pending = nil
|
@@ -223,8 +223,8 @@ module Celluloid
|
|
223
223
|
# Creates a Deferred object which represents a task which will finish in the future.
|
224
224
|
#
|
225
225
|
# @return [Deferred] Returns a new instance of Deferred
|
226
|
-
def defer
|
227
|
-
return Deferred.new(next_reactor)
|
226
|
+
def defer(reactor = nil)
|
227
|
+
return Deferred.new(next_reactor(reactor))
|
228
228
|
end
|
229
229
|
|
230
230
|
|
@@ -316,13 +316,13 @@ module Celluloid
|
|
316
316
|
|
317
317
|
#
|
318
318
|
# Promises are placed on reactor threads in a round robin
|
319
|
-
# I would have used pool however
|
320
|
-
# multiple concurrent promise chains on each reactor
|
319
|
+
# I would have used pool however pool blocks to completion and doesn't
|
320
|
+
# allows us to run multiple concurrent promise chains on each reactor.
|
321
321
|
#
|
322
|
-
def next_reactor
|
322
|
+
def next_reactor(current = nil)
|
323
323
|
@current = @current >= (@reactors.length - 1) ? 0 : @current + 1
|
324
324
|
selected = @reactors[@current]
|
325
|
-
selected !=
|
325
|
+
selected != current ? selected : next_reactor(current)
|
326
326
|
end
|
327
327
|
end
|
328
328
|
end
|
@@ -334,15 +334,15 @@ module Celluloid
|
|
334
334
|
Promise::Coordinator.supervise_as :Q
|
335
335
|
module Q
|
336
336
|
def self.defer
|
337
|
-
Actor[:Q].defer
|
337
|
+
::Celluloid::Actor[:Q].defer
|
338
338
|
end
|
339
339
|
|
340
340
|
def self.reject(reason = nil)
|
341
|
-
Actor[:Q].reject(reason)
|
341
|
+
::Celluloid::Actor[:Q].reject(reason)
|
342
342
|
end
|
343
343
|
|
344
344
|
def self.all(*promises)
|
345
|
-
Actor[:Q].all(*promises)
|
345
|
+
::Celluloid::Actor[:Q].all(*promises)
|
346
346
|
end
|
347
347
|
end
|
348
348
|
end
|
data/spec/promise_spec.rb
CHANGED
@@ -17,10 +17,9 @@ describe Celluloid::Q do
|
|
17
17
|
@promise = @deferred.promise
|
18
18
|
@log = []
|
19
19
|
@finish = proc {
|
20
|
-
while(
|
21
|
-
@
|
22
|
-
|
23
|
-
}
|
20
|
+
while(!@mutex.try_lock); end
|
21
|
+
@resource.signal
|
22
|
+
@mutex.unlock
|
24
23
|
}
|
25
24
|
@default_fail = proc { |reason|
|
26
25
|
fail(reason)
|
@@ -127,7 +126,7 @@ describe Celluloid::Q do
|
|
127
126
|
it "can modify the result of a promise before returning" do
|
128
127
|
@mutex.synchronize {
|
129
128
|
proc { |name|
|
130
|
-
@defer.async.
|
129
|
+
@defer.async.perform {
|
131
130
|
@deferred.resolve("Hello #{name}")
|
132
131
|
}
|
133
132
|
@promise.then(proc {|result|
|
@@ -262,9 +261,9 @@ describe Celluloid::Q do
|
|
262
261
|
@finish.call
|
263
262
|
}, @default_fail)
|
264
263
|
|
265
|
-
@defer.async.
|
266
|
-
@defer.async.
|
267
|
-
@defer.async.
|
264
|
+
@defer.async.perform { @deferred.resolve(:foo) }
|
265
|
+
@defer.async.perform { deferred2.resolve(:baz) }
|
266
|
+
@defer.async.perform { deferred1.resolve(:bar) }
|
268
267
|
|
269
268
|
@resource.wait(@mutex)
|
270
269
|
}
|
@@ -281,8 +280,8 @@ describe Celluloid::Q do
|
|
281
280
|
@finish.call
|
282
281
|
})
|
283
282
|
|
284
|
-
@defer.async.
|
285
|
-
@defer.async.
|
283
|
+
@defer.async.perform { @deferred.resolve(:foo) }
|
284
|
+
@defer.async.perform { deferred2.reject(:baz) }
|
286
285
|
|
287
286
|
@resource.wait(@mutex)
|
288
287
|
}
|
@@ -369,19 +368,19 @@ describe Celluloid::Q do
|
|
369
368
|
@mutex.synchronize {
|
370
369
|
@promise.then(proc {|result|
|
371
370
|
@log << result
|
372
|
-
:
|
371
|
+
:alt
|
373
372
|
}, @default_fail)
|
374
373
|
@promise.then(proc {|result|
|
375
374
|
@log << result
|
376
|
-
|
375
|
+
nil
|
377
376
|
}, @default_fail)
|
378
377
|
@promise.then(proc {|result|
|
379
378
|
@log << result
|
380
|
-
|
379
|
+
Celluloid::Q.reject('some reason')
|
381
380
|
}, @default_fail)
|
382
381
|
@promise.then(proc {|result|
|
383
382
|
@log << result
|
384
|
-
|
383
|
+
raise 'some error'
|
385
384
|
}, @default_fail)
|
386
385
|
@promise.then(proc {
|
387
386
|
@finish.call
|
@@ -399,19 +398,19 @@ describe Celluloid::Q do
|
|
399
398
|
@mutex.synchronize {
|
400
399
|
@promise.then(@default_fail, proc {|result|
|
401
400
|
@log << result
|
402
|
-
:
|
401
|
+
:alt
|
403
402
|
})
|
404
403
|
@promise.then(@default_fail, proc {|result|
|
405
404
|
@log << result
|
406
|
-
|
405
|
+
nil
|
407
406
|
})
|
408
407
|
@promise.then(@default_fail, proc {|result|
|
409
408
|
@log << result
|
410
|
-
|
409
|
+
Celluloid::Q.reject('some reason')
|
411
410
|
})
|
412
411
|
@promise.then(@default_fail, proc {|result|
|
413
412
|
@log << result
|
414
|
-
|
413
|
+
raise 'some error'
|
415
414
|
})
|
416
415
|
@promise.then(@default_fail, proc {|result|
|
417
416
|
@finish.call
|
@@ -470,8 +469,8 @@ describe Celluloid::Q do
|
|
470
469
|
class BlockingActor
|
471
470
|
include ::Celluloid
|
472
471
|
|
473
|
-
def
|
474
|
-
sleep (10
|
472
|
+
def delayed_response
|
473
|
+
sleep (5..10).to_a.sample
|
475
474
|
end
|
476
475
|
end
|
477
476
|
|
@@ -486,7 +485,7 @@ describe Celluloid::Q do
|
|
486
485
|
deferred_store = []
|
487
486
|
|
488
487
|
args = [proc {|val|
|
489
|
-
actor.
|
488
|
+
actor.delayed_response
|
490
489
|
count.update {|v| v + 1}
|
491
490
|
}, @default_fail]
|
492
491
|
|
@@ -506,6 +505,9 @@ describe Celluloid::Q do
|
|
506
505
|
|
507
506
|
@deferred.resolve()
|
508
507
|
@resource.wait(@mutex)
|
508
|
+
|
509
|
+
actor.terminate
|
510
|
+
::Celluloid::Actor.kill(actor)
|
509
511
|
}
|
510
512
|
end
|
511
513
|
|