|
| 1 | +# frozen_string_literal: true |
| 2 | +# rubocop:todo all |
| 3 | + |
| 4 | +require 'spec_helper' |
| 5 | + |
| 6 | +describe Mongo::Session do |
| 7 | + require_topology :replica_set |
| 8 | + |
| 9 | + describe 'transactions convenient API prose tests' do |
| 10 | + let(:client) { authorized_client } |
| 11 | + let(:admin_client) { authorized_client.use('admin') } |
| 12 | + let(:collection) { client['session-transaction-prose-test'] } |
| 13 | + |
| 14 | + before do |
| 15 | + collection.delete_many |
| 16 | + end |
| 17 | + |
| 18 | + after do |
| 19 | + disable_fail_command |
| 20 | + end |
| 21 | + |
| 22 | + # Prose test from: |
| 23 | + # specifications/source/transactions-convenient-api/tests/README.md |
| 24 | + # ### Retry Backoff is Enforced |
| 25 | + it 'adds measurable delay when jitter is enabled' do |
| 26 | + skip 'failCommand fail point is not available' unless fail_command_available? |
| 27 | + |
| 28 | + no_backoff_time = with_fixed_jitter(0) do |
| 29 | + with_commit_failures(13) do |
| 30 | + measure_with_transaction_time do |session| |
| 31 | + collection.insert_one({}, session: session) |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + with_backoff_time = with_fixed_jitter(1) do |
| 37 | + with_commit_failures(13) do |
| 38 | + measure_with_transaction_time do |session| |
| 39 | + collection.insert_one({}, session: session) |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 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) |
| 46 | + end |
| 47 | + |
| 48 | + private |
| 49 | + |
| 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 | + def with_fixed_jitter(value) |
| 61 | + allow(Random).to receive(:rand).and_return(value) |
| 62 | + yield |
| 63 | + end |
| 64 | + |
| 65 | + def with_commit_failures(times) |
| 66 | + admin_client.command( |
| 67 | + configureFailPoint: 'failCommand', |
| 68 | + mode: { times: times }, |
| 69 | + data: { |
| 70 | + failCommands: ['commitTransaction'], |
| 71 | + errorCode: 251, |
| 72 | + }, |
| 73 | + ) |
| 74 | + yield |
| 75 | + ensure |
| 76 | + disable_fail_command |
| 77 | + end |
| 78 | + |
| 79 | + def disable_fail_command |
| 80 | + admin_client.command(configureFailPoint: 'failCommand', mode: 'off') |
| 81 | + rescue Mongo::Error |
| 82 | + # Ignore cleanup failures. |
| 83 | + end |
| 84 | + |
| 85 | + def fail_command_available? |
| 86 | + admin_client.command(configureFailPoint: 'failCommand', mode: 'off') |
| 87 | + true |
| 88 | + rescue Mongo::Error |
| 89 | + false |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments