futuroscope 0.0.12 → 0.1.0

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: a1442fb21074e3f1f22aedd46da1880d581436dd
4
- data.tar.gz: 47f91c0266f4d75c1c7dafe868deb522f7a60d2a
3
+ metadata.gz: a2e406444d572ba1879ab2fb7963b81d27710dc5
4
+ data.tar.gz: dc9f7d86d65224ae8b48da33930a9439c814c272
5
5
  SHA512:
6
- metadata.gz: 64bf9683083deac9fdc9836f4278b8537420cf96cf947720f478708db5dcb6cddac58a33d9602d9dd03e4eb3cf6ce9622ea113e0ef6d1f9c615dc412bf9b57ca
7
- data.tar.gz: e5ce587bee8e52b1a306bc1f8292ccec39c06e2f8df60264652e0c64b4612b02ddf703b621021fe53d2c033fd43eb9cd2363a01861e63868b2a71211e1a89c87
6
+ metadata.gz: 3c13e918f2e3fbefd5ba7af31febc58efe08324ee4e75d430a558fcf90b936621a83faded2af549945c0d8e83d8febc83c6480290b6f2770754156dc73ca31b5
7
+ data.tar.gz: 327fdd0b92c35ac66b332af2b8ce7e7d25c13d01810213228b676dd10313480b38e072931a1c07c31be5260c0b476ff799f30b989daa25bdaa9e4e258858c511
data/.travis.yml CHANGED
@@ -1,9 +1,5 @@
1
1
  rvm:
2
- - 1.8.7
3
2
  - 1.9.3
4
3
  - 2.0.0
5
- - rbx-18mode
6
4
  - rbx-19mode
7
- - jruby-18mode
8
5
  - jruby-19mode
9
- - ree
data/README.md CHANGED
@@ -23,7 +23,7 @@ method on in it will be forwarded to the block's return value.
23
23
  If the thread didn't finish yet, it will block the program's execution until
24
24
  it's finished. Otherwise, it will immediataly return its value.
25
25
 
26
- Futuroscope is tested on `MRI 1.9.3`, `MRI 2.0.0`, `MRI 1.8.7`, `JRuby in 1.9 mode`, `JRuby in 1.8 mode`, `Rubinius 1.9 mode`, `Rubinius 1.8 mode` and `REE`.
26
+ Futuroscope is tested on `MRI 1.9.3`, `MRI 2.0.0`, `Rubinius (1.9)` and `JRuby (1.9)`.
27
27
 
28
28
  ## Installation
29
29
 
@@ -148,7 +148,7 @@ Futuroscope.default_pool.max_workers = 16
148
148
  Also, each future can be scheduled to a different pool like this:
149
149
 
150
150
  ```Ruby
151
- pool = Futuroscope::Pool.new(16, 32)
151
+ pool = Futuroscope::Pool.new(16..32)
152
152
 
153
153
  future = Future.new(pool){ :edballs }
154
154
 
@@ -1,11 +1,13 @@
1
+ require 'thread'
2
+
1
3
  module Futuroscope
2
4
  # A Future is an object that gets initialized with a block and will behave
3
5
  # exactly like the block's result, but being able to "borrow" its result from
4
6
  # the future. That is, will block when the result is not ready until it is,
5
7
  # and will return it instantly if the thread's execution already finished.
6
8
  #
7
- class Future
8
- extend Forwardable
9
+ class Future < BasicObject
10
+ extend ::Forwardable
9
11
 
10
12
  # Initializes a future with a block and starts its execution.
11
13
  #
@@ -22,8 +24,8 @@ module Futuroscope
22
24
  # block - A block that will be run in the background.
23
25
  #
24
26
  # Returns a Future
25
- def initialize(pool = Futuroscope.default_pool, &block)
26
- @queue = SizedQueue.new(1)
27
+ def initialize(pool = ::Futuroscope.default_pool, &block)
28
+ @queue = ::SizedQueue.new(1)
27
29
  @pool = pool
28
30
  @block = block
29
31
  @pool.queue self
@@ -31,9 +33,9 @@ module Futuroscope
31
33
 
32
34
  # Semipublic: Forces this future to be run.
33
35
  def run_future
34
- @queue.push(:value => @block.call)
35
- rescue Exception => e
36
- @queue.push(:exception => e)
36
+ @queue.push(value: @block.call)
37
+ rescue ::Exception => e
38
+ @queue.push(exception: e)
37
39
  end
38
40
 
39
41
  # Semipublic: Returns the future's value. Will wait for the future to be
@@ -47,9 +49,7 @@ module Futuroscope
47
49
  resolved[:value]
48
50
  end
49
51
 
50
- def_delegators :future_value,
51
- :to_s, :==, :kind_of?, :is_a?, :clone, :class, :inspect, :tap, :to_enum,
52
- :display, :eql?, :hash, :methods, :nil?
52
+ def_delegators :future_value, :!, :!=, :==, :equal?
53
53
 
54
54
  private
55
55
 
@@ -12,9 +12,9 @@ module Futuroscope
12
12
  # Public: Initializes a new Pool.
13
13
  #
14
14
  # thread_count - The number of workers that this pool is gonna have
15
- def initialize(min_workers = 8, max_workers = 16)
16
- @min_workers = min_workers
17
- @max_workers = max_workers
15
+ def initialize(range = 8..16)
16
+ @min_workers = range.min
17
+ @max_workers = range.max
18
18
  @queue = Queue.new
19
19
  @workers = Set.new
20
20
  @mutex = Mutex.new
@@ -72,8 +72,7 @@ module Futuroscope
72
72
  end
73
73
 
74
74
  def span_chance
75
- values = [true, false]
76
- values.respond_to?(:sample) ? values.sample : values.choice
75
+ [true, false].sample
77
76
  end
78
77
 
79
78
  def more_workers_than_needed?
@@ -1,3 +1,3 @@
1
1
  module Futuroscope
2
- VERSION = "0.0.12"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -4,10 +4,10 @@ require 'futuroscope/pool'
4
4
  module Futuroscope
5
5
  describe Pool do
6
6
  it "spins up a number of workers" do
7
- pool = Pool.new(2)
7
+ pool = Pool.new(2..4)
8
8
  expect(pool.workers).to have(2).workers
9
9
 
10
- pool = Pool.new(3)
10
+ pool = Pool.new(3..4)
11
11
  expect(pool.workers).to have(3).workers
12
12
  end
13
13
 
@@ -24,7 +24,7 @@ module Futuroscope
24
24
 
25
25
  describe "worker control" do
26
26
  it "adds more workers when needed and returns to the default amount" do
27
- pool = Pool.new(2, 8)
27
+ pool = Pool.new(2..8)
28
28
  pool.stub(:span_chance).and_return true
29
29
  10.times do |future|
30
30
  Future.new(pool){ sleep(1) }
@@ -38,13 +38,13 @@ module Futuroscope
38
38
  end
39
39
 
40
40
  it "allows overriding min workers real time" do
41
- pool = Pool.new(2, 8)
41
+ pool = Pool.new(2..8)
42
42
  pool.min_workers = 3
43
43
  expect(pool.workers).to have(3).workers
44
44
  end
45
45
 
46
46
  it "allows overriding max workers real time" do
47
- pool = Pool.new(2, 8)
47
+ pool = Pool.new(2..8)
48
48
  pool.stub(:span_chance).and_return true
49
49
  pool.max_workers = 4
50
50
 
@@ -59,10 +59,10 @@ module Futuroscope
59
59
 
60
60
  describe "#finalize" do
61
61
  it "shuts down all its workers" do
62
- pool = Pool.new(2, 8)
62
+ pool = Pool.new(2..8)
63
63
 
64
64
  pool.send(:finalize)
65
-
65
+
66
66
  expect(pool.workers).to have(0).workers
67
67
  end
68
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: futuroscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josep Jaume Rey Peroy