futuroscope 0.0.10 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60ea73929c330082898296c2512037998d2b9ad3
4
- data.tar.gz: 8886af488ab4d89dedac2e96488bd89f3e5bfd3c
3
+ metadata.gz: a1442fb21074e3f1f22aedd46da1880d581436dd
4
+ data.tar.gz: 47f91c0266f4d75c1c7dafe868deb522f7a60d2a
5
5
  SHA512:
6
- metadata.gz: fdcd4ef68d0fd26892f76e7f0f6ea0ac451569beac1eff44d697205f82837d5b642a25c4b26fbdea4fbc6565fb181d5bf32966e206482559c3623f8346868b9c
7
- data.tar.gz: dda945e88fc5f37816ab99a8882cc042c24c92e709e613a7b51957d8341e14a9fca65cc43a18ab278cc3c4765ceedf48386e192d5bfae7d4cf7e87142a83772f
6
+ metadata.gz: 64bf9683083deac9fdc9836f4278b8537420cf96cf947720f478708db5dcb6cddac58a33d9602d9dd03e4eb3cf6ce9622ea113e0ef6d1f9c615dc412bf9b57ca
7
+ data.tar.gz: e5ce587bee8e52b1a306bc1f8292ccec39c06e2f8df60264652e0c64b4612b02ddf703b621021fe53d2c033fd43eb9cd2363a01861e63868b2a71211e1a89c87
@@ -23,20 +23,17 @@ module Futuroscope
23
23
  #
24
24
  # Returns a Future
25
25
  def initialize(pool = Futuroscope.default_pool, &block)
26
- @mutex = Mutex.new
27
- @queue = Queue.new
26
+ @queue = SizedQueue.new(1)
28
27
  @pool = pool
29
28
  @block = block
30
29
  @pool.queue self
31
30
  end
32
31
 
32
+ # Semipublic: Forces this future to be run.
33
33
  def run_future
34
- begin
35
- @future_value = @block.call
36
- rescue Exception => e
37
- @exception = e
38
- end
39
- @queue.push :ok
34
+ @queue.push(:value => @block.call)
35
+ rescue Exception => e
36
+ @queue.push(:exception => e)
40
37
  end
41
38
 
42
39
  # Semipublic: Returns the future's value. Will wait for the future to be
@@ -44,12 +41,10 @@ module Futuroscope
44
41
  #
45
42
  # Returns the Future's block execution result.
46
43
  def future_value
47
- @mutex.synchronize do
48
- begin
49
- raise @exception if @exception
50
- return @future_value if defined?(@future_value)
51
- end while @queue.pop
52
- end
44
+ resolved = resolved_future_value
45
+
46
+ raise resolved[:exception] if resolved[:exception]
47
+ resolved[:value]
53
48
  end
54
49
 
55
50
  def_delegators :future_value,
@@ -58,6 +53,10 @@ module Futuroscope
58
53
 
59
54
  private
60
55
 
56
+ def resolved_future_value
57
+ @resolved_future ||= @queue.pop
58
+ end
59
+
61
60
  def method_missing(method, *args)
62
61
  future_value.send(method, *args)
63
62
  end
@@ -68,7 +68,12 @@ module Futuroscope
68
68
  end
69
69
 
70
70
  def can_spin_extra_workers?
71
- @workers.length < @max_workers
71
+ @workers.length < @max_workers && span_chance
72
+ end
73
+
74
+ def span_chance
75
+ values = [true, false]
76
+ values.respond_to?(:sample) ? values.sample : values.choice
72
77
  end
73
78
 
74
79
  def more_workers_than_needed?
@@ -1,3 +1,3 @@
1
1
  module Futuroscope
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -19,13 +19,19 @@ module Futuroscope
19
19
  while(future = @pool.pop) do
20
20
  future.run_future
21
21
  end
22
- stop
22
+ die
23
23
  end
24
24
  end
25
25
 
26
+ # Public: Stops this worker.
27
+ def stop
28
+ @thread.kill
29
+ die
30
+ end
31
+
26
32
  private
27
33
 
28
- def stop
34
+ def die
29
35
  @pool.worker_died(self)
30
36
  end
31
37
  end
@@ -25,6 +25,7 @@ module Futuroscope
25
25
  describe "worker control" do
26
26
  it "adds more workers when needed and returns to the default amount" do
27
27
  pool = Pool.new(2, 8)
28
+ pool.stub(:span_chance).and_return true
28
29
  10.times do |future|
29
30
  Future.new(pool){ sleep(1) }
30
31
  end
@@ -44,6 +45,7 @@ module Futuroscope
44
45
 
45
46
  it "allows overriding max workers real time" do
46
47
  pool = Pool.new(2, 8)
48
+ pool.stub(:span_chance).and_return true
47
49
  pool.max_workers = 4
48
50
 
49
51
  10.times do |future|
@@ -54,5 +56,24 @@ module Futuroscope
54
56
  expect(pool.workers).to have(4).workers
55
57
  end
56
58
  end
59
+
60
+ describe "#finalize" do
61
+ it "shuts down all its workers" do
62
+ pool = Pool.new(2, 8)
63
+
64
+ pool.send(:finalize)
65
+
66
+ expect(pool.workers).to have(0).workers
67
+ end
68
+ end
69
+
70
+ describe "#span_chance" do
71
+ it "returns true or false randomly" do
72
+ pool = Pool.new
73
+ chance = pool.send(:span_chance)
74
+
75
+ expect([true, false]).to include(chance)
76
+ end
77
+ end
57
78
  end
58
79
  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.0.10
4
+ version: 0.0.12
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: 2013-05-05 00:00:00.000000000 Z
11
+ date: 2013-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.0.2
122
+ rubygems_version: 2.0.3
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Futuroscope is yet another simple gem that implements the Futures concurrency