public async Task RetriableExecute_RetryOnExceptions(Driver.RetryPolicy policy, IList <Exception> exceptions, Type expectedExceptionType, Type innerExceptionType, Times funcCalledTimes, Times newSessionCalledTimes, Times nextSessionCalledTimes, Times retryActionCalledTimes) { var handler = (RetryHandler)QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance); var func = new Mock <Func <Task <int> > >(); var newSession = new Mock <Func <Task> >(); var nextSession = new Mock <Func <Task> >(); var retry = new Mock <Func <int, Task> >(); var seq = func.SetupSequence(f => f.Invoke()); foreach (var ex in exceptions) { seq = seq.ThrowsAsync(ex); } seq.ReturnsAsync(1); try { await handler.RetriableExecute <int>(func.Object, policy, newSession.Object, nextSession.Object, retry.Object); Assert.IsNull(expectedExceptionType); } catch (Exception e) { Assert.IsNotNull(expectedExceptionType); Assert.IsInstanceOfType(e, expectedExceptionType); if (innerExceptionType != null) { Assert.IsInstanceOfType(e.InnerException, innerExceptionType); } } func.Verify(f => f.Invoke(), funcCalledTimes); newSession.Verify(r => r.Invoke(), newSessionCalledTimes); nextSession.Verify(r => r.Invoke(), nextSessionCalledTimes); retry.Verify(r => r.Invoke(It.IsAny <int>()), retryActionCalledTimes); }
public void Dispose_DisposeSessionPool_DestroyAllSessions() { var mockCreator = new Mock <Func <Session> >(); var mockSession1 = new Mock <Session>(null, null, null, null, null); var mockSession2 = new Mock <Session>(null, null, null, null, null); mockCreator.SetupSequence(x => x()).Returns(mockSession1.Object).Returns(mockSession2.Object); 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.Dispose(); mockSession1.Verify(s => s.End(), Times.Exactly(1)); mockSession2.Verify(s => s.End(), Times.Exactly(1)); }
public void SetupTest() { mockClient = new Mock <AmazonQLDBSessionClient>(); var sendCommandResponse = new SendCommandResponse { StartSession = new StartSessionResult { SessionToken = "testToken" }, StartTransaction = new StartTransactionResult { TransactionId = "testTransactionIdddddd" }, ExecuteStatement = new ExecuteStatementResult { FirstPage = new Page { NextPageToken = null, Values = new List <ValueHolder>() } }, CommitTransaction = new CommitTransactionResult { CommitDigest = new MemoryStream(digest) }, ResponseMetadata = new ResponseMetadata { RequestId = "testId" } }; mockClient.Setup(x => x.SendCommandAsync(It.IsAny <SendCommandRequest>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(sendCommandResponse)); builder = QldbDriver.Builder() .WithLedger("testLedger") .WithRetryLogging() .WithLogger(NullLogger.Instance) .WithAWSCredentials(new Mock <AWSCredentials>().Object) .WithQLDBSessionConfig(new AmazonQLDBSessionConfig()); }
public void GetSession_GetTwoSessionsFromPoolOfOneAfterFirstOneDisposed_NoThrowOnSecondGet() { var mockCreator = new Mock <Func <Session> >(); var mockSession = new Mock <Session>(null, null, null, null, null); mockCreator.Setup(x => x()).Returns(mockSession.Object); mockSession.Setup(x => x.StartTransaction()).Returns(new StartTransactionResult { TransactionId = "testTransactionIdddddd" }); var pool = new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 1, NullLogger.Instance); var returnedSession = pool.GetSession(); returnedSession.Release(); var nextSession = pool.GetSession(); Assert.IsNotNull(nextSession); nextSession.StartTransaction(); mockCreator.Verify(x => x(), Times.Exactly(1)); }
public async Task Dispose_DisposeSessionPool_DestroyAllSessions() { var mockCreator = new Mock <Func <Task <Session> > >(); var mockSession1 = new Mock <Session>(null, null, null, null, null); var mockSession2 = new Mock <Session>(null, null, null, null, null); mockCreator.SetupSequence(x => x()).ReturnsAsync(mockSession1.Object).ReturnsAsync(mockSession2.Object); 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.DisposeAsync(); mockSession1.Verify(s => s.End(It.IsAny <CancellationToken>()), Times.Exactly(1)); mockSession2.Verify(s => s.End(It.IsAny <CancellationToken>()), Times.Exactly(1)); }
public void Execute_NoException_ReturnFunctionValue() { 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.Setup(f => f.Invoke(It.IsAny <TransactionExecutor>())).Returns(1); var mockRetry = new Mock <Action <int> >(); var pool = new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 1, NullLogger.Instance); pool.Execute(mockFunction.Object, Driver.RetryPolicy.Builder().Build(), retry.Object); mockCreator.Verify(x => x(), Times.Once); retry.Verify(r => r.Invoke(It.IsAny <int>()), Times.Never); }
public async Task Execute_ExceptionOnExecute_ShouldOnlyRetryOnISEAndTAOE(Exception exception, bool expectThrow) { var statement = "DELETE FROM table;"; var h1 = QldbHash.ToQldbHash(TestTransactionId); h1 = Transaction.Dot(h1, statement, new List <IIonValue> { }); var sendCommandResponseWithStartSession = new SendCommandResponse { StartTransaction = new StartTransactionResult { TransactionId = TestTransactionId }, ResponseMetadata = new ResponseMetadata { RequestId = "testId" } }; var sendCommandResponseExecute = new SendCommandResponse { ExecuteStatement = new ExecuteStatementResult { FirstPage = new Page { NextPageToken = null } }, ResponseMetadata = new ResponseMetadata { RequestId = "testId" } }; var sendCommandResponseCommit = new SendCommandResponse { CommitTransaction = new CommitTransactionResult { TransactionId = TestTransactionId, CommitDigest = new MemoryStream(h1.Hash), }, ResponseMetadata = new ResponseMetadata { RequestId = "testId" } }; var mockCreator = new Mock <Func <Task <Session> > >(); var mockSession = new Mock <Session>(null, null, null, null, null); mockSession.Setup(x => x.StartTransaction(It.IsAny <CancellationToken>())).ReturnsAsync(sendCommandResponseWithStartSession.StartTransaction); mockSession.SetupSequence(x => x.ExecuteStatement(It.IsAny <String>(), It.IsAny <String>(), It.IsAny <List <IIonValue> >(), It.IsAny <CancellationToken>())) .ThrowsAsync(exception) .ThrowsAsync(exception) .ReturnsAsync(sendCommandResponseExecute.ExecuteStatement); mockSession.Setup(x => x.CommitTransaction(It.IsAny <String>(), It.IsAny <MemoryStream>(), It.IsAny <CancellationToken>())) .ReturnsAsync(sendCommandResponseCommit.CommitTransaction); mockCreator.Setup(x => x()).ReturnsAsync(mockSession.Object); var driver = new QldbDriver( new SessionPool(mockCreator.Object, QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 4, NullLogger.Instance)); try { await driver.Execute(txn => txn.Execute(statement)); } catch (Exception e) { Assert.AreEqual(exception, e); Assert.IsTrue(expectThrow); return; } Assert.IsFalse(expectThrow); }
public async Task TestListTableNamesLists() { var factory = new ValueFactory(); var tables = new List <string>() { "table1", "table2" }; var ions = tables.Select(t => CreateValueHolder(factory.NewString(t))).ToList(); var h1 = QldbHash.ToQldbHash(TestTransactionId); h1 = Transaction.Dot(h1, QldbDriver.TableNameQuery, new List <IIonValue> { }); var sendCommandResponseWithStartSession = new SendCommandResponse { StartSession = new StartSessionResult { SessionToken = "testToken" }, ResponseMetadata = new ResponseMetadata { RequestId = "testId" } }; var sendCommandResponseStartTransaction = new SendCommandResponse { StartTransaction = new StartTransactionResult { TransactionId = TestTransactionId }, ResponseMetadata = new ResponseMetadata { RequestId = "testId" } }; var sendCommandResponseExecute = new SendCommandResponse { ExecuteStatement = new ExecuteStatementResult { FirstPage = new Page { NextPageToken = null, Values = ions } }, ResponseMetadata = new ResponseMetadata { RequestId = "testId" } }; var sendCommandResponseCommit = new SendCommandResponse { CommitTransaction = new CommitTransactionResult { CommitDigest = new MemoryStream(h1.Hash), TransactionId = TestTransactionId }, ResponseMetadata = new ResponseMetadata { RequestId = "testId" } }; mockClient.SetupSequence(x => x.SendCommandAsync(It.IsAny <SendCommandRequest>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(sendCommandResponseWithStartSession)) .Returns(Task.FromResult(sendCommandResponseStartTransaction)) .Returns(Task.FromResult(sendCommandResponseExecute)) .Returns(Task.FromResult(sendCommandResponseCommit)); var driver = new QldbDriver( new SessionPool(() => Session.StartSession("ledgerName", mockClient.Object, NullLogger.Instance), QldbDriverBuilder.CreateDefaultRetryHandler(NullLogger.Instance), 4, NullLogger.Instance)); var result = await driver.ListTableNames(); Assert.IsNotNull(result); CollectionAssert.AreEqual(tables, result.ToList()); }
public void SetupTest() { builder = QldbDriver.Builder().WithLedger("testLedger"); }