team_effort 1.0.1 → 1.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
  SHA256:
3
- metadata.gz: 2d7779d24f1c2aa78460778a38d505c106677eab6afe2c829ebd551b169ce551
4
- data.tar.gz: 2f96281c9c5558d23d4bb49153d450bd8a4617b0ec551d93fcb6ebab5de13246
3
+ metadata.gz: fd78f953296ae896e5e3c7adff6b079b04203376c2cddf7e57a0fdde2c7757ca
4
+ data.tar.gz: 97bbea07471ca7c8e84a53db5e25ce113f03d8c505bce7c73b6c0327c73f2b53
5
5
  SHA512:
6
- metadata.gz: 7090174299461bd8e9d50190c3b7d10cede4fcb918985d741c72e629860ee2b98f866daf0c512f3b5fe5c452ca07972c70e09bd39d8f858d671e74ffa9bd5e66
7
- data.tar.gz: 24a64b1dbaf6b2b6f4d6718bd49cf208e9cb92f6c3d86ef0b5d3c28afc71eba4a65b0e72eb5707eecf09bfda6318de90be6dee044df69fc86e936444ddc7f629
6
+ metadata.gz: 9fce2986ca5a3812d5d2e6af686ae8ab423c5f2c2cf80a55eff218a42fef43257e2cfe5e815bd49b1378f2393185b82b141eed3f76d0d3dfc8af9a8cd5fcc26c
7
+ data.tar.gz: 309b7f39b82f8cf0e4de1294f6742689c4fc85caa6d28ff904959d2517391a4c1fb4c8670f61a0afe571d0b40f4e3b65d0be1acb26d825fbd45d572f81a46965
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.libs << 'test'
5
+ t.pattern = "test/*_test.rb"
6
6
  end
7
7
 
8
8
  desc 'Run tests'
data/lib/team_effort.rb CHANGED
@@ -4,13 +4,14 @@ module TeamEffort
4
4
  def self.work(enumerable, max_process_count = 4, progress_proc: nil)
5
5
  pids = []
6
6
  arg_sets = []
7
+ previous_proc_result = nil
7
8
 
8
9
  max_count = enumerable.count
9
10
  completed_count = 0
10
11
 
11
12
  enumerable.each do |arg_set|
12
13
  while pids.size == max_process_count
13
- pids, arg_sets, completed_count = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
14
+ pids, arg_sets, completed_count, previous_proc_result = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc, previous_proc_result)
14
15
  end
15
16
 
16
17
  pids << fork do
@@ -28,11 +29,11 @@ module TeamEffort
28
29
  end
29
30
 
30
31
  while !pids.empty?
31
- pids, arg_sets, completed_count = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
32
+ pids, arg_sets, completed_count, previous_proc_result = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc, previous_proc_result)
32
33
  end
33
34
  end
34
35
 
35
- def self.wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
36
+ def self.wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc, previous_proc_result)
36
37
  pid, status = Process.wait2
37
38
  pids_index = pids.index(pid)
38
39
  if pids_index
@@ -40,7 +41,13 @@ module TeamEffort
40
41
  arg_set = arg_sets.delete_at pids_index
41
42
  raise "TeamEffort child process failed when processing > #{arg_set} <" if !status.success?
42
43
  end
43
- progress_proc.call(completed_count += 1, max_count) if progress_proc
44
- [pids, arg_sets, completed_count]
44
+
45
+ if progress_proc
46
+ progress_proc_args = [completed_count += 1, max_count]
47
+ progress_proc_args << previous_proc_result if progress_proc.arity == 3
48
+ proc_result = progress_proc.call(*progress_proc_args)
49
+ end
50
+
51
+ [pids, arg_sets, completed_count, proc_result]
45
52
  end
46
53
  end
@@ -1,3 +1,3 @@
1
1
  module TeamEffort
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -24,16 +24,16 @@ describe TeamEffort do
24
24
  end
25
25
 
26
26
  lines = output.split(/\n/)
27
- lines.size.must_equal 3
27
+ _(lines.size).must_equal 3
28
28
  lines.each do |line|
29
- line.must_match(/^\d+$/)
29
+ _(line).must_match(/^\d+$/)
30
30
  end
31
31
  end
32
32
 
33
33
  it 'exits when a child process fails' do
34
34
  data = [8, 4, 2, 0, 4, 8]
35
35
 
36
- err = -> {
36
+ err = _(-> {
37
37
  $stderr = File.open('/dev/null', 'w')
38
38
  begin
39
39
  TeamEffort.work(data, 1) do |item|
@@ -43,8 +43,8 @@ describe TeamEffort do
43
43
  $stderr.close
44
44
  $stderr = STDERR
45
45
  end
46
- }.must_raise RuntimeError
47
- err.message.must_match /TeamEffort child process failed when processing > 0 </
46
+ }).must_raise RuntimeError
47
+ _(err.message).must_match %r/TeamEffort child process failed when processing > 0 </
48
48
  end
49
49
 
50
50
  it 'invokes an optional proc when it completes an item' do
@@ -53,13 +53,29 @@ describe TeamEffort do
53
53
  proc = ->(item_index, max_items) { proc_data << [item_index, max_items] }
54
54
  TeamEffort.work(data, 1, progress_proc: proc) {}
55
55
 
56
- proc_data.must_equal [
56
+ _(proc_data).must_equal [
57
57
  [1, 3],
58
58
  [2, 3],
59
59
  [3, 3],
60
60
  ]
61
61
  end
62
62
 
63
+ it 'invokes an optional proc with the previous result of the optional proc when it completes an item' do
64
+ data = %w|one two three|
65
+ proc_data = []
66
+ proc = ->(item_index, max_items, previous_result) {
67
+ if previous_result == 2
68
+ proc_data << [item_index, max_items]
69
+ end
70
+ item_index
71
+ }
72
+ TeamEffort.work(data, 1, progress_proc: proc) {}
73
+
74
+ _(proc_data).must_equal [
75
+ [3, 3]
76
+ ]
77
+ end
78
+
63
79
  it 'ignores other child process completions' do
64
80
  output_io_class = Class.new do
65
81
  def initialize
@@ -112,7 +128,7 @@ describe TeamEffort do
112
128
 
113
129
  lines = output_io.lines
114
130
 
115
- lines.must_equal [
131
+ _(lines).must_equal [
116
132
  'unmanaged starting',
117
133
  'unmanaged waiting for IO',
118
134
  'task 1 starting',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: team_effort
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Felkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-10 00:00:00.000000000 Z
11
+ date: 2020-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubyforge_project:
80
- rubygems_version: 2.7.7
79
+ rubygems_version: 3.0.8
81
80
  signing_key:
82
81
  specification_version: 4
83
82
  summary: Use child processes to process a collection in parallel