33require 'logger'
44require 'parallel'
55
6+ # rubocop:disable Style/GlobalVars
67describe OctocatalogDiff ::Util ::Parallel do
8+ before ( :each ) do
9+ $octocatalog_diff_util_parallel_spec_tempdir = Dir . mktmpdir
10+ end
11+
12+ after ( :each ) do
13+ OctocatalogDiff ::Spec . clean_up_tmpdir ( $octocatalog_diff_util_parallel_spec_tempdir)
14+ end
15+
716 context 'with parallel processing' do
817 it 'should parallelize and return task results' do
918 class Foo
1019 def one ( arg , _logger = nil )
11- time_save = Time . now
12- sleep 2
13- 'one ' + arg + ' ' + time_save . to_i . to_s
20+ [ 'one' , arg , Process . pid ] . join ( ' ' )
1421 end
1522
1623 def two ( arg , _logger = nil )
17- sleep 1.25
18- 'two ' + arg + ' ' + Time . now . to_i . to_s
24+ [ 'two' , arg , Process . pid ] . join ( ' ' )
1925 end
2026 end
2127
@@ -30,28 +36,33 @@ def two(arg, _logger = nil)
3036 expect ( one_result ) . to be_a_kind_of ( OctocatalogDiff ::Util ::Parallel ::Result )
3137 expect ( one_result . status ) . to eq ( true )
3238 expect ( one_result . exception ) . to eq ( nil )
33- expect ( one_result . output ) . to match ( /^one abc \d +$ / )
39+ expect ( one_result . output ) . to match ( /^one abc / )
3440
3541 two_result = result [ 1 ]
3642 expect ( two_result ) . to be_a_kind_of ( OctocatalogDiff ::Util ::Parallel ::Result )
3743 expect ( two_result . status ) . to eq ( true )
3844 expect ( two_result . exception ) . to eq ( nil )
39- expect ( two_result . output ) . to match ( /^two def \d +$ / )
45+ expect ( two_result . output ) . to match ( /^two def / )
4046
41- one_time = Regexp . last_match ( 1 ) . to_i if one_result . output =~ /(\d +)$/
42- two_time = Regexp . last_match ( 1 ) . to_i if two_result . output =~ /(\d +)$/
43- expect ( one_time ) . to be_within ( 2 ) . of ( two_time )
44- expect ( one_time ) . to be < two_time
47+ # Process ID should be difference since the tasks are supposed to be forked
48+ one_pid = one_result . output . split ( /\s +/ ) . last
49+ two_pid = two_result . output . split ( /\s +/ ) . last
50+ expect ( one_pid ) . not_to be_nil
51+ expect ( one_pid ) . not_to eq ( two_pid )
4552 end
4653
4754 it 'should handle a task that fails after other successes' do
4855 class Foo
4956 def one ( arg , _logger = nil )
57+ File . open ( File . join ( $octocatalog_diff_util_parallel_spec_tempdir, 'one' ) , 'w' ) { |f | f . write '' }
5058 'one ' + arg
5159 end
5260
5361 def two ( _arg , _logger = nil )
54- sleep 1
62+ 100 . times do
63+ break if File . file? ( File . join ( $octocatalog_diff_util_parallel_spec_tempdir, 'one' ) )
64+ sleep 0.1
65+ end
5566 raise 'Two failed'
5667 end
5768 end
@@ -79,7 +90,8 @@ def two(_arg, _logger = nil)
7990 it 'should kill running tasks when one task fails' do
8091 class Foo
8192 def one ( arg , _logger = nil )
82- sleep 1
93+ sleep 10
94+ File . open ( File . join ( $octocatalog_diff_util_parallel_spec_tempdir, 'one' ) , 'w' ) { |f | f . write '' }
8395 'one ' + arg
8496 end
8597
@@ -107,6 +119,8 @@ def two(_arg, _logger = nil)
107119 expect ( two_result . status ) . to eq ( false )
108120 expect ( two_result . exception ) . to be_a_kind_of ( RuntimeError )
109121 expect ( two_result . exception . message ) . to eq ( 'Two failed' )
122+
123+ expect ( File . file? ( File . join ( $octocatalog_diff_util_parallel_spec_tempdir, 'one' ) ) ) . to eq ( false )
110124 end
111125
112126 it 'should log debug messages' do
@@ -273,7 +287,6 @@ def two(arg, _logger = nil)
273287
274288 one_time = Regexp . last_match ( 1 ) . to_i if one_result . output =~ /(\d +)$/
275289 two_time = Regexp . last_match ( 1 ) . to_i if two_result . output =~ /(\d +)$/
276- expect ( one_time ) . to be_within ( 2 ) . of ( two_time )
277290 expect ( one_time ) . to be < two_time
278291 end
279292
@@ -468,3 +481,4 @@ def validate(arg, _logger = nil)
468481 end
469482 end
470483end
484+ # rubocop:enable Style/GlobalVars
0 commit comments