|
16 | 16 | end
|
17 | 17 |
|
18 | 18 | context 'with parallel processing' do
|
| 19 | + it 'should only Process.wait() its own children' do |
| 20 | + class Foo |
| 21 | + def one(arg, _logger = nil) |
| 22 | + 'one ' + arg |
| 23 | + end |
| 24 | + |
| 25 | + def two(arg, _logger = nil) |
| 26 | + 'two ' + arg |
| 27 | + end |
| 28 | + |
| 29 | + def dont_wait_me_bro(sleep_for = 1) |
| 30 | + # do we need a rescue block here? |
| 31 | + pid = fork do |
| 32 | + sleep sleep_for |
| 33 | + Kernel.exit! 0 # Kernel.exit! avoids at_exit from parents being triggered by children exiting |
| 34 | + end |
| 35 | + pid |
| 36 | + end |
| 37 | + |
| 38 | + def wait_on_me(pid) |
| 39 | + status = nil |
| 40 | + # just in case status never equals anything |
| 41 | + count = 100 |
| 42 | + while status.nil? || count > 0 |
| 43 | + count -= 1 |
| 44 | + status = Process.waitpid2(pid, Process::WNOHANG) |
| 45 | + end |
| 46 | + status |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + c = Foo.new |
| 51 | + # start my non-parallel process first |
| 52 | + just_a_guy = c.dont_wait_me_bro |
| 53 | + |
| 54 | + one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1') |
| 55 | + two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2') |
| 56 | + result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true) |
| 57 | + expect(result).to be_a_kind_of(Array) |
| 58 | + expect(result.size).to eq(2) |
| 59 | + |
| 60 | + one_result = result[0] |
| 61 | + expect(one_result).to be_a_kind_of(OctocatalogDiff::Util::Parallel::Result) |
| 62 | + expect(one_result.status).to eq(true) |
| 63 | + expect(one_result.exception).to eq(nil) |
| 64 | + expect(one_result.output).to match(/^one abc/) |
| 65 | + |
| 66 | + two_result = result[1] |
| 67 | + expect(two_result).to be_a_kind_of(OctocatalogDiff::Util::Parallel::Result) |
| 68 | + expect(two_result.status).to eq(true) |
| 69 | + expect(two_result.exception).to eq(nil) |
| 70 | + expect(two_result.output).to match(/^two def/) |
| 71 | + |
| 72 | + # just_a_guy should still be need to be waited |
| 73 | + result = c.wait_on_me(just_a_guy) |
| 74 | + expect(result).to be_a_kind_of(Array) |
| 75 | + # test result and check for error conditions |
| 76 | + end |
| 77 | + |
19 | 78 | it 'should parallelize and return task results' do
|
20 | 79 | class Foo
|
21 | 80 | def one(arg, _logger = nil)
|
|
0 commit comments