taskbag 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6616c9985c2c790afcb154a3cbd4712c54486033
4
+ data.tar.gz: 545ed492816ecc0247efec8950e66669e0d05174
5
+ SHA512:
6
+ metadata.gz: 458768e9b9c0c79797b690458a1cf82aacd8ebafcf6a55f3ed8c03479da40f84e7b597d0a063d513ee83b89f0ad049110eab2ce3bf74dbbce87d9f207f289831
7
+ data.tar.gz: 5b2f71e994403c7bcc1046becdb7ca0d8c50e5fa59bde3a62a6d5cc07317384b6802105961b9670281c70de69ea71f84d5cec50f3a52066843fbfee141f2eab1
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ bundler_args: --without development
3
+ cache: bundler
4
+ rvm:
5
+ - "2.1.1"
6
+ - "2.0.0"
7
+ - "1.9.3"
8
+ - "1.9.2"
9
+ script: bundle exec rspec
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # TaskBag
2
+ [![Gem Version](https://badge.fury.io/rb/taskbag.png)](http://badge.fury.io/rb/taskbag)
2
3
 
3
4
  A super simple implementation of the Bag-of-Tasks Paradigm [1].
4
5
 
@@ -31,7 +32,7 @@ In TaskBag you can actually have different tasks in the same bag.
31
32
 
32
33
  ### When not to use it
33
34
 
34
- If you don't in the above.
35
+ If you're not in the above situation.
35
36
 
36
37
  ### How to use it
37
38
 
@@ -77,4 +78,5 @@ As said before, you can add more tasks as the result of a job, for example, if y
77
78
  ## References
78
79
 
79
80
  [1] http://www.cs.arizona.edu/~greg/mpdbook/glossary.html
80
- [2] http://www.eead.csic.es/compbio/material/programacion_rocks/pics/paral_tareas.pdf
81
+
82
+ [2] http://www.eead.csic.es/compbio/material/programacion_rocks/pics/paral_tareas.pdf
data/lib/taskbag.rb CHANGED
@@ -1,3 +1,11 @@
1
1
  require 'taskbag/bag'
2
2
  require 'taskbag/worker'
3
3
  require 'taskbag/version'
4
+
5
+ module TaskBag
6
+ def self.work(nworkers)
7
+ bag = Bag.open(nworkers)
8
+ yield bag
9
+ bag.close!
10
+ end
11
+ end
data/lib/taskbag/bag.rb CHANGED
@@ -35,5 +35,13 @@ module TaskBag
35
35
  def next
36
36
  @jobs.pop unless @jobs.empty?
37
37
  end
38
+
39
+ def self.open(nworkers)
40
+ Bag.new.tap {|b| b.open(nworkers)}
41
+ end
42
+
43
+ def nworkers
44
+ @threads.size
45
+ end
38
46
  end
39
47
  end
@@ -1,3 +1,3 @@
1
1
  module TaskBag
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
data/spec/bag_spec.rb CHANGED
@@ -1,79 +1,94 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe TaskBag::Bag do
4
- let(:mocked_queue) { double('mocked queue', empty?: true) }
5
- subject { TaskBag::Bag.new }
6
-
7
- it { should be_closed }
8
-
9
- describe 'opening a bag' do
10
- it 'makes the bag open' do
11
- subject.open 0
12
- subject.should_not be_closed
13
- subject.close!
14
- end
15
-
16
- it 'raises an exception if already open' do
17
- subject.open 0
18
- expect { subject.open(0) }.to raise_error
19
- end
20
-
21
- it 'creates the given number of workers in different threads' do
22
- TaskBag::Worker.should_receive(:start)
23
- .with(subject)
24
- .exactly(3).times
25
- .and_return 1, 2, 3
26
-
27
- subject.open(3).should have(3).items
28
- subject.close!
29
- end
30
- end
31
-
32
- describe 'closing a bag' do
33
- subject { TaskBag::Bag.new(mocked_queue) }
34
-
35
- it 'should be closed after' do
36
- mocked_queue.should_receive(:empty?).and_return true
37
- subject.open 0
38
- subject.close!
39
- subject.should be_closed
40
- end
41
-
42
- it 'raises an exception if already closed' do
43
- expect { subject.close! }.to raise_error
44
- end
45
-
46
- it 'waits for all jobs to be passed to workers before closing' do
47
- mocked_queue.should_receive(:empty?).and_return false, false, true
48
- subject.open 0
49
- subject.close!
50
- end
51
-
52
- context 'a full bag' do
53
- subject { TaskBag::Bag.new }
54
- it 'waits for every worker to finish before it closes' do
55
- finished = 0
56
- job = double('job')
57
- job.stub :run do
58
- sleep 0.5
59
- finished += 1
60
- end
61
- subject.open 2
62
- subject.add job
63
- subject.add job
64
- subject.add job
65
- subject.close!
66
- finished.should == 3
67
- end
68
- end
69
- end
70
-
71
- it 'execute the jobs in a FIFO fashion' do
72
- subject.add 1
73
- subject.add 2
74
-
75
- subject.next.should be 1
76
- subject.next.should be 2
77
- subject.next.should be_nil
78
- end
79
- end
4
+ let(:mocked_queue) { double('mocked queue', empty?: true) }
5
+ subject { TaskBag::Bag.new }
6
+
7
+ it { should be_closed }
8
+
9
+ describe '.open' do
10
+ subject { TaskBag::Bag.open(5) }
11
+
12
+ it { should_not be_closed }
13
+
14
+ it 'is opened with the given number of workers' do
15
+ subject.nworkers.should == 5
16
+ end
17
+ end
18
+
19
+ describe 'opening a bag' do
20
+ it 'makes the bag open' do
21
+ subject.open 0
22
+ subject.should_not be_closed
23
+ subject.close!
24
+ end
25
+
26
+ it 'raises an exception if already open' do
27
+ subject.open 0
28
+ expect { subject.open(0) }.to raise_error
29
+ end
30
+
31
+ it 'creates the given number of workers in different threads' do
32
+ TaskBag::Worker.should_receive(:start)
33
+ .with(subject)
34
+ .exactly(3).times
35
+ .and_return 1, 2, 3
36
+
37
+ subject.open(3).should have(3).items
38
+ subject.close!
39
+ end
40
+
41
+ it 'is opened with the given number of workers' do
42
+ subject.open 10
43
+ subject.nworkers.should == 10
44
+ end
45
+ end
46
+
47
+ describe 'closing a bag' do
48
+ subject { TaskBag::Bag.new(mocked_queue) }
49
+
50
+ it 'should be closed after' do
51
+ mocked_queue.should_receive(:empty?).and_return true
52
+ subject.open 0
53
+ subject.close!
54
+ subject.should be_closed
55
+ end
56
+
57
+ it 'raises an exception if already closed' do
58
+ expect { subject.close! }.to raise_error
59
+ end
60
+
61
+ it 'waits for all jobs to be passed to workers before closing' do
62
+ mocked_queue.should_receive(:empty?).and_return false, false, true
63
+ subject.open 0
64
+ subject.close!
65
+ end
66
+
67
+ context 'a full bag' do
68
+ subject { TaskBag::Bag.new }
69
+ it 'waits for every worker to finish before it closes' do
70
+ finished = 0
71
+ job = double('job')
72
+ job.stub :run do
73
+ sleep 0.5
74
+ finished += 1
75
+ end
76
+ subject.open 2
77
+ subject.add job
78
+ subject.add job
79
+ subject.add job
80
+ subject.close!
81
+ finished.should == 3
82
+ end
83
+ end
84
+ end
85
+
86
+ it 'execute the jobs in a FIFO fashion' do
87
+ subject.add 1
88
+ subject.add 2
89
+
90
+ subject.next.should be 1
91
+ subject.next.should be 2
92
+ subject.next.should be_nil
93
+ end
94
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe TaskBag do
4
+ let(:bag) { double('task bag') }
5
+ around {|example| TaskBag.work(10, &example)}
6
+
7
+ describe '.work' do
8
+ it 'opens a bag with the given number of workers' do
9
+ allow(bag).to receive(:close!)
10
+ expect(TaskBag::Bag).to receive(:open).with(10) { bag }
11
+
12
+ TaskBag.work(10) do |b|
13
+ expect(b).to be bag
14
+ end
15
+ end
16
+
17
+ it 'closes the bag after it is done with the given block' do
18
+ allow(TaskBag::Bag).to receive(:open).and_return(bag)
19
+ TaskBag.work(10) do |b|
20
+ # This will guarantee that the close! method will only
21
+ # be called after this block is completed
22
+ expect(bag).to receive(:close!)
23
+ end
24
+ end
25
+ end
26
+ end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taskbag
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
5
- prerelease:
4
+ version: 2.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Carlos Palhares
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-06 00:00:00.000000000 Z
11
+ date: 2014-04-23 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  description: A simplistic task of bags implementation for multithreaded scripts
@@ -65,7 +58,8 @@ executables: []
65
58
  extensions: []
66
59
  extra_rdoc_files: []
67
60
  files:
68
- - .gitignore
61
+ - ".gitignore"
62
+ - ".travis.yml"
69
63
  - Gemfile
70
64
  - LICENSE.txt
71
65
  - README.md
@@ -76,34 +70,35 @@ files:
76
70
  - lib/taskbag/worker.rb
77
71
  - spec/bag_spec.rb
78
72
  - spec/spec_helper.rb
73
+ - spec/taskbag_spec.rb
79
74
  - spec/worker_spec.rb
80
75
  - taskbag.gemspec
81
76
  homepage: https://github.com/xjunior/taskbag
82
77
  licenses:
83
78
  - MIT
79
+ metadata: {}
84
80
  post_install_message:
85
81
  rdoc_options: []
86
82
  require_paths:
87
83
  - lib
88
84
  required_ruby_version: !ruby/object:Gem::Requirement
89
- none: false
90
85
  requirements:
91
- - - ! '>='
86
+ - - ">="
92
87
  - !ruby/object:Gem::Version
93
88
  version: '0'
94
89
  required_rubygems_version: !ruby/object:Gem::Requirement
95
- none: false
96
90
  requirements:
97
- - - ! '>='
91
+ - - ">="
98
92
  - !ruby/object:Gem::Version
99
93
  version: '0'
100
94
  requirements: []
101
95
  rubyforge_project:
102
- rubygems_version: 1.8.23
96
+ rubygems_version: 2.2.2
103
97
  signing_key:
104
- specification_version: 3
98
+ specification_version: 4
105
99
  summary: A simplistic task of bags implementation for multithreaded scripts
106
100
  test_files:
107
101
  - spec/bag_spec.rb
108
102
  - spec/spec_helper.rb
103
+ - spec/taskbag_spec.rb
109
104
  - spec/worker_spec.rb