Skip to content

Commit e195af1

Browse files
committed
Harmonize tests and new parallel methods
1 parent e0f6cee commit e195af1

2 files changed

Lines changed: 28 additions & 22 deletions

File tree

lib/octocatalog-diff/util/parallel.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def initialize(opts = {})
6363
#
6464
# Note: Parallelization throws intermittent errors under travis CI, so it will be disabled by
6565
# default for integration tests.
66-
def self.run_tasks(task_array, logger = nil, parallelized = true)
66+
def self.run_tasks(task_array, logger = nil, parallelized = true, raise_exception = true)
6767
# Create a throwaway logger object if one is not given
6868
logger ||= Logger.new(StringIO.new)
6969

@@ -82,12 +82,13 @@ def self.run_tasks(task_array, logger = nil, parallelized = true)
8282
end
8383
logger.debug "Initialized parallel task result array: size=#{result.size}"
8484

85-
if parallelized
85+
exception = if parallelized
8686
run_tasks_parallel(result, task_array, logger)
8787
else
8888
run_tasks_serial(result, task_array, logger)
8989
end
9090

91+
raise exception if exception && raise_exception
9192
result
9293
end
9394

@@ -128,8 +129,9 @@ def self.run_tasks_parallel(result, task_array, logger)
128129
pidmap.delete(this_pid)
129130

130131
next if result[index].status
131-
raise result[index].exception
132+
return result[index].exception
132133
end
134+
nil
133135
ensure
134136
pidmap.each do |pid, pid_data|
135137
pid_data[:reader].close
@@ -152,11 +154,9 @@ def self.run_tasks_serial(result, task_array, logger)
152154
task_array.each_with_index do |ele, task_counter|
153155
result[task_counter] = execute_task(ele, logger)
154156
next if result[task_counter].status
155-
raise result[task_counter].exception if result[task_counter].exception
156-
# :nocov:
157-
raise "Serial task #{task_counter} had status=#{result[task_counter].status} but no exception was set"
158-
# :nocov:
157+
return result[task_counter].exception
159158
end
159+
nil
160160
end
161161

162162
# Process a single task.
@@ -174,9 +174,15 @@ def self.execute_task(task, logger)
174174
end
175175

176176
begin
177-
task.validate(output, logger)
177+
successful_validation = task.validate(output, logger)
178+
if successful_validation
179+
logger.debug("Success #{task.description}")
180+
else
181+
logger.warn("Failed #{task.description} validation")
182+
raise "Failed #{task.description} validation (unspecified error)"
183+
end
178184
rescue => exc
179-
logger.warn("Failed #{task.description}")
185+
logger.warn("Failed #{task.description} validation: #{exc.class} #{exc.message}")
180186
result.status = false
181187
result.exception = exc
182188
end

spec/octocatalog-diff/tests/util/parallel_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def two(_arg, _logger = nil)
6969
c = Foo.new
7070
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1')
7171
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2')
72-
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true)
72+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true, false)
7373
expect(result).to be_a_kind_of(Array)
7474
expect(result.size).to eq(2)
7575

@@ -102,7 +102,7 @@ def two(_arg, _logger = nil)
102102
c = Foo.new
103103
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1')
104104
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2')
105-
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true)
105+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true, false)
106106
expect(result).to be_a_kind_of(Array)
107107
expect(result.size).to eq(2)
108108

@@ -133,12 +133,12 @@ def my_method(arg, _logger = nil)
133133
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:my_method), args: 'abc', description: 'test1')
134134
expect do
135135
logger = Logger.new(STDERR)
136-
OctocatalogDiff::Util::Parallel.run_tasks([one], logger, true)
136+
OctocatalogDiff::Util::Parallel.run_tasks([one], logger, true, false)
137137
end.to output(/DEBUG.*Begin test1/).to_stderr_from_any_process
138138

139139
expect do
140140
logger = Logger.new(STDERR)
141-
OctocatalogDiff::Util::Parallel.run_tasks([one], logger, true)
141+
OctocatalogDiff::Util::Parallel.run_tasks([one], logger, true, false)
142142
end.to output(/DEBUG.*Success test1/).to_stderr_from_any_process
143143
end
144144

@@ -154,12 +154,12 @@ def my_method(arg, _logger = nil)
154154
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:my_method), args: 'def', description: 'test2')
155155
expect do
156156
logger = Logger.new(STDERR)
157-
OctocatalogDiff::Util::Parallel.run_tasks([one, two], logger, true)
157+
OctocatalogDiff::Util::Parallel.run_tasks([one, two], logger, true, false)
158158
end.to output(/DEBUG.*Begin test[12]/).to_stderr_from_any_process
159159

160160
expect do
161161
logger = Logger.new(STDERR)
162-
OctocatalogDiff::Util::Parallel.run_tasks([one, two], logger, true)
162+
OctocatalogDiff::Util::Parallel.run_tasks([one, two], logger, true, false)
163163
end.to output(/DEBUG.*Failed test[12]: RuntimeError (abc|def)/).to_stderr_from_any_process
164164
end
165165

@@ -215,7 +215,7 @@ def validate(arg, _logger = nil, _extra_args = {})
215215
v = c.method(:validate)
216216
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1', validator: v)
217217
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2', validator: v)
218-
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true)
218+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true, false)
219219
expect(result).to be_a_kind_of(Array)
220220
expect(result.size).to eq(2)
221221

@@ -243,7 +243,7 @@ def validate(arg, _logger = nil, _extra_args = {})
243243
v = c.method(:validate)
244244
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1', validator: v)
245245
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2', validator: v)
246-
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true)
246+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true, false)
247247
expect(result).to be_a_kind_of(Array)
248248
expect(result.size).to eq(2)
249249

@@ -303,7 +303,7 @@ def two(_arg, _logger = nil)
303303
c = Foo.new
304304
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1')
305305
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2')
306-
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, false)
306+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, false, false)
307307
expect(result).to be_a_kind_of(Array)
308308
expect(result.size).to eq(2)
309309

@@ -334,7 +334,7 @@ def two(arg, _logger = nil)
334334
c = Foo.new
335335
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1')
336336
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2')
337-
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, false)
337+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, false, false)
338338
expect(result).to be_a_kind_of(Array)
339339
expect(result.size).to eq(2)
340340

@@ -378,7 +378,7 @@ def my_method(arg, _logger = nil)
378378
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:my_method), args: 'abc', description: 'test1')
379379
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:my_method), args: 'def', description: 'test2')
380380
logger, logger_string = OctocatalogDiff::Spec.setup_logger
381-
OctocatalogDiff::Util::Parallel.run_tasks([one, two], logger, false)
381+
OctocatalogDiff::Util::Parallel.run_tasks([one, two], logger, false, false)
382382
expect(logger_string.string).to match(/DEBUG.*Begin test1/)
383383
expect(logger_string.string).to match(/DEBUG.*Failed test1: RuntimeError abc/)
384384
end
@@ -434,7 +434,7 @@ def validate(arg, _logger = nil, _extra_args = {})
434434
v = c.method(:validate)
435435
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1', validator: v)
436436
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2', validator: v)
437-
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, false)
437+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, false, false)
438438
expect(result).to be_a_kind_of(Array)
439439
expect(result.size).to eq(2)
440440

@@ -461,7 +461,7 @@ def validate(arg, _logger = nil, _extra_args = {})
461461
v = c.method(:validate)
462462
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1', validator: v)
463463
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2', validator: v)
464-
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, false)
464+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, false, false)
465465
expect(result).to be_a_kind_of(Array)
466466
expect(result.size).to eq(2)
467467

0 commit comments

Comments
 (0)