Ejemplo n.º 1
0
 private int CalculateTime(bool timeout)
 {
     return((int)TransactionExecutorReflector.__transactionTimeout().TotalSeconds + (timeout ? 10 : -10));
 }
Ejemplo n.º 2
0
        public void WithTransaction_commit_after_callback_processing_should_be_processed_with_expected_result(
            bool[] isRetryAttemptsWithTimeout, // the array length should be the same with a number of failed attempts from `commitTransactionErrorStates`
            WithTransactionErrorState[] commitTransactionErrorStates,
            bool shouldExceptionBeThrown,
            int expectedCommitTransactionAttempts,
            bool?expectedFullTransactionBeRetriedState,
            bool async)
        {
            var now       = DateTime.UtcNow;
            var mockClock = CreateClockMock(now, isRetryAttemptsWithTimeout, false);

            var mockCoreSession = CreateCoreSessionMock();

            // Initialize commit result
            if (async)
            {
                var mockCommitProcessing = mockCoreSession.SetupSequence(c => c.CommitTransactionAsync(It.IsAny <CancellationToken>()));
                foreach (var commitTransactionErrorState in commitTransactionErrorStates)
                {
                    if (commitTransactionErrorState == WithTransactionErrorState.NoError)
                    {
                        mockCommitProcessing.Returns(Task.FromResult(0));
                    }
                    else
                    {
                        var commitException = PrepareException(commitTransactionErrorState);
                        mockCommitProcessing.Throws(commitException);
                    }
                }
            }
            else
            {
                var mockCommitProcessing = mockCoreSession.SetupSequence(c => c.CommitTransaction(It.IsAny <CancellationToken>()));
                foreach (var commitTransactionErrorState in commitTransactionErrorStates)
                {
                    if (commitTransactionErrorState == WithTransactionErrorState.NoError)
                    {
                        mockCommitProcessing.Pass();
                    }
                    else
                    {
                        var commitException = PrepareException(commitTransactionErrorState);
                        mockCommitProcessing.Throws(commitException);
                    }
                }
            }

            var subject = CreateSubject(coreSession: mockCoreSession.Object, clock: mockClock.Object);

            // Commit processing
            if (async)
            {
                if (shouldExceptionBeThrown)
                {
                    Assert.ThrowsAnyAsync <MongoException>(() => TransactionExecutorReflector.CommitWithRetriesAsync(subject, now, mockClock.Object, CancellationToken.None)).GetAwaiter().GetResult();
                }
                else
                {
                    var result = TransactionExecutorReflector.CommitWithRetriesAsync(subject, now, mockClock.Object, CancellationToken.None).Result;
                    expectedFullTransactionBeRetriedState.Should().Be(result);
                }

                mockCoreSession.Verify(handle => handle.CommitTransactionAsync(It.IsAny <CancellationToken>()), Times.Exactly(expectedCommitTransactionAttempts));
            }
            else
            {
                if (shouldExceptionBeThrown)
                {
                    Assert.ThrowsAny <MongoException>(() => TransactionExecutorReflector.CommitWithRetries(subject, now, mockClock.Object, CancellationToken.None));
                }
                else
                {
                    var result = TransactionExecutorReflector.CommitWithRetries(subject, now, mockClock.Object, CancellationToken.None);
                    expectedFullTransactionBeRetriedState.Should().Be(result);
                }

                mockCoreSession.Verify(handle => handle.CommitTransaction(It.IsAny <CancellationToken>()), Times.Exactly(expectedCommitTransactionAttempts));
            }
        }