Skip to content

Commit cc8c21d

Browse files
Isaiah FrantzIsaiah Frantz
authored andcommitted
test that non-parallel launched processes dont get waited
1 parent 5e942d6 commit cc8c21d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,65 @@
1616
end
1717

1818
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? or count <= 0
43+
status = Process.waitpid(pid, Process::WNOHANG)
44+
count -= 1
45+
end
46+
end
47+
end
48+
49+
c = Foo.new
50+
# start my non-parallel process first
51+
just_a_guy = c.dont_wait_me_bro()
52+
53+
one = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:one), args: 'abc', description: 'test1')
54+
two = OctocatalogDiff::Util::Parallel::Task.new(method: c.method(:two), args: 'def', description: 'test2')
55+
result = OctocatalogDiff::Util::Parallel.run_tasks([one, two], nil, true)
56+
expect(result).to be_a_kind_of(Array)
57+
expect(result.size).to eq(2)
58+
59+
one_result = result[0]
60+
expect(one_result).to be_a_kind_of(OctocatalogDiff::Util::Parallel::Result)
61+
expect(one_result.status).to eq(true)
62+
expect(one_result.exception).to eq(nil)
63+
expect(one_result.output).to match(/^one abc/)
64+
65+
two_result = result[1]
66+
expect(two_result).to be_a_kind_of(OctocatalogDiff::Util::Parallel::Result)
67+
expect(two_result.status).to eq(true)
68+
expect(two_result.exception).to eq(nil)
69+
expect(two_result.output).to match(/^two def/)
70+
71+
# just_a_guy should still be need to be waited
72+
result = c.wait_on_me(just_a_guy)
73+
expect(result).to be_a_kind_of(Array)
74+
# test result and check for error conditions
75+
76+
end
77+
1978
it 'should parallelize and return task results' do
2079
class Foo
2180
def one(arg, _logger = nil)

0 commit comments

Comments
 (0)