Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        public void SleepIsCalledOnEntry()
        {
            // given
            var target = new BeaconSendingCaptureOffState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).Sleep(BeaconSendingCaptureOffState.StatusCheckInterval);
        }
Ejemplo n.º 4
0
        public void SleepIsNotCalledIfShutdownIsRequested()
        {
            // given
            mockContext.IsShutdownRequested.Returns(true);
            var target = new BeaconSendingCaptureOffState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.DidNotReceive().Sleep(Arg.Any <int>());
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        public void TransitionToFlushStateIsPerformedOnShutdown()
        {
            // given
            mockContext.IsShutdownRequested.Returns(true);
            var target = new BeaconSendingCaptureOffState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).NextState = Arg.Any <BeaconSendingFlushSessionsState>();
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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();
        }
Ejemplo n.º 9
0
        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>();
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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>();
        }
Ejemplo n.º 13
0
        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>());
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 15
0
        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>();
        }