public void TimeSyncRequetsAreRetried() { // given httpClient.SendTimeSyncRequest().Returns( // request 1 fails 2 times x => null, x => null, x => CreateValidTimeResponse(currentTime, 10), // request 2 fails 1 time x => null, x => CreateValidTimeResponse(currentTime, 10), // request 3 fails 4 times x => null, x => null, x => null, x => null, x => CreateValidTimeResponse(currentTime, 10), // request 4 fails 0 times x => CreateValidTimeResponse(currentTime, 10), // request 5 fails 1 time x => null, x => CreateValidTimeResponse(currentTime, 10) ); // when var target = new BeaconSendingTimeSyncState(); target.Execute(context); // then httpClient.Received(13).SendTimeSyncRequest(); }
public void TimeSyncRequetsAreRetried() { // given var erroneousResponse = new TimeSyncResponse(Substitute.For <ILogger>(), string.Empty, Response.HttpBadRequest, new Dictionary <string, List <string> >()); httpClient.SendTimeSyncRequest().Returns( // request 1 fails 2 times x => erroneousResponse, x => erroneousResponse, x => CreateValidTimeResponse(currentTime, 10), // request 2 fails 1 time x => erroneousResponse, x => CreateValidTimeResponse(currentTime, 10), // request 3 fails 4 times x => erroneousResponse, x => erroneousResponse, x => erroneousResponse, x => erroneousResponse, x => CreateValidTimeResponse(currentTime, 10), // request 4 fails 0 times x => CreateValidTimeResponse(currentTime, 10), // request 5 fails 1 time x => erroneousResponse, x => CreateValidTimeResponse(currentTime, 10) ); // when var target = new BeaconSendingTimeSyncState(); target.Execute(context); // then httpClient.Received(13).SendTimeSyncRequest(); }
public void StateTransitionToCaptureOffIsMadeIfTooManyRequestsResponseIsReceived() { // given var responseHeaders = new Dictionary <string, List <string> > { { Response.ResponseKeyRetryAfter, new List <string> { "456" } } }; var response = new TimeSyncResponse(Substitute.For <ILogger>(), string.Empty, Response.HttpTooManyRequests, responseHeaders); httpClient.SendTimeSyncRequest().Returns(response); context.IsTimeSyncSupported.Returns(true); context.IsCaptureOn.Returns(true); AbstractBeaconSendingState capturedState = null; context.NextState = Arg.Do <AbstractBeaconSendingState>(x => capturedState = x); var target = new BeaconSendingTimeSyncState(true); // when target.Execute(context); // then context.ReceivedWithAnyArgs(1).NextState = null; Assert.That(capturedState, Is.Not.Null); Assert.That(capturedState, Is.InstanceOf <BeaconSendingCaptureOffState>()); Assert.That(((BeaconSendingCaptureOffState)capturedState).sleepTimeInMilliseconds, Is.EqualTo(456 * 1000)); }
protected override void DoExecute(IBeaconSendingContext context) { // every two hours a time sync shall be performed if (BeaconSendingTimeSyncState.IsTimeSyncRequired(context)) { // transition to time sync state. if cature on is still true after time sync we will end up in the CaputerOnState again context.NextState = new BeaconSendingTimeSyncState(); return; } context.Sleep(); // send new session request for all sessions that are new SendNewSessionRequests(context); statusResponse = null; // send all finished sessions SendFinishedSessions(context); // check if we need to send open sessions & do it if necessary SendOpenSessions(context); // check if send interval spent -> send current beacon(s) of open Sessions HandleStatusResponse(context, statusResponse); }
public void ToStringReturnsTheStateName() { // given var target = new BeaconSendingTimeSyncState(); // then Assert.That(target.ToString(), Is.EqualTo("TimeSync")); }
public void ShutdownStateIsTerminalStateForInitialTimeSync() { // when var target = new BeaconSendingTimeSyncState(true); // then Assert.That(target.ShutdownState, Is.InstanceOf <BeaconSendingTerminalState>()); }
public void ShutdownStateIsFlushSessionStateForTimeReSync() { // when var target = new BeaconSendingTimeSyncState(); // then Assert.That(target.ShutdownState, Is.InstanceOf <BeaconSendingFlushSessionsState>()); }
public void StateIsNotTerminal() { // when var target = new BeaconSendingTimeSyncState(); // then Assert.That(target.IsTerminalState, Is.False); }
protected override void DoExecute(IBeaconSendingContext context) { // every two hours a time sync shall be performed if (BeaconSendingTimeSyncState.IsTimeSyncRequired(context)) { // transition to time sync state. if cature on is still true after time sync we will end up in the CaputerOnState again context.NextState = new BeaconSendingTimeSyncState(); return; } context.Sleep(); // send new session request for all sessions that are new var newSessionsResponse = SendNewSessionRequests(context); if (BeaconSendingResponseUtil.IsTooManyRequestsResponse(newSessionsResponse)) { // server is currently overloaded, temporarily switch to capture off context.NextState = new BeaconSendingCaptureOffState(newSessionsResponse.GetRetryAfterInMilliseconds()); return; } // send all finished sessions var finishedSessionsResponse = SendFinishedSessions(context); if (BeaconSendingResponseUtil.IsTooManyRequestsResponse(finishedSessionsResponse)) { // server is currently overloaded, temporarily switch to capture off context.NextState = new BeaconSendingCaptureOffState(finishedSessionsResponse.GetRetryAfterInMilliseconds()); return; } // check if we need to send open sessions & do it if necessary var openSessionsResponse = SendOpenSessions(context); if (BeaconSendingResponseUtil.IsTooManyRequestsResponse(openSessionsResponse)) { // server is currently overloaded, temporarily switch to capture off context.NextState = new BeaconSendingCaptureOffState(openSessionsResponse.GetRetryAfterInMilliseconds()); return; } // collect the last status response var lastStatusResponse = newSessionsResponse; if (openSessionsResponse != null) { lastStatusResponse = openSessionsResponse; } else if (finishedSessionsResponse != null) { lastStatusResponse = finishedSessionsResponse; } // check if send interval spent -> send current beacon(s) of open Sessions HandleStatusResponse(context, lastStatusResponse); }
public void TimeSyncRequestsAreInterruptedAfterUnsuccessfulRetries() { // given var target = new BeaconSendingTimeSyncState(); // when target.Execute(context); // then httpClient.Received(BeaconSendingTimeSyncState.TIME_SYNC_REQUESTS + 1).SendTimeSyncRequest(); }
public void InitCompleteIsNotCalledForNonInitialRequest() { // given httpClient.SendTimeSyncRequest().Returns((TimeSyncResponse)null); // alwasys return invalid response // when var target = new BeaconSendingTimeSyncState(); target.Execute(context); // then context.DidNotReceive().InitCompleted(Arg.Any <bool>()); }
public void InitCompleteIsCalledForInitialRequest_OnFailure() { // given httpClient.SendTimeSyncRequest().Returns((TimeSyncResponse)null); // alwasys return invalid response // when var target = new BeaconSendingTimeSyncState(true); target.Execute(context); // then context.Received(1).InitCompleted(true); }
public void InitCompleteIsCalledForInitialRequest_OnSuccess() { // given httpClient.SendTimeSyncRequest().Returns(x => CreateValidTimeResponse(currentTime, 10)); // alwasys return valid response // when var target = new BeaconSendingTimeSyncState(true); target.Execute(context); // then context.Received(1).InitCompleted(true); }
public void TransitionToCaptureOffIsPerformedOnSuccess() { // given httpClient.SendTimeSyncRequest().Returns(x => CreateValidTimeResponse(currentTime, 10)); // alwasys return valid response context.IsCaptureOn.Returns(false); // captureOn is false // when var target = new BeaconSendingTimeSyncState(); target.Execute(context); // then context.Received(1).CurrentState = Arg.Any <BeaconSendingCaptureOffState>(); }
public void InitCompleteIsNotCalledForNonInitialRequest() { // given var erroneousResponse = new TimeSyncResponse(Substitute.For <ILogger>(), string.Empty, Response.HttpBadRequest, new Dictionary <string, List <string> >()); httpClient.SendTimeSyncRequest().Returns(erroneousResponse); // alwasys return invalid response // when var target = new BeaconSendingTimeSyncState(); target.Execute(context); // then context.DidNotReceive().InitCompleted(Arg.Any <bool>()); }
public void SleepTimeIsDoubledAndResetAfterSuccess() { // given var erroneousResponse = new TimeSyncResponse(Substitute.For <ILogger>(), string.Empty, Response.HttpBadRequest, new Dictionary <string, List <string> >()); httpClient.SendTimeSyncRequest().Returns( // request 1 fails 2 times x => erroneousResponse, x => erroneousResponse, x => erroneousResponse, x => erroneousResponse, x => erroneousResponse, x => CreateValidTimeResponse(currentTime, 10), // request 2 fails 1 time x => erroneousResponse, // other requets do not fail x => CreateValidTimeResponse(currentTime, 10), x => CreateValidTimeResponse(currentTime, 10), x => CreateValidTimeResponse(currentTime, 10), x => CreateValidTimeResponse(currentTime, 10) ); // when var target = new BeaconSendingTimeSyncState(); target.Execute(context); // then httpClient.Received(11).SendTimeSyncRequest(); context.Received(6).Sleep(Arg.Any <int>()); Received.InOrder(() => { // sleeps for first request context.Sleep(1000); // start with 1 sec context.Sleep(2000); // double context.Sleep(4000); // double context.Sleep(8000); // double context.Sleep(16000); // double // sleeps for second request context.Sleep(1000); // start with 1 sec again }); }
public void SleepTimeIsDoubledAndResetAfterSuccess() { httpClient.SendTimeSyncRequest().Returns( // request 1 fails 2 times x => null, x => null, x => null, x => null, x => null, x => CreateValidTimeResponse(currentTime, 10), // request 2 fails 1 time x => null, // other requets do not fail x => CreateValidTimeResponse(currentTime, 10), x => CreateValidTimeResponse(currentTime, 10), x => CreateValidTimeResponse(currentTime, 10), x => CreateValidTimeResponse(currentTime, 10) ); // when var target = new BeaconSendingTimeSyncState(); target.Execute(context); // then httpClient.Received(11).SendTimeSyncRequest(); context.Received(6).Sleep(Arg.Any <int>()); Received.InOrder(() => { // sleeps for first request context.Sleep(1000); // start with 1 sec context.Sleep(2000); // double context.Sleep(4000); // double context.Sleep(8000); // double context.Sleep(16000); // double // sleeps for second request context.Sleep(1000); // start with 1 sec again }); }