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)); }
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 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 }); }