|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +# Retryable Writes Prose Test 6: Test error propagation after |
| 6 | +# encountering multiple errors. |
| 7 | +# |
| 8 | +# Spec reference: |
| 9 | +# specifications/source/retryable-writes/tests/README.md |
| 10 | +# "6. Test error propagation after encountering multiple errors." |
| 11 | +describe 'Retryable writes prose test 6: error propagation' do |
| 12 | + require_topology :replica_set |
| 13 | + min_server_version '6.0' |
| 14 | + |
| 15 | + let(:client) do |
| 16 | + authorized_client.with(retry_writes: true) |
| 17 | + end |
| 18 | + |
| 19 | + let(:admin_client) { client.use(:admin) } |
| 20 | + |
| 21 | + let(:collection) { client['error-propagation-prose-test'] } |
| 22 | + |
| 23 | + after do |
| 24 | + admin_client.command(configureFailPoint: 'failCommand', mode: 'off') |
| 25 | + rescue Mongo::Error |
| 26 | + # Ignore cleanup failures. |
| 27 | + end |
| 28 | + |
| 29 | + # Case 1: Test that drivers return the correct error when receiving |
| 30 | + # only errors without NoWritesPerformed. |
| 31 | + context 'Case 1: only errors without NoWritesPerformed' do |
| 32 | + it 'returns the most recent error (10107)' do |
| 33 | + # Step 2: Configure a fail point with error code 91 and |
| 34 | + # RetryableError + SystemOverloadedError labels. |
| 35 | + admin_client.command( |
| 36 | + configureFailPoint: 'failCommand', |
| 37 | + mode: { times: 1 }, |
| 38 | + data: { |
| 39 | + failCommands: [ 'insert' ], |
| 40 | + errorCode: 91, |
| 41 | + errorLabels: %w[RetryableError SystemOverloadedError] |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + # Step 3: Via CommandFailedEvent, configure a fail point with |
| 46 | + # error code 10107 once the 91 error is observed. |
| 47 | + failpoint_set = false |
| 48 | + subscriber = Mrss::EventSubscriber.new |
| 49 | + client.subscribe(Mongo::Monitoring::COMMAND, subscriber) |
| 50 | + |
| 51 | + allow(subscriber).to receive(:failed).and_wrap_original do |m, event| |
| 52 | + m.call(event) |
| 53 | + if !failpoint_set && event.command_name == 'insert' |
| 54 | + failpoint_set = true |
| 55 | + admin_client.command( |
| 56 | + configureFailPoint: 'failCommand', |
| 57 | + mode: 'alwaysOn', |
| 58 | + data: { |
| 59 | + failCommands: [ 'insert' ], |
| 60 | + errorCode: 10_107, |
| 61 | + errorLabels: %w[RetryableError SystemOverloadedError] |
| 62 | + } |
| 63 | + ) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + # Step 4: Attempt an insertOne. Assert error code is 10107. |
| 68 | + error = nil |
| 69 | + begin |
| 70 | + collection.insert_one(x: 1) |
| 71 | + rescue Mongo::Error::OperationFailure => e |
| 72 | + error = e |
| 73 | + end |
| 74 | + |
| 75 | + expect(error).not_to be_nil |
| 76 | + expect(error.code).to eq(10_107) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # Case 2: Test that drivers return the correct error when receiving |
| 81 | + # only errors with NoWritesPerformed. |
| 82 | + context 'Case 2: only errors with NoWritesPerformed' do |
| 83 | + it 'returns the first error (91)' do |
| 84 | + # Step 2: Configure a fail point with error code 91 and |
| 85 | + # RetryableError + SystemOverloadedError + NoWritesPerformed labels. |
| 86 | + admin_client.command( |
| 87 | + configureFailPoint: 'failCommand', |
| 88 | + mode: { times: 1 }, |
| 89 | + data: { |
| 90 | + failCommands: [ 'insert' ], |
| 91 | + errorCode: 91, |
| 92 | + errorLabels: %w[RetryableError SystemOverloadedError NoWritesPerformed] |
| 93 | + } |
| 94 | + ) |
| 95 | + |
| 96 | + # Step 3: Via CommandFailedEvent, configure a fail point with |
| 97 | + # error code 10107 and NoWritesPerformed once the 91 error is observed. |
| 98 | + failpoint_set = false |
| 99 | + subscriber = Mrss::EventSubscriber.new |
| 100 | + client.subscribe(Mongo::Monitoring::COMMAND, subscriber) |
| 101 | + |
| 102 | + allow(subscriber).to receive(:failed).and_wrap_original do |m, event| |
| 103 | + m.call(event) |
| 104 | + if !failpoint_set && event.command_name == 'insert' |
| 105 | + failpoint_set = true |
| 106 | + admin_client.command( |
| 107 | + configureFailPoint: 'failCommand', |
| 108 | + mode: 'alwaysOn', |
| 109 | + data: { |
| 110 | + failCommands: [ 'insert' ], |
| 111 | + errorCode: 10_107, |
| 112 | + errorLabels: %w[RetryableError SystemOverloadedError NoWritesPerformed] |
| 113 | + } |
| 114 | + ) |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + # Step 4: Attempt an insertOne. Assert error code is 91. |
| 119 | + error = nil |
| 120 | + begin |
| 121 | + collection.insert_one(x: 1) |
| 122 | + rescue Mongo::Error::OperationFailure => e |
| 123 | + error = e |
| 124 | + end |
| 125 | + |
| 126 | + expect(error).not_to be_nil |
| 127 | + expect(error.code).to eq(91) |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + # Case 3: Test that drivers return the correct error when receiving |
| 132 | + # some errors with NoWritesPerformed and some without. |
| 133 | + context 'Case 3: mixed errors with and without NoWritesPerformed' do |
| 134 | + it 'returns the error without NoWritesPerformed (91)' do |
| 135 | + # Step 2: Via CommandFailedEvent, configure a fail point with |
| 136 | + # error code 91 and NoWritesPerformed for subsequent retries. |
| 137 | + failpoint_set = false |
| 138 | + subscriber = Mrss::EventSubscriber.new |
| 139 | + client.subscribe(Mongo::Monitoring::COMMAND, subscriber) |
| 140 | + |
| 141 | + allow(subscriber).to receive(:failed).and_wrap_original do |m, event| |
| 142 | + m.call(event) |
| 143 | + if !failpoint_set && event.command_name == 'insert' |
| 144 | + failpoint_set = true |
| 145 | + admin_client.command( |
| 146 | + configureFailPoint: 'failCommand', |
| 147 | + mode: 'alwaysOn', |
| 148 | + data: { |
| 149 | + failCommands: [ 'insert' ], |
| 150 | + errorCode: 91, |
| 151 | + errorLabels: %w[RetryableError SystemOverloadedError NoWritesPerformed] |
| 152 | + } |
| 153 | + ) |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + # Step 3: Configure initial fail point with error code 91 |
| 158 | + # WITHOUT NoWritesPerformed. |
| 159 | + admin_client.command( |
| 160 | + configureFailPoint: 'failCommand', |
| 161 | + mode: { times: 1 }, |
| 162 | + data: { |
| 163 | + failCommands: [ 'insert' ], |
| 164 | + errorCode: 91, |
| 165 | + errorLabels: %w[RetryableError SystemOverloadedError] |
| 166 | + } |
| 167 | + ) |
| 168 | + |
| 169 | + # Step 4: Attempt an insertOne. Assert error code is 91 and |
| 170 | + # error does NOT contain NoWritesPerformed label. |
| 171 | + error = nil |
| 172 | + begin |
| 173 | + collection.insert_one(x: 1) |
| 174 | + rescue Mongo::Error::OperationFailure => e |
| 175 | + error = e |
| 176 | + end |
| 177 | + |
| 178 | + expect(error).not_to be_nil |
| 179 | + expect(error.code).to eq(91) |
| 180 | + expect(error.label?('NoWritesPerformed')).to be false |
| 181 | + end |
| 182 | + end |
| 183 | +end |
0 commit comments