myna_eventmachine 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +0 -1
- data/lib/myna/future.rb +6 -7
- data/lib/myna/version.rb +1 -1
- data/test/test_future.rb +17 -3
- metadata +1 -1
data/README.md
CHANGED
data/lib/myna/future.rb
CHANGED
@@ -27,9 +27,9 @@ require 'thread'
|
|
27
27
|
|
28
28
|
module Future
|
29
29
|
|
30
|
-
def Future.run
|
30
|
+
def Future.run(&block)
|
31
31
|
future = Future.new
|
32
|
-
future.run(
|
32
|
+
future.run(block)
|
33
33
|
future
|
34
34
|
end
|
35
35
|
|
@@ -68,9 +68,10 @@ module Future
|
|
68
68
|
self.set(value, :failure)
|
69
69
|
end
|
70
70
|
|
71
|
-
def run
|
71
|
+
def run(proc)
|
72
72
|
begin
|
73
|
-
|
73
|
+
value = proc.call()
|
74
|
+
self.succeed(value)
|
74
75
|
rescue => exn
|
75
76
|
self.fail(exn)
|
76
77
|
end
|
@@ -120,9 +121,7 @@ module Future
|
|
120
121
|
def map(&block)
|
121
122
|
future = Future.new
|
122
123
|
self.on_complete do |value|
|
123
|
-
future.run
|
124
|
-
block.call(value)
|
125
|
-
end
|
124
|
+
future.run(proc { block.call(value) })
|
126
125
|
end
|
127
126
|
future
|
128
127
|
end
|
data/lib/myna/version.rb
CHANGED
data/test/test_future.rb
CHANGED
@@ -54,9 +54,23 @@ describe Future do
|
|
54
54
|
|
55
55
|
describe "Future.fail" do
|
56
56
|
it "must cause an exception to be raised on get" do
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
exn = Exception.new("A test")
|
58
|
+
f = Future::Future.new
|
59
|
+
f.fail(exn)
|
60
|
+
|
61
|
+
proc { f.get }.must_raise Exception
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "Future.run" do
|
66
|
+
it "must complete the future with the expected value" do
|
67
|
+
f = Future.run do 1 + 1 end
|
68
|
+
f.get.must_equal 2
|
69
|
+
end
|
70
|
+
|
71
|
+
it "must catch exceptions and complete the future in a failed state" do
|
72
|
+
f = Future.run do 1/0 end
|
73
|
+
proc { f.get }.must_raise ZeroDivisionError
|
60
74
|
end
|
61
75
|
end
|
62
76
|
end
|