|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +# Retryable Writes Prose Test Case 4: Test that drivers set the maximum |
| 6 | +# number of retries for all retryable write errors when an overload error |
| 7 | +# is encountered. |
| 8 | +# |
| 9 | +# Spec reference: |
| 10 | +# specifications/source/retryable-writes/tests/README.md |
| 11 | +# "Case 4: Test that drivers set the maximum number of retries for all |
| 12 | +# retryable write errors when an overload error is encountered" |
| 13 | +describe 'Retryable writes prose test case 4: overload retry count' do |
| 14 | + require_topology :replica_set |
| 15 | + min_server_version '6.0' |
| 16 | + |
| 17 | + let(:client) do |
| 18 | + authorized_client.with(retry_writes: true) |
| 19 | + end |
| 20 | + |
| 21 | + let(:admin_client) { client.use(:admin) } |
| 22 | + |
| 23 | + let(:collection) { client['overload-retry-count-writes-prose-test'] } |
| 24 | + |
| 25 | + let(:subscriber) { Mrss::EventSubscriber.new } |
| 26 | + |
| 27 | + # MAX_ADAPTIVE_RETRIES in spec terminology; the configured max overload |
| 28 | + # retries for this client (defaults to DEFAULT_MAX_RETRIES). |
| 29 | + let(:max_adaptive_retries) { client.retry_policy.max_retries } |
| 30 | + |
| 31 | + let(:insert_started_events) do |
| 32 | + subscriber.started_events.select { |e| e.command_name == 'insert' } |
| 33 | + end |
| 34 | + |
| 35 | + after do |
| 36 | + admin_client.command(configureFailPoint: 'failCommand', mode: 'off') |
| 37 | + rescue Mongo::Error |
| 38 | + # Ignore cleanup failures. |
| 39 | + end |
| 40 | + |
| 41 | + it 'makes MAX_ADAPTIVE_RETRIES + 1 total attempts' do |
| 42 | + # Step 2: Configure a fail point for insert that fires once with an |
| 43 | + # overload error (code 91, labels RetryableError + SystemOverloadedError). |
| 44 | + admin_client.command( |
| 45 | + configureFailPoint: 'failCommand', |
| 46 | + mode: { times: 1 }, |
| 47 | + data: { |
| 48 | + failCommands: %w[insert], |
| 49 | + errorCode: 91, |
| 50 | + errorLabels: %w[RetryableError SystemOverloadedError] |
| 51 | + } |
| 52 | + ) |
| 53 | + |
| 54 | + # Step 3: Via CommandFailedEvent, when the first insert error fires, |
| 55 | + # configure a second fail point for insert with a non-overload retryable |
| 56 | + # write error (code 91, labels RetryableError + RetryableWriteError), |
| 57 | + # set to alwaysOn. |
| 58 | + failpoint_set = false |
| 59 | + client.subscribe(Mongo::Monitoring::COMMAND, subscriber) |
| 60 | + |
| 61 | + allow(subscriber).to receive(:failed).and_wrap_original do |m, event| |
| 62 | + m.call(event) |
| 63 | + if !failpoint_set && event.command_name == 'insert' |
| 64 | + failpoint_set = true |
| 65 | + admin_client.command( |
| 66 | + configureFailPoint: 'failCommand', |
| 67 | + mode: 'alwaysOn', |
| 68 | + data: { |
| 69 | + failCommands: %w[insert], |
| 70 | + errorCode: 91, |
| 71 | + errorLabels: %w[RetryableError RetryableWriteError] |
| 72 | + } |
| 73 | + ) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + # Step 4: Attempt an insertOne. Expect it to fail. |
| 78 | + subscriber.clear_events! |
| 79 | + expect do |
| 80 | + collection.insert_one(x: 1) |
| 81 | + end.to raise_error(Mongo::Error::OperationFailure) |
| 82 | + |
| 83 | + # Step 5: Assert that MAX_ADAPTIVE_RETRIES + 1 total insert commands |
| 84 | + # were sent (1 initial attempt + MAX_ADAPTIVE_RETRIES retries). |
| 85 | + expect(insert_started_events.length).to eq(max_adaptive_retries + 1) |
| 86 | + end |
| 87 | +end |
0 commit comments