package org.distributeme.core.failing; import org.distributeme.core.ClientSideCallContext; import org.junit.Test; import static org.junit.Assert.assertEquals; public class FailoverAndReturnTest { @Test public void testFailingToFailoverServer(){ String myServiceId = "FOO"; String failoverServiceId = new FailoverAndReturnIn10Microseconds().getRegistrationName(myServiceId); ClientSideCallContext ctx = new ClientSideCallContext(myServiceId, "foo", null); FailoverAndReturnIn10Microseconds f = new FailoverAndReturnIn10Microseconds(); assertEquals(myServiceId, f.getServiceIdForCall(ctx)); FailDecision firstFailure = f.callFailed(ctx); assertEquals(FailDecision.Reaction.RETRYONCE, firstFailure.getReaction()); assertEquals(failoverServiceId, firstFailure.getTargetService()); //this is what the stub does ctx.setServiceId(firstFailure.getTargetService()); ctx.increaseCallCount(); assertEquals(failoverServiceId, f.getServiceIdForCall(ctx)); //the following line is outcommented because it doesn't work and breaks the test, however the undelying logic isn#t necessery wrong, because //this situation is prevented by framework (RETRY_ONCE calls aren't executed third time)). //assertEquals(FailDecision.Reaction.FAIL, f.callFailed(ctx).getReaction()); //from now on, every request should go to failover service! ClientSideCallContext ctx2 = new ClientSideCallContext(myServiceId, "foo", null); assertEquals(failoverServiceId, f.getServiceIdForCall(ctx2)); //now, after 9 microseconds we should try the original service again try{ Thread.sleep(10); }catch(Exception e){} ClientSideCallContext ctx3 = new ClientSideCallContext(myServiceId, "foo", null); assertEquals(myServiceId, f.getServiceIdForCall(ctx3)); } }