예제 #1
0
        public async Task Execute_HaveOCCExceptionsAndAbortFailuresWithinRetryLimit_Succeeded()
        {
            var mockCreator = new Mock <Func <Task <Session> > >();
            var mockSession = new Mock <Session>(null, null, null, null, null);

            mockCreator.Setup(x => x()).ReturnsAsync(mockSession.Object);
            var retry = new Mock <Func <int, Task> >();

            var sendCommandResponseStart = new StartTransactionResult
            {
                TransactionId = "testTransactionIdddddd"
            };

            var h1 = QldbHash.ToQldbHash("testTransactionIdddddd");

            var sendCommandResponseCommit = new CommitTransactionResult
            {
                CommitDigest  = new MemoryStream(h1.Hash),
                TransactionId = "testTransactionIdddddd"
            };

            var abortResponse    = new AbortTransactionResult {
            };
            var serviceException = new AmazonServiceException();

            mockSession.Setup(x => x.StartTransaction(It.IsAny <CancellationToken>()))
            .ReturnsAsync(sendCommandResponseStart);
            mockSession.Setup(x => x.CommitTransaction(It.IsAny <string>(), It.IsAny <MemoryStream>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(sendCommandResponseCommit);
            mockSession.SetupSequence(x => x.AbortTransaction(It.IsAny <CancellationToken>()))
            .ThrowsAsync(serviceException)
            .ReturnsAsync(abortResponse)
            .ThrowsAsync(serviceException);

            var mockFunction = new Mock <Func <TransactionExecutor, Task <int> > >();

            mockFunction.SetupSequence(f => f.Invoke(It.IsAny <TransactionExecutor>()))
            .ThrowsAsync(new OccConflictException("occ"))
            .ThrowsAsync(new OccConflictException("occ"))
            .ThrowsAsync(new OccConflictException("occ"))
            .ReturnsAsync(1);
            var mockRetry = new Mock <Action <int> >();

            var pool = new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 2, NullLogger.Instance);

            var session1 = await pool.GetSession();

            var session2 = await pool.GetSession();

            session1.Release();
            session2.Release();

            await pool.Execute(mockFunction.Object, Driver.RetryPolicy.Builder().Build(), retry.Object);

            mockCreator.Verify(x => x(), Times.Exactly(2));
            retry.Verify(r => r.Invoke(It.IsAny <int>()), Times.Exactly(3));
            Assert.AreEqual(2, pool.AvailablePermit());
        }
        public void Execute_HaveISE_Succeeded()
        {
            var mockCreator = new Mock <Func <Session> >();
            var mockSession = new Mock <Session>(null, null, null, null, null);

            mockCreator.Setup(x => x()).Returns(mockSession.Object);
            var retry = new Mock <Action <int> >();

            var sendCommandResponseStart = new StartTransactionResult
            {
                TransactionId = "testTransactionIdddddd"
            };

            var h1 = QldbHash.ToQldbHash("testTransactionIdddddd");

            var sendCommandResponseCommit = new CommitTransactionResult
            {
                CommitDigest  = new MemoryStream(h1.Hash),
                TransactionId = "testTransactionIdddddd"
            };

            mockSession.Setup(x => x.StartTransaction())
            .Returns(sendCommandResponseStart);
            mockSession.Setup(x => x.CommitTransaction(It.IsAny <string>(), It.IsAny <MemoryStream>()))
            .Returns(sendCommandResponseCommit);

            var mockFunction = new Mock <Func <TransactionExecutor, int> >();

            mockFunction.SetupSequence(f => f.Invoke(It.IsAny <TransactionExecutor>()))
            .Throws(new InvalidSessionException("invalid"))
            .Throws(new InvalidSessionException("invalid"))
            .Throws(new InvalidSessionException("invalid"))
            .Throws(new InvalidSessionException("invalid"))
            .Returns(1);
            var mockRetry = new Mock <Action <int> >();

            var pool = new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 2, NullLogger.Instance);

            var session1 = pool.GetSession();
            var session2 = pool.GetSession();

            session1.Release();
            session2.Release();

            pool.Execute(mockFunction.Object, Driver.RetryPolicy.Builder().Build(), retry.Object);

            mockCreator.Verify(x => x(), Times.Exactly(6));
            retry.Verify(r => r.Invoke(It.IsAny <int>()), Times.Exactly(4));
            Assert.AreEqual(2, pool.AvailablePermit());
        }