@@ -73,13 +73,13 @@ def initialize
7373 # step "Setup", "bin/setup"
7474 # step "Single test", "bin/rails", "test", "--name", "test_that_is_one"
7575 # step "Flaky test", "bin/rails test test/models/flaky_test.rb", attempts: 3
76- def step ( title , *command , attempts : 1 )
76+ def step ( title , *command , attempts : 1 , timeout : nil )
7777 raise ArgumentError , "attempts must be a positive integer" unless attempts . positive?
7878
7979 heading title , command . join ( " " ) , type : :title
8080
8181 successful = retry_until_success ( attempts ) do
82- system ( *command )
82+ run_command ( *command , timeout : )
8383 end
8484
8585 report ( title ) { results << [ successful , title ] }
@@ -175,5 +175,23 @@ def retry_until_success(attempts)
175175 end
176176 false
177177 end
178+
179+ def run_command ( *command , timeout : nil )
180+ pid = Process . spawn ( *command )
181+ unless timeout
182+ Process . wait ( pid )
183+ return $?. success?
184+ end
185+
186+
187+ Timeout . timeout ( timeout ) do
188+ Process . wait ( pid )
189+ $?. success?
190+ rescue Timeout ::Error
191+ puts "process not finished in #{ timeout } s, killing it"
192+ Process . kill ( "TERM" , pid )
193+ false
194+ end
195+ end
178196 end
179197end
0 commit comments