public void TestExecute_RetryOnExceptions( Driver.RetryPolicy policy, IList <Exception> exceptions, bool expectThrow, Times retryActionCalledTimes) { string statement = "DELETE FROM table;"; var h1 = QldbHash.ToQldbHash(TestTransactionId); h1 = Transaction.Dot(h1, statement, new List <IIonValue> { }); mockClient.QueueResponse(StartSessionResponse(TestRequestId)); mockClient.QueueResponse(StartTransactionResponse(TestTransactionId, TestRequestId)); foreach (var ex in exceptions) { mockClient.QueueResponse(ex); // OccConflictException reuses the session so no need for another start session. if (ex is not OccConflictException) { mockClient.QueueResponse(StartSessionResponse(TestRequestId)); } mockClient.QueueResponse(StartTransactionResponse(TestTransactionId, TestRequestId)); } mockClient.QueueResponse(ExecuteResponse(TestRequestId, null)); mockClient.QueueResponse(CommitResponse(TestTransactionId, TestRequestId, h1.Hash)); var retry = new Mock <Action <int> >(); try { testDriver.Execute(txn => txn.Execute(statement), policy, retry.Object); Assert.IsFalse(expectThrow); } catch (Exception e) { Assert.IsTrue(expectThrow); Assert.IsTrue(exceptions.Count > 0); // The exception should be the same type as the last exception in our exception list. Exception finalException = exceptions[exceptions.Count - 1]; Type expectedExceptionType = finalException.GetType(); Assert.IsInstanceOfType(e, expectedExceptionType); } retry.Verify(r => r.Invoke(It.IsAny <int>()), retryActionCalledTimes); mockClient.Clear(); }
public void 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 <int> >(); var newSession = new Mock <Action>(); var nextSession = new Mock <Action>(); var retry = new Mock <Action <int> >(); var seq = func.SetupSequence(f => f.Invoke()); foreach (var ex in exceptions) { seq = seq.Throws(ex); } seq.Returns(1); try { 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); }