Skip to content

Commit f59afea

Browse files
committed
Signal handling spec is more robust
Closes #611.
1 parent 7c084cc commit f59afea

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

spec/mysql2/client_spec.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,28 +417,31 @@ def run_gc
417417
# XXX this test is not deterministic (because Unix signal handling is not)
418418
# and may fail on a loaded system
419419
it "should run signal handlers while waiting for a response" do
420+
kill_time = 0.1
421+
query_time = 2 * kill_time
422+
420423
mark = {}
421-
trap(:USR1) { mark[:USR1] = Time.now }
424+
422425
begin
423-
mark[:START] = Time.now
426+
trap(:USR1) { mark.store(:USR1, Time.now) }
424427
pid = fork do
425-
sleep 0.1 # wait for client query to start
428+
sleep kill_time # wait for client query to start
426429
Process.kill(:USR1, Process.ppid)
427430
sleep # wait for explicit kill to prevent GC disconnect
428431
end
429-
@client.query('SELECT SLEEP(0.2)')
430-
mark[:END] = Time.now
431-
expect(mark.include?(:USR1)).to be true
432-
expect(mark[:USR1] - mark[:START]).to be >= 0.1
433-
expect(mark[:USR1] - mark[:START]).to be < 0.13
434-
expect(mark[:END] - mark[:USR1]).to be > 0.09
435-
expect(mark[:END] - mark[:START]).to be >= 0.2
436-
expect(mark[:END] - mark[:START]).to be < 0.23
432+
mark.store(:QUERY_START, Time.now)
433+
@client.query("SELECT SLEEP(#{query_time})")
434+
mark.store(:QUERY_END, Time.now)
435+
ensure
437436
Process.kill(:TERM, pid)
438437
Process.waitpid2(pid)
439-
ensure
440438
trap(:USR1, 'DEFAULT')
441439
end
440+
441+
# the query ran uninterrupted
442+
expect(mark.fetch(:QUERY_END) - mark.fetch(:QUERY_START)).to be_within(0.02).of(query_time)
443+
# signals fired while the query was running
444+
expect(mark.fetch(:USR1)).to be_between(mark.fetch(:QUERY_START), mark.fetch(:QUERY_END))
442445
end
443446

444447
it "#socket should return a Fixnum (file descriptor from C)" do

0 commit comments

Comments
 (0)