|
25 | 25 | it 'adds measurable delay when jitter is enabled' do |
26 | 26 | skip 'failCommand fail point is not available' unless fail_command_available? |
27 | 27 |
|
28 | | - no_backoff_time = with_fixed_jitter(0) do |
| 28 | + no_backoff_sleeps = [] |
| 29 | + with_fixed_jitter(0) do |
29 | 30 | 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) } |
32 | 34 | end |
33 | 35 | end |
34 | 36 | end |
35 | 37 |
|
36 | | - with_backoff_time = with_fixed_jitter(1) do |
| 38 | + with_backoff_sleeps = [] |
| 39 | + with_fixed_jitter(1) do |
37 | 40 | 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) } |
40 | 44 | end |
41 | 45 | end |
42 | 46 | end |
43 | 47 |
|
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) |
46 | 52 | end |
47 | 53 |
|
48 | 54 | private |
49 | 55 |
|
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 | | - |
60 | 56 | def with_fixed_jitter(value) |
61 | 57 | allow(Random).to receive(:rand).and_return(value) |
62 | 58 | yield |
|
0 commit comments