public void SendingOpenSessionsIsAbortedImmediatelyWhenTooManyRequestsResponseIsReceived() { //given const int sleepTime = 987654; var statusResponse = Substitute.For <IStatusResponse>(); statusResponse.ResponseCode.Returns(StatusResponse.HttpTooManyRequests); statusResponse.IsErroneousResponse.Returns(true); statusResponse.GetRetryAfterInMilliseconds().Returns(sleepTime); mockSession1Open.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>()) .Returns(statusResponse); mockSession1Open.IsDataSendingAllowed.Returns(true); mockSession2Open.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>()) .Returns(statusResponse); mockSession2Open.IsDataSendingAllowed.Returns(true); BeaconSendingCaptureOffState capturedState = null; mockContext.NextState = Arg.Do <BeaconSendingCaptureOffState>(x => capturedState = x); var target = new BeaconSendingCaptureOnState(); // when target.Execute(mockContext); // then mockSession1Open.Received(1).SendBeacon(Arg.Any <IHttpClientProvider>(), mockContext); _ = mockSession1Open.Received(1).IsDataSendingAllowed; Assert.That(mockSession2Open.ReceivedCalls(), Is.Empty); Assert.That(capturedState, Is.Not.Null); Assert.That(capturedState.SleepTimeInMilliseconds, Is.EqualTo(sleepTime)); }
public void ABeaconSendingCaptureOffStateStaysInOffStateWhenServerRespondsWithTooManyRequests() { //given var target = new BeaconSendingCaptureOffState(12345); const int retryTimeout = 1234; var tooManyRequestsResponse = Substitute.For <IStatusResponse>(); tooManyRequestsResponse.ResponseCode.Returns(StatusResponse.HttpTooManyRequests); tooManyRequestsResponse.IsErroneousResponse.Returns(true); tooManyRequestsResponse.GetRetryAfterInMilliseconds().Returns(retryTimeout); mockHttpClient.SendStatusRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(tooManyRequestsResponse); mockContext.IsCaptureOn.Returns(false); AbstractBeaconSendingState capturedState = null; mockContext.NextState = Arg.Do <AbstractBeaconSendingState>(x => capturedState = x); // when calling execute target.Execute(mockContext); // then verify next state Assert.That(capturedState, Is.Not.Null); Assert.That(capturedState, Is.InstanceOf <BeaconSendingCaptureOffState>()); Assert.That(((BeaconSendingCaptureOffState)capturedState).SleepTimeInMilliseconds, Is.EqualTo(retryTimeout)); }
public void ABeaconSendingCaptureOffStateStaysInOffStateWhenServerRespondsWithTooManyRequests() { //given var target = new BeaconSendingCaptureOffState(12345); var responseHeaders = new Dictionary <string, List <string> > { { Response.ResponseKeyRetryAfter, new List <string> { "1234" } } }; var tooManyRequestsResponse = new StatusResponse(logger, string.Empty, Response.HttpTooManyRequests, responseHeaders); httpClient.SendStatusRequest().Returns(tooManyRequestsResponse); context.IsCaptureOn.Returns(false); AbstractBeaconSendingState capturedState = null; context.NextState = Arg.Do <AbstractBeaconSendingState>(x => capturedState = x); // when calling execute target.Execute(context); // then verify next state Assert.That(capturedState, Is.Not.Null); Assert.That(capturedState, Is.InstanceOf <BeaconSendingCaptureOffState>()); Assert.That(((BeaconSendingCaptureOffState)capturedState).sleepTimeInMilliseconds, Is.EqualTo(1234 * 1000)); }
public void ToStringReturnsTheStateName() { // given var target = new BeaconSendingCaptureOffState(); // then Assert.That(target.ToString(), Is.EqualTo("CaptureOff")); }
public void StateIsNotTerminal() { // when var target = new BeaconSendingCaptureOffState(); // then Assert.That(target.IsTerminalState, Is.False); }
public void ShutdownStateIsFlushState() { // when var target = new BeaconSendingCaptureOffState(); // then Assert.That(target.ShutdownState, Is.InstanceOf(typeof(BeaconSendingFlushSessionsState))); }
public void SleepIsCalledOnEntry() { // given var target = new BeaconSendingCaptureOffState(); // when target.Execute(mockContext); // then mockContext.Received(1).Sleep(BeaconSendingCaptureOffState.StatusCheckInterval); }
public void TransitionToFlushStateIsPerformedOnShutdown() { // given mockContext.IsShutdownRequested.Returns(true); var target = new BeaconSendingCaptureOffState(); // when target.Execute(mockContext); // then mockContext.Received(1).NextState = Arg.Any <BeaconSendingFlushSessionsState>(); }
public void StatusRequestIsRetried() { // given mockHttpClient.SendStatusRequest(Arg.Any <IAdditionalQueryParameters>()).Returns((StatusResponse)null); // always return null var target = new BeaconSendingCaptureOffState(); // when target.Execute(mockContext); // then mockHttpClient.Received(6).SendStatusRequest(mockContext); }
public void SleepIsNotCalledIfShutdownIsRequested() { // given mockContext.IsShutdownRequested.Returns(true); var target = new BeaconSendingCaptureOffState(); // when target.Execute(mockContext); // then mockContext.DidNotReceive().Sleep(Arg.Any <int>()); }
public void ABeaconSendingCaptureOffStateWaitsForGivenTime() { //given var target = new BeaconSendingCaptureOffState(12345); context.IsCaptureOn.Returns(true); // when calling execute target.Execute(context); // then verify the custom amount of time was waited context.Received(1).Sleep(12345); }
public void StatusRequestIsRetried() { // given httpClient.SendStatusRequest().Returns((StatusResponse)null); // always return null // when var target = new BeaconSendingCaptureOffState(); target.Execute(context); // then httpClient.Received(6).SendStatusRequest(); }
public void TransitionToCaptureOnStateIsPerformed() { // given context.IsCaptureOn.Returns(true); httpClient.SendStatusRequest().Returns(new StatusResponse(logger, string.Empty, 200, new Dictionary <string, List <string> >())); // when var target = new BeaconSendingCaptureOffState(); target.Execute(context); // then context.Received(1).NextState = Arg.Any <BeaconSendingCaptureOnState>(); }
public void ABeaconSendingCaptureOffStateWaitsForGivenTime() { //given const int sleepTime = 12345; var target = new BeaconSendingCaptureOffState(sleepTime); mockContext.IsCaptureOn.Returns(true); // when target.Execute(mockContext); // then mockContext.Received(1).Sleep(sleepTime); }
public void SleepIsCalledOnEntry() { // given context.CurrentTimestamp.Returns(0); context.LastStatusCheckTime.Returns(0); httpClient.SendStatusRequest().Returns(new StatusResponse(logger, string.Empty, 200, new Dictionary <string, List <string> >())); // when var target = new BeaconSendingCaptureOffState(); target.Execute(context); // then context.Received(1).Sleep(BeaconSendingCaptureOffState.STATUS_CHECK_INTERVAL); }
public void ABeaconSendingCaptureOffStateTransitionsToCaptureOnStateWhenCapturingActive() { // given var target = new BeaconSendingCaptureOffState(); mockContext.IsCaptureOn.Returns(true); // when target.Execute(mockContext); // then mockContext.Received(1).DisableCaptureAndClear(); mockContext.Received(1).LastStatusCheckTime = Arg.Any <long>(); mockContext.Received(1).NextState = Arg.Any <BeaconSendingCaptureOnState>(); }
public void SleepIsNotCalledIfShutdownIsRequested() { // given context.IsShutdownRequested.Returns(true); context.CurrentTimestamp.Returns(0); context.LastStatusCheckTime.Returns(0); httpClient.SendStatusRequest().Returns(new StatusResponse(logger, string.Empty, 200, new Dictionary <string, List <string> >())); // when var target = new BeaconSendingCaptureOffState(); target.Execute(context); // then context.DidNotReceive().Sleep(Arg.Any <int>()); }
public void TransitionToTimeSyncIsPerformedIfNotDoneYet() { // given context.IsCaptureOn.Returns(true); context.IsTimeSyncSupported.Returns(true); httpClient.SendStatusRequest().Returns(new StatusResponse(string.Empty, 200, new Dictionary <string, List <string> >())); var target = new BeaconSendingCaptureOffState(); // when target.Execute(context); // then context.Received(1).NextState = Arg.Any <BeaconSendingTimeSyncState>(); context.Received(1).NextState = Arg.Is <BeaconSendingTimeSyncState>(arg => arg.IsInitialTimeSync == true); }
public void TransitionToCaptureOnStateIsPerformed() { // given context.IsCaptureOn.Returns(true); context.IsTimeSyncSupported.Returns(true); context.IsTimeSynced.Returns(true); httpClient.SendStatusRequest().Returns(new StatusResponse(string.Empty, 200)); // when var target = new BeaconSendingCaptureOffState(); target.Execute(context); // then context.Received(1).NextState = Arg.Any <BeaconSendingCaptureOnState>(); }
public void SendingFinishedSessionsIsAbortedImmediatelyWhenTooManyRequestsResponseIsReceived() { // given const int sleepTime = 4321; var target = new BeaconSendingCaptureOnState(); var statusResponse = Substitute.For <IStatusResponse>(); statusResponse.ResponseCode.Returns(StatusResponse.HttpTooManyRequests); statusResponse.IsErroneousResponse.Returns(true); statusResponse.GetRetryAfterInMilliseconds().Returns(sleepTime); mockSession3Finished.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>()) .Returns(statusResponse); mockSession3Finished.IsDataSendingAllowed.Returns(true); mockSession4Finished.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>()) .Returns(statusResponse); mockSession4Finished.IsDataSendingAllowed.Returns(true); BeaconSendingCaptureOffState captureState = null; mockContext.NextState = Arg.Do <BeaconSendingCaptureOffState>(c => captureState = c); // when target.Execute(mockContext); // then _ = mockSession3Finished.Received(1).IsDataSendingAllowed; mockSession3Finished.Received(1) .SendBeacon(Arg.Any <IHttpClientProvider>(), mockContext); mockSession3Finished.Received(0).UpdateServerConfiguration(Arg.Any <IServerConfiguration>()); mockSession3Finished.Received(0).DecreaseNumRemainingSessionRequests(); // verify no interactions with second finished session Assert.That(mockSession4Finished.ReceivedCalls(), Is.Empty); // verify no interactions with open sessions Assert.That(mockSession1Open.ReceivedCalls(), Is.Empty); Assert.That(mockSession2Open.ReceivedCalls(), Is.Empty); _ = mockContext.Received(1).GetAllFinishedAndConfiguredSessions(); mockContext.Received(0).RemoveSession(Arg.Any <ISessionInternals>()); Assert.That(captureState, Is.Not.Null); mockContext.Received(1).NextState = captureState; Assert.That(captureState.SleepTimeInMilliseconds, Is.EqualTo(sleepTime)); }
public void NewSessionRequestsAreAbortedWhenTooManyRequestsResponseIsReceived() { // given const int sleepTime = 6543; var target = new BeaconSendingCaptureOnState(); var statusResponse = Substitute.For <IStatusResponse>(); statusResponse.ResponseCode.Returns(StatusResponse.HttpTooManyRequests); statusResponse.IsErroneousResponse.Returns(true); statusResponse.GetRetryAfterInMilliseconds().Returns(sleepTime); var mockClient = Substitute.For <IHttpClient>(); mockClient.SendNewSessionRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(statusResponse); mockContext.GetHttpClient().Returns(mockClient); mockContext.GetAllNotConfiguredSessions() .Returns(new List <ISessionInternals> { mockSession5New, mockSession6New }); mockSession5New.CanSendNewSessionRequest.Returns(true); mockSession6New.CanSendNewSessionRequest.Returns(true); BeaconSendingCaptureOffState capturedState = null; mockContext.NextState = Arg.Do <BeaconSendingCaptureOffState>(x => capturedState = x); // when target.Execute(mockContext); // verify for first new sessions a new session request has been made mockClient.Received(1).SendNewSessionRequest(mockContext); // verify no changes on first _ = mockSession5New.Received(1).CanSendNewSessionRequest; mockSession5New.Received(0).UpdateServerConfiguration(Arg.Any <IServerConfiguration>()); mockSession5New.Received(0).DecreaseNumRemainingSessionRequests(); // verify second new session is not used at all Assert.That(mockSession6New.ReceivedCalls(), Is.Empty); // ensure also transition to CaptureOffState Assert.That(capturedState, Is.Not.Null); mockContext.Received(1).NextState = capturedState; Assert.That(capturedState.SleepTimeInMilliseconds, Is.EqualTo(sleepTime)); }