futuroscope 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +8 -4
- data/lib/futuroscope/future.rb +4 -1
- data/lib/futuroscope/version.rb +1 -1
- data/spec/futuroscope/future_spec.rb +27 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fd5d28ef251a17a7ef93724aeb9bd633c7918e5
|
4
|
+
data.tar.gz: b99fa961184a443c7fcc68c3e547536bfa8295c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 216febe1e1ef8ea9041cbdb053dd2d5aa926e12a30056bff728e48947931436e59160c9d1ca50bec0cafc0a4579f9e804b8887cba6270d324c0ac595370691ae
|
7
|
+
data.tar.gz: 702c1d669a4b74cc126aacac0cbf3841651737588b8c6068ae6f12d1aa01536698933856c80825ae33112881824e75fa923846c84b973c9b905710064bd885be
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -6,10 +6,14 @@
|
|
6
6
|
[![Dependency Status](https://gemnasium.com/codegram/futuroscope.png)](https://gemnasium.com/codegram/futuroscope)
|
7
7
|
|
8
8
|
Futursocope is a simple library that implements futures in ruby. Futures are a
|
9
|
-
concurrency pattern meant to help you deal with
|
9
|
+
concurrency pattern meant to help you deal with threads in a simple, transparent way.
|
10
10
|
|
11
|
-
It's specially useful
|
12
|
-
|
11
|
+
It's specially useful in situations where you have calls to an expensive resource that
|
12
|
+
could be done in parallel (they are not chained), but you don't wanna deal with low-level
|
13
|
+
threads. HTTP calls are a good example.
|
14
|
+
|
15
|
+
Also useful when you want to spin up a process that runs in the background, do some stuff
|
16
|
+
in the middle, and wait for that process to return.
|
13
17
|
|
14
18
|
[![The awesome Futuroscope park](http://europe.eurostar.com/wp-content/uploads/2011/06/Futuroscope10-59-of-107.jpg)](http://futuroscope.com)
|
15
19
|
|
@@ -24,7 +28,7 @@ method on in it will be forwarded to the block's return value.
|
|
24
28
|
If the thread didn't finish yet, it will block the program's execution until
|
25
29
|
it's finished. Otherwise, it will immediately return its value.
|
26
30
|
|
27
|
-
Futuroscope is tested on `MRI 1.9.3`, `MRI 2.0.0`, `MRI 2.1.0`, `Rubinius (
|
31
|
+
Futuroscope is tested on `MRI 1.9.3`, `MRI 2.0.0`, `MRI 2.1.0`, `Rubinius (2.2.3)` and `JRuby (1.9)`.
|
28
32
|
|
29
33
|
Check out [futuroscope's post on Codegram's blog](http://blog.codegram.com/2013/5/new-gem-released-futuroscope) to get started.
|
30
34
|
|
data/lib/futuroscope/future.rb
CHANGED
@@ -30,6 +30,7 @@ module Futuroscope
|
|
30
30
|
@queue = ::SizedQueue.new(1)
|
31
31
|
@pool = pool
|
32
32
|
@block = block
|
33
|
+
@mutex = Mutex.new
|
33
34
|
@pool.queue self
|
34
35
|
end
|
35
36
|
|
@@ -70,7 +71,9 @@ module Futuroscope
|
|
70
71
|
private
|
71
72
|
|
72
73
|
def resolved_future_value
|
73
|
-
@resolved_future
|
74
|
+
@resolved_future || @mutex.synchronize do
|
75
|
+
@resolved_future ||= @queue.pop
|
76
|
+
end
|
74
77
|
end
|
75
78
|
end
|
76
79
|
end
|
data/lib/futuroscope/version.rb
CHANGED
@@ -66,5 +66,32 @@ module Futuroscope
|
|
66
66
|
expect(future.dup).to eq future
|
67
67
|
end
|
68
68
|
|
69
|
+
context "when at least another thread is alive" do
|
70
|
+
# If no threads are alive, the VM raises an exception, therefore we need to ensure there is one.
|
71
|
+
|
72
|
+
before :each do
|
73
|
+
@live_thread = Thread.new { loop { } }
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
it "doesn't hang when 2 threads try to obtain its result before it's finished" do
|
78
|
+
test_thread = Thread.new do
|
79
|
+
future = Future.new { sleep 1; 1 }
|
80
|
+
f1 = Future.new { future + 1 }
|
81
|
+
f2 = Future.new { future + 2 }
|
82
|
+
f1.future_value
|
83
|
+
f2.future_value
|
84
|
+
end
|
85
|
+
sleep 2
|
86
|
+
expect(test_thread).to_not be_alive
|
87
|
+
test_thread.kill
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
after :each do
|
92
|
+
@live_thread.kill
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
69
96
|
end
|
70
97
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: futuroscope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josep Jaume Rey Peroy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|