Skip to content

Commit cdd512f

Browse files
Fix timing flakiness in transaction backoff prose test
Replace wall-clock measurement with sleep spy to verify requested backoff durations rather than actual elapsed time. This eliminates CI failures on loaded machines where OS sleep overshoots the 0.5s tolerance.
1 parent 65aa4cc commit cdd512f

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

spec/mongo/session_transaction_prose_spec.rb

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,34 @@
2525
it 'adds measurable delay when jitter is enabled' do
2626
skip 'failCommand fail point is not available' unless fail_command_available?
2727

28-
no_backoff_time = with_fixed_jitter(0) do
28+
no_backoff_sleeps = []
29+
with_fixed_jitter(0) do
2930
with_commit_failures(13) do
30-
measure_with_transaction_time do |session|
31-
collection.insert_one({}, session: session)
31+
client.start_session do |session|
32+
allow(session).to receive(:sleep) { |d| no_backoff_sleeps << d }
33+
session.with_transaction { collection.insert_one({}, session: session) }
3234
end
3335
end
3436
end
3537

36-
with_backoff_time = with_fixed_jitter(1) do
38+
with_backoff_sleeps = []
39+
with_fixed_jitter(1) do
3740
with_commit_failures(13) do
38-
measure_with_transaction_time do |session|
39-
collection.insert_one({}, session: session)
41+
client.start_session do |session|
42+
allow(session).to receive(:sleep) { |d| with_backoff_sleeps << d }
43+
session.with_transaction { collection.insert_one({}, session: session) }
4044
end
4145
end
4246
end
4347

44-
# Sum of 13 backoffs per spec is approximately 1.8 seconds.
45-
expect(with_backoff_time).to be_within(0.5).of(no_backoff_time + 1.8)
48+
# With jitter=0 all requested sleeps are zero; with jitter=1 they sum to
49+
# approximately 1.8 seconds (sum of 13 exponential backoffs, per spec).
50+
expect(no_backoff_sleeps.sum).to eq(0)
51+
expect(with_backoff_sleeps.sum).to be_within(0.05).of(1.8)
4652
end
4753

4854
private
4955

50-
def measure_with_transaction_time
51-
start_time = Mongo::Utils.monotonic_time
52-
client.start_session do |session|
53-
session.with_transaction do
54-
yield(session)
55-
end
56-
end
57-
Mongo::Utils.monotonic_time - start_time
58-
end
59-
6056
def with_fixed_jitter(value)
6157
allow(Random).to receive(:rand).and_return(value)
6258
yield

0 commit comments

Comments
 (0)