public void SleepTimeIsDoubledBetweenStatusRequestRetries()
        {
            //given
            var errorResponse = Substitute.For <IStatusResponse>();

            errorResponse.ResponseCode.Returns(StatusResponse.HttpBadRequest);
            errorResponse.IsErroneousResponse.Returns(true);

            mockHttpClient.SendStatusRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(errorResponse);
            mockContext.IsShutdownRequested.Returns(false, false, false, false, false, true);

            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            mockContext.Received(5).Sleep(Arg.Any <int>());
            Received.InOrder(() =>
            {
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 2);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 4);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 8);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 16);
            }
                             );
        }
        public void TransitionToTimeSyncIsPerformedOnSuccess()
        {
            // when
            var target = new BeaconSendingInitState();

            target.Execute(context);

            // then
            context.Received(1).NextState = Arg.Any <BeaconSendingTimeSyncState>();
        }
        public void ASuccessfulStatusResponseSetsInitCompletedToTrueForCaptureOff()
        {
            // given
            mockAttributes.IsCapture.Returns(false);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).InitCompleted(true);
        }
Esempio n. 4
0
        public void TransitionToCaptureOffIsPerformedOnSuccessIfCapturingIsDisabled()
        {
            // given
            context.IsCaptureOn.Returns(false);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // then
            context.Received(1).NextState = Arg.Any <BeaconSendingCaptureOffState>();
        }
Esempio n. 5
0
        public void OpenKitInitIsCompletedOnSuccessIfCapturingIsDisabled()
        {
            // given
            context.IsCaptureOn.Returns(true);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // then
            context.Received(1).InitCompleted(true);
        }
        public void InitIsTerminatedIfShutdownRequestedWithValidResponse()
        {
            // given
            mockContext.IsShutdownRequested.Returns(true); // shutdown is requested
            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(2).InitCompleted(false);
            mockContext.Received(1).NextState = Arg.Any <BeaconSendingTerminalState>();
        }
        public void ASuccessfulStatusResponsePerformsStateTransitionToCaptureOffIfCapturingIsDisabled()
        {
            // given
            mockContext.IsCaptureOn.Returns(false);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).HandleStatusResponse(mockResponse);
            mockContext.Received(1).NextState = Arg.Any <BeaconSendingCaptureOffState>();
        }
Esempio n. 8
0
        public void LastStatusCheckTimeIsSetInExecute()
        {
            // given
            context.IsShutdownRequested.Returns(true); // shutdown is requested
            context.CurrentTimestamp.Returns(654321L);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // then
            context.Received(1).LastStatusCheckTime = 654321L;
        }
Esempio n. 9
0
        public void LastOpenSessionBeaconSendTimeIsSetInExecute()
        {
            // given
            context.IsShutdownRequested.Returns(true); // shutdown is requested
            context.CurrentTimestamp.Returns(123456L);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // then
            context.Received(1).LastOpenSessionBeaconSendTime = 123456L;
        }
Esempio n. 10
0
        public void TransitionToTimeSyncIsPerformedOnSuccess()
        {
            // given
            httpClient.SendStatusRequest().Returns(new StatusResponse(string.Empty, 200)); // return valid status response (!= null)

            // when
            var target = new BeaconSendingInitState();

            target.Execute(context);

            // then
            context.Received(1).NextState = Arg.Any <BeaconSendingTimeSyncState>();
        }
Esempio n. 11
0
        public void InitCompleteIsCalledIfShutdownIsRequested()
        {
            // given
            context.IsShutdownRequested.Returns(true); // shutdown is requested
            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // then
            context.Received(2).InitCompleted(false);
            context.Received(1).NextState = Arg.Any <BeaconSendingTerminalState>();
        }
Esempio n. 12
0
        public void StatusRequestIsRetried()
        {
            // given
            httpClient.SendStatusRequest().Returns((StatusResponse)null); // always return null
            context.IsShutdownRequested.Returns(false, false, false, false, false, true);

            // when
            var target = new BeaconSendingInitState();

            target.Execute(context);

            // then
            httpClient.Received(6).SendStatusRequest();
        }
Esempio n. 13
0
        public void LastOpenSessionBeaconSendTimeIsSetInExecute()
        {
            // given
            context.IsShutdownRequested.Returns(true);                                                                                // shutdown is requested
            context.CurrentTimestamp.Returns(123456L);
            httpClient.SendStatusRequest().Returns(new StatusResponse(string.Empty, 200, new Dictionary <string, List <string> >())); // return valid status response (!= null)

            // when
            var target = new BeaconSendingInitState();

            target.Execute(context);

            // then
            context.Received(1).LastOpenSessionBeaconSendTime = 123456L;
        }
Esempio n. 14
0
        public void InitCompleteIsCalledIfShutdownIsRequested()
        {
            // given
            context.IsShutdownRequested.Returns(true);                                     // shutdown is requested
            httpClient.SendStatusRequest().Returns(new StatusResponse(string.Empty, 200)); // return valid status response (!= null)

            // when
            var target = new BeaconSendingInitState();

            target.Execute(context);

            // then
            context.Received(1).InitCompleted(false);
            context.Received(1).NextState = Arg.Any <BeaconSendingTerminalState>();
        }
        public void ExecuteSetsLastStatusCheckTime()
        {
            // given
            const long timestamp = 654321;

            mockContext.IsShutdownRequested.Returns(true); // shutdown is requested
            mockContext.CurrentTimestamp.Returns(timestamp);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).LastStatusCheckTime = timestamp;
        }
Esempio n. 16
0
        public void LastStatusCheckTimeIsSetInExecute()
        {
            // given
            context.IsShutdownRequested.Returns(true);                                     // shutdown is requested
            context.CurrentTimestamp.Returns(654321L);
            httpClient.SendStatusRequest().Returns(new StatusResponse(string.Empty, 200)); // return valid status response (!= null)

            // when
            var target = new BeaconSendingInitState();

            target.Execute(context);

            // then
            context.Received(1).LastStatusCheckTime = 654321L;
        }
Esempio n. 17
0
        public void StatusRequestIsRetried()
        {
            // given
            var erroneousResponse = new StatusResponse(logger, string.Empty, Response.HttpBadRequest, new Dictionary <string, List <string> >());

            httpClient.SendStatusRequest().Returns(erroneousResponse); // always return erroneous response
            context.IsShutdownRequested.Returns(false, false, false, false, false, true);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // then
            httpClient.Received(6).SendStatusRequest();
        }
        public void SendStatusRequestIsRetried()
        {
            // given
            var errorResponse = Substitute.For <IStatusResponse>();

            errorResponse.ResponseCode.Returns(StatusResponse.HttpBadRequest);
            errorResponse.IsErroneousResponse.Returns(true);
            mockHttpClient.SendStatusRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(errorResponse); // always return erroneous response
            mockContext.IsShutdownRequested.Returns(false, false, false, false, false, true);

            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // then
            mockHttpClient.Received(6).SendStatusRequest(mockContext);
        }
        public void ReceivingTooManyRequestsResponseDisablesCapturing()
        {
            // given
            const int retryTimeout  = 1234;
            var       errorResponse = Substitute.For <IStatusResponse>();

            errorResponse.ResponseCode.Returns(StatusResponse.HttpTooManyRequests);
            errorResponse.IsErroneousResponse.Returns(true);
            errorResponse.GetRetryAfterInMilliseconds().Returns(retryTimeout);
            mockHttpClient.SendStatusRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(errorResponse);
            mockContext.IsShutdownRequested.Returns(false, true);

            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // verify sleep was performed accordingly
            mockContext.Received(1).DisableCaptureAndClear();
        }
        public void ReceivingTooManyRequestsResponseDisablesCapturing()
        {
            // given
            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.IsShutdownRequested.Returns(false, true);

            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // verify sleep was performed accordingly
            context.Received(1).DisableCapture();
        }
        public void InitialStatusRequestGivesUpWhenShutdownRequestIsSetDuringExecution()
        {
            // given
            var errorResponse = Substitute.For <IStatusResponse>();

            errorResponse.ResponseCode.Returns(StatusResponse.HttpBadRequest);
            errorResponse.IsErroneousResponse.Returns(true);
            mockHttpClient.SendStatusRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(errorResponse);
            mockContext.IsShutdownRequested.Returns(false, false, true);

            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(2).InitCompleted(false);                               // init completed with error
            mockContext.Received(1).NextState = Arg.Any <BeaconSendingTerminalState>(); // transition to terminal state

            mockContext.Received(3).GetHttpClient();
            mockHttpClient.Received(3).SendStatusRequest(mockContext);

            mockContext.Received(2).Sleep(Arg.Any <int>());
        }
        public void ReinitializeSleepsBeforeSendingStatusRequestsAgain()
        {
            // given
            var count             = 0;
            var erroneousResponse = Substitute.For <IStatusResponse>();

            erroneousResponse.ResponseCode.Returns(StatusResponse.HttpBadRequest);
            erroneousResponse.IsErroneousResponse.Returns(true);
            mockHttpClient.SendStatusRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(erroneousResponse); // always return erroneous response
            mockContext.IsShutdownRequested.Returns(_ => count++ > 40);

            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // then
            // verify sleeps - first total number and then correct order
            mockContext.ReceivedWithAnyArgs(41).Sleep(0);

            Received.InOrder(() =>
            {
                // from first round
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 2);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 4);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 8);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 16);
                // delay between first and second attempt
                mockContext.Sleep(BeaconSendingInitState.ReInitDelayMilliseconds[0]);
                // and again the sequence
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 2);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 4);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 8);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 16);
                // delay between second and third attempt
                mockContext.Sleep(BeaconSendingInitState.ReInitDelayMilliseconds[1]);
                // and again the sequence
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 2);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 4);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 8);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 16);
                // delay between third and fourth attempt
                mockContext.Sleep(BeaconSendingInitState.ReInitDelayMilliseconds[2]);
                // and again the sequence
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 2);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 4);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 8);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 16);
                // delay between fourth and fifth attempt
                mockContext.Sleep(BeaconSendingInitState.ReInitDelayMilliseconds[3]);
                // and again the sequence
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 2);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 4);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 8);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 16);
                // delay between fifth and sixth attempt
                mockContext.Sleep(BeaconSendingInitState.ReInitDelayMilliseconds[4]);
                // and again the sequence
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 2);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 4);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 8);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 16);
                // delay between sixth and seventh attempt
                mockContext.Sleep(BeaconSendingInitState.ReInitDelayMilliseconds[4]);
                // and again the sequence
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 2);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 4);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 8);
                mockContext.Sleep(BeaconSendingInitState.InitialRetrySleepTimeMilliseconds * 16);
            });
        }
Esempio n. 23
0
        public void ReinitializeSleepsBeforeSendingStatusRequests()
        {
            // given
            var count = 0;

            httpClient.SendStatusRequest().Returns((StatusResponse)null); // always return null
            context.IsShutdownRequested.Returns(_ => { return(count++ > 40); });

            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // then
            // verify sleeps - first total number and then correct order
            context.ReceivedWithAnyArgs(41).Sleep(0);

            Received.InOrder(() =>
            {
                // from first round
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 2);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 4);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 8);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 16);
                // delay between first and second attempt
                context.Sleep(BeaconSendingInitState.REINIT_DELAY_MILLISECONDS[0]);
                // and again the sequence
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 2);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 4);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 8);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 16);
                // delay between second and third attempt
                context.Sleep(BeaconSendingInitState.REINIT_DELAY_MILLISECONDS[1]);
                // and again the sequence
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 2);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 4);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 8);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 16);
                // delay between third and fourth attempt
                context.Sleep(BeaconSendingInitState.REINIT_DELAY_MILLISECONDS[2]);
                // and again the sequence
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 2);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 4);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 8);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 16);
                // delay between fourth and fifth attempt
                context.Sleep(BeaconSendingInitState.REINIT_DELAY_MILLISECONDS[3]);
                // and again the sequence
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 2);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 4);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 8);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 16);
                // delay between fifth and sixth attempt
                context.Sleep(BeaconSendingInitState.REINIT_DELAY_MILLISECONDS[4]);
                // and again the sequence
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 2);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 4);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 8);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 16);
                // delay between sixth and seventh attempt
                context.Sleep(BeaconSendingInitState.REINIT_DELAY_MILLISECONDS[4]);
                // and again the sequence
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 2);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 4);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 8);
                context.Sleep(BeaconSendingInitState.INITIAL_RETRY_SLEEP_TIME_MILLISECONDS * 16);
            });
        }