future_proof 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in future_proof.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nikita Cernovs
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # FutureProof
2
+
3
+ FutureProof provides concurrency extensions for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'future_proof'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install future_proof
18
+
19
+ ## Usage
20
+
21
+ ### Future
22
+
23
+ f = FutureProof::Future.new { |a, b| a + b }
24
+
25
+ f.call # => executing in a thread
26
+
27
+ f.value # => returning value when ready
28
+
29
+ ### ThreadPool
30
+
31
+ tp = FutureProof::ThreadPool.new(5)
32
+
33
+ 10.times do |i|
34
+ tp.submit(Proc.new { |a, b| a + b }, i, i + 1)
35
+ end
36
+
37
+ tp.perform # executing 10 procs in 5 threads
38
+
39
+ tp.values # releasing threads and returning values
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'future_proof/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "future_proof"
8
+ spec.version = FutureProof::VERSION
9
+ spec.authors = ["Nikita Cernovs"]
10
+ spec.email = ["nikita.cernovs@gmail.com"]
11
+ spec.description = %q{Ruby concurrency extenstions}
12
+ spec.summary = %q{Ruby concurrency extenstions}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,23 @@
1
+ class FutureProof::Future
2
+ def initialize(&block)
3
+ @block = block
4
+ end
5
+
6
+ def call(*args)
7
+ thread(*args)
8
+ end
9
+
10
+ def value(*args)
11
+ thread(*args).value
12
+ end
13
+
14
+ def complete?
15
+ !thread.alive?
16
+ end
17
+
18
+ private
19
+
20
+ def thread(*args)
21
+ @thread ||= Thread.new { @block.call *args }
22
+ end
23
+ end
@@ -0,0 +1,58 @@
1
+ require 'thread'
2
+
3
+ class FutureProof::ThreadPool
4
+ def initialize(size)
5
+ @size = size
6
+ @threads = []
7
+ @queue = Queue.new
8
+ @values = Queue.new
9
+ end
10
+
11
+ def submit(job, *args)
12
+ @queue.push [job, args]
13
+ end
14
+
15
+ def perform
16
+ unless @threads.any? { |t| t.alive? }
17
+ @size.times do
18
+ @threads << Thread.new do
19
+ while job = @queue.pop
20
+ if job == :END_OF_WORK
21
+ break
22
+ else
23
+ result = begin
24
+ job[0].call(*job[1])
25
+ rescue => e
26
+ e.to_s
27
+ end
28
+ @values.push result
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def finalize
37
+ @size.times { @queue.push :END_OF_WORK }
38
+ end
39
+
40
+ def wait
41
+ finalize
42
+ @threads.map &:join
43
+ end
44
+
45
+ def values
46
+ wait
47
+ @values.instance_variable_get(:@que)
48
+ end
49
+
50
+ def finalize
51
+ @size.times { @queue.push :END_OF_WORK }
52
+ end
53
+
54
+ def finalize!
55
+ @queue.clear
56
+ finalize
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module FutureProof
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "future_proof/version"
2
+ require 'future_proof/future'
3
+ require 'future_proof/thread_pool'
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe FutureProof::Future do
4
+
5
+ let(:future) do
6
+ FutureProof::Future.new do |a, b|
7
+ sleep 1
8
+ a / b
9
+ end
10
+ end
11
+
12
+ describe '#call' do
13
+ it 'should run a thread' do
14
+ future.call(24, 2).should be_an_instance_of Thread
15
+ end
16
+ end
17
+
18
+ describe '#value' do
19
+ context 'before #call' do
20
+ it 'should calculate value' do
21
+ future.value(24, 2).should eq 12
22
+ end
23
+ end
24
+
25
+ context 'after #call' do
26
+ before { @thread = future.call(24, 2) }
27
+
28
+ it 'should not require arguments' do
29
+ future.value.should eq 12
30
+ end
31
+
32
+ it 'should use the same thread' do
33
+ @thread.should_receive(:value)
34
+ future.value
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '#comlete?' do
40
+ before { future.call(24, 2) }
41
+
42
+ context 'not finished' do
43
+ it 'should not be completed' do
44
+ future.should_not be_complete
45
+ end
46
+ end
47
+
48
+ context 'finished' do
49
+ it 'should be completed' do
50
+ sleep 2
51
+ future.should be_complete
52
+ end
53
+ end
54
+ end
55
+
56
+ describe 'exceptions' do
57
+ describe '#call' do
58
+ it 'should not raise exeptions' do
59
+ expect { future.call(24, 0); sleep 2 }.not_to raise_error
60
+ end
61
+ end
62
+
63
+ describe '#value' do
64
+ it 'should raise exeption' do
65
+ expect { future.value(24, 0) }.to raise_error ZeroDivisionError
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe FutureProof::ThreadPool do
4
+
5
+ let(:pool_size) { 3 }
6
+ let(:task) { Proc.new { |a,b| sleep 1; a / b } }
7
+ let(:thread_pool) { FutureProof::ThreadPool.new pool_size }
8
+
9
+ describe '#submit' do
10
+ before { thread_pool.submit(task, 24, 2) }
11
+
12
+ it 'task should be submitted to queue' do
13
+ thread_pool.instance_variable_get(:@queue).size.should eq 1
14
+ end
15
+
16
+ context 'before perform' do
17
+ it 'shold not have been started to execute' do
18
+ thread_pool.finalize!
19
+ thread_pool.perform
20
+ thread_pool.values.should be_empty
21
+ end
22
+ end
23
+
24
+ context 'after perform' do
25
+ before { thread_pool.perform }
26
+
27
+ describe 'first task' do
28
+ it 'should start executing' do
29
+ thread_pool.finalize
30
+ thread_pool.values.should eq [12]
31
+ end
32
+ end
33
+
34
+ describe 'rest tasks' do
35
+ it 'should start executing on #submit' do
36
+ thread_pool.submit task, 6, 3
37
+ sleep 3
38
+ thread_pool.instance_variable_get(:@values).instance_variable_get(:@que).size.should eq 2
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#finalize' do
45
+ it 'should allow all tasks to finish' do
46
+ 5.times { thread_pool.submit(task, 24, 2) }
47
+ thread_pool.finalize
48
+ thread_pool.perform
49
+ thread_pool.values.should eq [12, 12, 12, 12, 12]
50
+ end
51
+ end
52
+
53
+
54
+ describe '#finalize!' do
55
+ it 'should not start executing not started tasks' do
56
+ 5.times { thread_pool.submit(task, 24, 2) }
57
+ thread_pool.perform
58
+ sleep 0.5
59
+ thread_pool.finalize!
60
+ thread_pool.values.should eq [12, 12, 12]
61
+ end
62
+ end
63
+
64
+ describe 'exceptions' do
65
+ before { thread_pool.submit(task, 24, 0) }
66
+
67
+ it 'should not raise exceptions' do
68
+ expect { thread_pool.perform; thread_pool.wait }.not_to raise_error
69
+ end
70
+
71
+ it 'should return name of an exception' do
72
+ thread_pool.perform
73
+ thread_pool.values.should eq ['divided by 0']
74
+ end
75
+ end
76
+ end
@@ -0,0 +1 @@
1
+ require 'future_proof'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: future_proof
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nikita Cernovs
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Ruby concurrency extenstions
63
+ email:
64
+ - nikita.cernovs@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - future_proof.gemspec
75
+ - lib/future_proof.rb
76
+ - lib/future_proof/future.rb
77
+ - lib/future_proof/thread_pool.rb
78
+ - lib/future_proof/version.rb
79
+ - spec/future_proof/future_spec.rb
80
+ - spec/future_proof/thread_pool_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: ''
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.25
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Ruby concurrency extenstions
107
+ test_files:
108
+ - spec/future_proof/future_spec.rb
109
+ - spec/future_proof/thread_pool_spec.rb
110
+ - spec/spec_helper.rb