future_proof 0.0.3 → 0.0.4

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: 15ccb48d47e099121a7d5f7244f78df5e3a9ac5a
4
- data.tar.gz: a54c4d62b82764b907c03fb3882f2acb2fa194a8
3
+ metadata.gz: f4a0d0a882b8da1f1bf03f83a78fdd5c358312cb
4
+ data.tar.gz: 7cc74e59ce7c4433bda42268b342702dbba72fd8
5
5
  SHA512:
6
- metadata.gz: 3994b096ae89e3cfe08f0a7358ee336ca38606b161ae8b5ffd8232ccb0b8e0efc75e53fa0aa85952be798f6da6b2fd4771e8f021b5fa9a134e0f6f2f9300e23b
7
- data.tar.gz: a2d931e05870443e7a741df982193d8499eaeae9fffc04f6641cef25fec39067f5d4807e94289743e9946dcfd31667a10e9de2a10a52e09e13c588cfd421398d
6
+ metadata.gz: a325925c0458a6a4ce764e449b66890ac998ba0c6eff8ca18837596d6fb1f91da74c04841566cd53489297a0cc6069a8e9d6e04d9f17a0057614a594fec563c9
7
+ data.tar.gz: 2ec4705cff77ce7254edb1fc7c6aeb123915199e549c9e50875821f60b931028a5882a4526bf1294d571d377470680b196d9571717f3c6e3748aebcc26df0150
@@ -1,8 +1,11 @@
1
1
  module FutureProof
2
+
3
+ # Module +Exceptionable+ provides methods to read values with exceptions
2
4
  module Exceptionable
3
5
 
4
6
  private
5
7
 
8
+ # Returns value or raises exception if its an exception instance
6
9
  def raise_or_value(value)
7
10
  value.is_a?(StandardError) ? raise(value) : value
8
11
  end
@@ -1,23 +1,52 @@
1
1
  module FutureProof
2
+
3
+ # Class +Futures+ can be used to create Procs
4
+ # that should be executed inside a Thread.
2
5
  class Future
6
+
7
+ # Initializes new +Future+.
8
+ #
9
+ # @example Initialize +Future+
10
+ # FutureProof::Future.new { |a, b| a + b }
3
11
  def initialize(&block)
4
12
  @block = block
5
13
  end
6
14
 
15
+ # Executes +Future+ in a thread with given params.
16
+ #
17
+ # @param [Array<Object>] *args proc arguments
18
+ #
19
+ # @exmaple call +Future+
20
+ # future.call(2, 2)
7
21
  def call(*args)
8
22
  thread(*args)
9
23
  end
10
24
 
25
+ # Return a result of a +Future+.
26
+ #
27
+ # @param [Array<Object>] *args proc arguments if they were not suplied before.
28
+ #
29
+ # @note It requires list of arguments if +Future+ was not called before.
30
+ #
31
+ # @exmaple Get value of +Future+
32
+ # future.value
11
33
  def value(*args)
12
34
  thread(*args).value
13
35
  end
14
36
 
37
+ # Return true if Future is done working.
38
+ #
39
+ # @exmaple Get state of +Future+
40
+ # future.complete?
41
+ #
42
+ # @return [true, galse] true if +Future+ is done working.
15
43
  def complete?
16
44
  !thread.alive?
17
45
  end
18
46
 
19
47
  private
20
48
 
49
+ # Thread that +Future+ is wrapped around.
21
50
  def thread(*args)
22
51
  @thread ||= Thread.new { @block.call *args }
23
52
  end
@@ -1,4 +1,8 @@
1
1
  module FutureProof
2
+
3
+ # +FutureArray+ should be used to raise exceptions
4
+ # if specific values are exception instances
5
+ # on a direct access with #[], #first, #last, #each, #sort and so on.
2
6
  class FutureArray
3
7
  include Enumerable
4
8
  include FutureProof::Exceptionable
@@ -7,10 +11,14 @@ module FutureProof
7
11
  @arry = Array.new arg
8
12
  end
9
13
 
14
+ # Acces +FutureArray+ value by index.
10
15
  def [](index)
11
16
  raise_or_value @arry[index]
12
17
  end
13
18
 
19
+ # Array of values.
20
+ #
21
+ # @note raises an exception if any value if an exception.
14
22
  def all
15
23
  map { |a| a }
16
24
  end
@@ -21,6 +29,9 @@ module FutureProof
21
29
  end
22
30
  end
23
31
 
32
+ # Iterates through array elements.
33
+ #
34
+ # @note raises an error if any value is an exception.
24
35
  def each
25
36
  @arry.each { |a| yield raise_or_value(a) }
26
37
  end
@@ -1,4 +1,7 @@
1
1
  module FutureProof
2
+
3
+ # +FutureQueue+ is designed to handle exceptions that happen in a threads.
4
+ # It raises an exception when user tries to access "exceptional" value.
2
5
  class FutureQueue < Queue
3
6
 
4
7
  include FutureProof::Exceptionable
@@ -8,6 +11,11 @@ module FutureProof
8
11
  super()
9
12
  end
10
13
 
14
+ # Pushes value into a queue by executing it.
15
+ # If execution results in an exception
16
+ # then exception is stored itself.
17
+ #
18
+ # @note allowed only when queue is running.
11
19
  def push(*values, &block)
12
20
  raise_future_proof_exception if finished?
13
21
  value = if block_given?
@@ -22,36 +30,58 @@ module FutureProof
22
30
  super(value)
23
31
  end
24
32
 
33
+ # Pops value or raises an exception.
34
+ #
35
+ # @return [Object] first value of queue.
36
+ #
37
+ # @note allowed only when queue is running.
25
38
  def pop
26
39
  raise_future_proof_exception if finished?
27
40
  raise_or_value super
28
41
  end
29
42
 
43
+ # Returns +FutureArray+ with all values.
44
+ #
45
+ # @return [FutureProof::FutureArray] Array with values.
46
+ #
47
+ # @note allowed only when queue is stopped.
30
48
  def values
31
49
  raise_future_proof_exception unless finished?
32
50
  @values ||= FutureProof::FutureArray.new(instance_variable_get(:@que).dup)
33
51
  end
34
52
 
53
+ # Returns +FutureArray+ with all values.
54
+ #
55
+ # @return [Object] value in a queue.
56
+ #
57
+ # @note allowed only when queue is stopped.
58
+ # @note raises an exception if it happened during execution.
35
59
  def [](index)
36
60
  raise_future_proof_exception unless finished?
37
61
  values[index]
38
62
  end
39
63
 
64
+ # Stops queue.
40
65
  def stop!
41
66
  @finished = true
42
67
  end
43
68
 
69
+ # Starts queue.
44
70
  def start!
45
71
  @values = nil
46
72
  @finished = false
47
73
  end
48
74
 
49
- private
50
-
75
+ # Checks if queue is stopped.
76
+ #
77
+ # @return [true, false] true if queue is not working.
51
78
  def finished?
52
79
  @finished
53
80
  end
54
81
 
82
+ private
83
+
84
+ # Raises an exception if queue is accessed in the wrong state.
55
85
  def raise_future_proof_exception
56
86
  raise FutureProof::FutureProofException.new 'Queu is not accessible in the state given!'
57
87
  end
@@ -1,7 +1,12 @@
1
1
  require 'thread'
2
2
 
3
3
  module FutureProof
4
+
5
+ # +ThreadPool+ could be used to schedule and group threads.
4
6
  class ThreadPool
7
+ # Initializes a new thread pool.
8
+ #
9
+ # @params [FixNum] size the size of the pool thread.
5
10
  def initialize(size)
6
11
  @size = size
7
12
  @threads = []
@@ -9,10 +14,21 @@ module FutureProof
9
14
  @values = FutureProof::FutureQueue.new
10
15
  end
11
16
 
17
+ # Submits a task to a thread pool.
18
+ #
19
+ # @param [Array<Object>] *args job arguments.
20
+ #
21
+ # @example
22
+ # thread_pool.submit(25, 2) { |a, b| a ** b }
23
+ #
24
+ # @note Does not start the execution until #perform is called.
12
25
  def submit(*args, &block)
13
26
  @queue.push [block, args]
14
27
  end
15
28
 
29
+ # Starts execution of the thread pool.
30
+ #
31
+ # @note Can be restarted after finalization.
16
32
  def perform
17
33
  unless @threads.any? { |t| t.alive? }
18
34
  @values.start!
@@ -30,25 +46,27 @@ module FutureProof
30
46
  end
31
47
  end
32
48
 
49
+ # Flags that after all pool jobs are processed thread pool should stop the reactor.
33
50
  def finalize
34
51
  @size.times { @queue.push :END_OF_WORK }
35
52
  end
36
53
 
54
+ # Calls #finalize and block programm flow until all jobs are processed.
37
55
  def wait
38
56
  finalize
39
57
  @threads.map &:join
40
58
  end
41
59
 
60
+ # Calls #wait and returns array of all calculated values.
61
+ #
62
+ # @return [FutureProof::FutureArray] instance of FutureArray with all values.
42
63
  def values
43
64
  wait
44
65
  @values.stop!
45
66
  @values.values
46
67
  end
47
68
 
48
- def finalize
49
- @size.times { @queue.push :END_OF_WORK }
50
- end
51
-
69
+ # Commands to remove all pool tasks and finishes the execution after all running tasks are completed.
52
70
  def finalize!
53
71
  @queue.clear
54
72
  finalize
@@ -1,3 +1,3 @@
1
1
  module FutureProof
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -71,7 +71,11 @@ describe FutureProof::ThreadPool do
71
71
  end
72
72
 
73
73
  describe 'exceptions' do
74
- before { thread_pool.submit(24, 0, &task) }
74
+ before do
75
+ thread_pool.submit 24, 0 do |a, b|
76
+ a / b
77
+ end
78
+ end
75
79
 
76
80
  context 'without asking for specific value' do
77
81
  it 'should not raise exceptions' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: future_proof
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Cernovs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-18 00:00:00.000000000 Z
11
+ date: 2013-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler