public virtual void TestFailoverOnStandbyException() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(NewFlipFlopProxyProvider(), RetryPolicies.FailoverOnNetworkException(1)); Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningString ()); try { unreliable.SucceedsOnceThenFailsReturningString(); NUnit.Framework.Assert.Fail("should not have succeeded twice"); } catch (UnreliableInterface.UnreliableException e) { // Make sure there was no failover on normal exception. Assert.Equal("impl1", e.Message); } unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface>(NewFlipFlopProxyProvider (UnreliableImplementation.TypeOfExceptionToFailWith.StandbyException, UnreliableImplementation.TypeOfExceptionToFailWith .UnreliableException), RetryPolicies.FailoverOnNetworkException(1)); Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningString ()); // Make sure we fail over since the first implementation threw a StandbyException Assert.Equal("impl2", unreliable.SucceedsOnceThenFailsReturningString ()); }
public virtual void TestFailoverOnNetworkExceptionIdempotentOperation() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(NewFlipFlopProxyProvider(UnreliableImplementation.TypeOfExceptionToFailWith.IoException , UnreliableImplementation.TypeOfExceptionToFailWith.UnreliableException), RetryPolicies .FailoverOnNetworkException(1)); Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningString ()); try { unreliable.SucceedsOnceThenFailsReturningString(); NUnit.Framework.Assert.Fail("should not have succeeded twice"); } catch (IOException e) { // Make sure we *don't* fail over since the first implementation threw an // IOException and this method is not idempotent Assert.Equal("impl1", e.Message); } Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningStringIdempotent ()); // Make sure we fail over since the first implementation threw an // IOException and this method is idempotent. Assert.Equal("impl2", unreliable.SucceedsOnceThenFailsReturningStringIdempotent ()); }
public virtual void TestRetryForever() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(unreliableImpl, RetryPolicies.RetryForever); unreliable.AlwaysSucceeds(); unreliable.FailsOnceThenSucceeds(); unreliable.FailsTenTimesThenSucceeds(); }
public virtual void TestSucceedsTenTimesThenFailOver() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(NewFlipFlopProxyProvider(), new TestFailoverProxy.FailOverOnceOnAnyExceptionPolicy ()); for (int i = 0; i < 10; i++) { Assert.Equal("impl1", unreliable.SucceedsTenTimesThenFailsReturningString ()); } Assert.Equal("impl2", unreliable.SucceedsTenTimesThenFailsReturningString ()); }
public virtual void TestTryOnceThenFail() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(unreliableImpl, RetryPolicies.TryOnceThenFail); unreliable.AlwaysSucceeds(); try { unreliable.FailsOnceThenSucceeds(); NUnit.Framework.Assert.Fail("Should fail"); } catch (UnreliableInterface.UnreliableException) { } }
public virtual void TestNeverFailOver() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(NewFlipFlopProxyProvider(), RetryPolicies.TryOnceThenFail); unreliable.SucceedsOnceThenFailsReturningString(); try { unreliable.SucceedsOnceThenFailsReturningString(); NUnit.Framework.Assert.Fail("should not have succeeded twice"); } catch (UnreliableInterface.UnreliableException e) { Assert.Equal("impl1", e.Message); } }
public virtual void TestExceptionPropagatedForNonIdempotentVoid() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(NewFlipFlopProxyProvider(UnreliableImplementation.TypeOfExceptionToFailWith.IoException , UnreliableImplementation.TypeOfExceptionToFailWith.UnreliableException), RetryPolicies .FailoverOnNetworkException(1)); try { unreliable.NonIdempotentVoidFailsIfIdentifierDoesntMatch("impl2"); NUnit.Framework.Assert.Fail("did not throw an exception"); } catch (Exception) { } }
public virtual void TestExponentialRetry() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(unreliableImpl, RetryPolicies.ExponentialBackoffRetry(5, 1L, TimeUnit.Nanoseconds )); unreliable.AlwaysSucceeds(); unreliable.FailsOnceThenSucceeds(); try { unreliable.FailsTenTimesThenSucceeds(); NUnit.Framework.Assert.Fail("Should fail"); } catch (UnreliableInterface.UnreliableException) { } }
public virtual void TestRetryUpToMaximumCountWithProportionalSleep() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(unreliableImpl, RetryPolicies.RetryUpToMaximumCountWithProportionalSleep(8, 1, TimeUnit.Nanoseconds)); unreliable.AlwaysSucceeds(); unreliable.FailsOnceThenSucceeds(); try { unreliable.FailsTenTimesThenSucceeds(); NUnit.Framework.Assert.Fail("Should fail"); } catch (UnreliableInterface.UnreliableException) { } }
public virtual void TestRetryByRemoteException() { IDictionary <Type, RetryPolicy> exceptionToPolicyMap = Collections.SingletonMap <Type, RetryPolicy>(typeof(UnreliableInterface.FatalException), RetryPolicies.TryOnceThenFail ); UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(unreliableImpl, RetryPolicies.RetryByRemoteException(RetryPolicies.RetryForever , exceptionToPolicyMap)); try { unreliable.AlwaysFailsWithRemoteFatalException(); NUnit.Framework.Assert.Fail("Should fail"); } catch (RemoteException) { } }
public virtual void TestExpectedIOException() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(NewFlipFlopProxyProvider(UnreliableImplementation.TypeOfExceptionToFailWith.RemoteException , UnreliableImplementation.TypeOfExceptionToFailWith.UnreliableException), RetryPolicies .FailoverOnNetworkException(RetryPolicies.TryOnceThenFail, 10, 1000, 10000)); try { unreliable.FailsIfIdentifierDoesntMatch("no-such-identifier"); NUnit.Framework.Assert.Fail("Should have thrown *some* exception"); } catch (Exception e) { Assert.True("Expected IOE but got " + e.GetType(), e is IOException ); } }
public virtual void TestRpcInvocation() { // For a proxy method should return true UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(unreliableImpl, RetryPolicies.RetryForever); Assert.True(RetryInvocationHandler.IsRpcInvocation(unreliable)); // Embed the proxy in ProtocolTranslator ProtocolTranslator xlator = new _ProtocolTranslator_83(unreliable); // For a proxy wrapped in ProtocolTranslator method should return true Assert.True(RetryInvocationHandler.IsRpcInvocation(xlator)); // Ensure underlying proxy was looked at Assert.Equal(xlator.ToString(), "1"); // For non-proxy the method must return false NUnit.Framework.Assert.IsFalse(RetryInvocationHandler.IsRpcInvocation(new object( ))); }
public virtual void TestFailoverBetweenMultipleStandbys() { long millisToSleep = 10000; UnreliableImplementation impl1 = new UnreliableImplementation("impl1", UnreliableImplementation.TypeOfExceptionToFailWith .StandbyException); TestFailoverProxy.FlipFlopProxyProvider <UnreliableInterface> proxyProvider = new TestFailoverProxy.FlipFlopProxyProvider <UnreliableInterface>(typeof(UnreliableInterface ), impl1, new UnreliableImplementation("impl2", UnreliableImplementation.TypeOfExceptionToFailWith .StandbyException)); UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(proxyProvider, RetryPolicies.FailoverOnNetworkException(RetryPolicies.TryOnceThenFail , 10, 1000, 10000)); new _Thread_328(millisToSleep, impl1).Start(); string result = unreliable.FailsIfIdentifierDoesntMatch("renamed-impl1"); Assert.Equal("renamed-impl1", result); }
public virtual void TestSuccedsOnceThenFailOver() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(NewFlipFlopProxyProvider(), new TestFailoverProxy.FailOverOnceOnAnyExceptionPolicy ()); Assert.Equal("impl1", unreliable.SucceedsOnceThenFailsReturningString ()); Assert.Equal("impl2", unreliable.SucceedsOnceThenFailsReturningString ()); try { unreliable.SucceedsOnceThenFailsReturningString(); NUnit.Framework.Assert.Fail("should not have succeeded more than twice"); } catch (UnreliableInterface.UnreliableException) { } }
public virtual void TestConcurrentMethodFailures() { TestFailoverProxy.FlipFlopProxyProvider <UnreliableInterface> proxyProvider = new TestFailoverProxy.FlipFlopProxyProvider <UnreliableInterface>(typeof(UnreliableInterface ), new TestFailoverProxy.SynchronizedUnreliableImplementation("impl1", UnreliableImplementation.TypeOfExceptionToFailWith .StandbyException, 2), new UnreliableImplementation("impl2", UnreliableImplementation.TypeOfExceptionToFailWith .StandbyException)); UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(proxyProvider, RetryPolicies.FailoverOnNetworkException(10)); TestFailoverProxy.ConcurrentMethodThread t1 = new TestFailoverProxy.ConcurrentMethodThread (unreliable); TestFailoverProxy.ConcurrentMethodThread t2 = new TestFailoverProxy.ConcurrentMethodThread (unreliable); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Assert.Equal("impl2", t1.result); Assert.Equal("impl2", t2.result); Assert.Equal(1, proxyProvider.GetFailoversOccurred()); }
public virtual void TestRetryInterruptible() { UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface >(unreliableImpl, RetryPolicies.RetryUpToMaximumTimeWithFixedSleep(10, 10, TimeUnit .Seconds)); CountDownLatch latch = new CountDownLatch(1); AtomicReference <Thread> futureThread = new AtomicReference <Thread >(); ExecutorService exec = Executors.NewSingleThreadExecutor(); Future <Exception> future = exec.Submit(new _Callable_216(futureThread, latch, unreliable )); latch.Await(); Thread.Sleep(1000); // time to fail and sleep Assert.True(futureThread.Get().IsAlive()); futureThread.Get().Interrupt(); Exception e = future.Get(1, TimeUnit.Seconds); // should return immediately NUnit.Framework.Assert.IsNotNull(e); Assert.Equal(typeof(Exception), e.GetType()); Assert.Equal("sleep interrupted", e.Message); }
/// <summary> /// <p> /// Create a proxy for an interface of an implementation class /// using the same retry policy for each method in the interface. /// </summary> /// <remarks> /// <p> /// Create a proxy for an interface of an implementation class /// using the same retry policy for each method in the interface. /// </p> /// </remarks> /// <param name="iface">the interface that the retry will implement</param> /// <param name="implementation">the instance whose methods should be retried</param> /// <param name="retryPolicy">the policy for retrying method call failures</param> /// <returns>the retry proxy</returns> public static object Create <T>(T implementation, RetryPolicy retryPolicy) { System.Type iface = typeof(T); return(RetryProxy.Create(iface, new DefaultFailoverProxyProvider <T>(iface, implementation ), retryPolicy)); }