Ejemplo n.º 1
0
        protected override void DoExecute(IBeaconSendingContext context)
        {
            context.DisableCaptureAndClear();

            var currentTime = context.CurrentTimestamp;

            var delta = SleepTimeInMilliseconds > 0
                ? SleepTimeInMilliseconds
                : (int)(StatusCheckInterval - (currentTime - context.LastStatusCheckTime));

            if (delta > 0 && !context.IsShutdownRequested)
            {
                // still have some time to sleep
                context.Sleep(delta);
            }

            // send the status request
            var statusResponse = BeaconSendingRequestUtil.SendStatusRequest(context, StatusRequestRetries, InitialRetrySleepTimeMilliseconds);

            // process the response
            HandleStatusResponse(context, statusResponse);

            // update the last status check time in any case
            context.LastStatusCheckTime = currentTime;
        }
Ejemplo n.º 2
0
        public void SleepTimeIsDoubledBetweenConsecutiveRetries()
        {
            // given
            var erroneousResponse = new StatusResponse(Substitute.For <ILogger>(), string.Empty, Response.HttpBadRequest, new Dictionary <string, List <string> >());

            context.IsShutdownRequested.Returns(false);
            httpClient.SendStatusRequest().Returns(erroneousResponse);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(context, 5, 1000);

            // then
            Assert.That(obtained, Is.SameAs(erroneousResponse));

            context.Received(6).GetHTTPClient();
            httpClient.Received(6).SendStatusRequest();

            Received.InOrder(() =>
            {
                context.Sleep(1000);
                context.Sleep(2 * 1000);
                context.Sleep(4 * 1000);
                context.Sleep(8 * 1000);
                context.Sleep(16 * 1000);
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Execute status requests, until a successful response was received or shutdown was requested.
        /// </summary>
        /// <param name="context">The state's context</param>
        /// <returns>The last received status response, which might be erroneous if shutdown has been requested.</returns>
        private IStatusResponse ExecuteStatusRequest(IBeaconSendingContext context)
        {
            IStatusResponse statusResponse;

            while (true)
            {
                var currentTimestamp = context.CurrentTimestamp;
                context.LastOpenSessionBeaconSendTime = currentTimestamp;
                context.LastStatusCheckTime           = currentTimestamp;

                statusResponse = BeaconSendingRequestUtil.SendStatusRequest(context, MaxInitialStatusRequestRetries, InitialRetrySleepTimeMilliseconds);
                if (context.IsShutdownRequested || BeaconSendingResponseUtil.IsSuccessfulResponse(statusResponse))
                {
                    // shutdown was requested or a successful status response was received
                    break;
                }

                var sleepTime = ReInitDelayMilliseconds[reinitializeDelayIndex];
                if (BeaconSendingResponseUtil.IsTooManyRequestsResponse(statusResponse))
                {
                    // in case of too many requests the server might send us a retry-after
                    sleepTime = statusResponse.GetRetryAfterInMilliseconds();
                    // also temporarily disable capturing to avoid further server overloading
                    context.DisableCaptureAndClear();
                }

                // status request needs to be sent again after some delay
                context.Sleep(sleepTime);
                reinitializeDelayIndex = Math.Min(reinitializeDelayIndex + 1, ReInitDelayMilliseconds.Length - 1); // ensure no out of bounds
            }

            return(statusResponse);
        }
        protected override void DoExecute(IBeaconSendingContext context)
        {
            StatusResponse statusResponse;

            while (true)
            {
                long currentTimestamp = context.CurrentTimestamp;
                context.LastOpenSessionBeaconSendTime = currentTimestamp;
                context.LastStatusCheckTime           = currentTimestamp;

                statusResponse = BeaconSendingRequestUtil.SendStatusRequest(context, MAX_INITIAL_STATUS_REQUEST_RETRIES, INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                if (context.IsShutdownRequested || statusResponse != null)
                {
                    // shutdown was requested or a status response was received
                    break;
                }

                // status request needs to be sent again after some delay
                context.Sleep(REINIT_DELAY_MILLISECONDS[reinitializeDelayIndex]);
                reinitializeDelayIndex = Math.Min(reinitializeDelayIndex + 1, REINIT_DELAY_MILLISECONDS.Length - 1); // ensure no out of bounds
            }

            if (!context.IsShutdownRequested && statusResponse != null)
            {
                // success -> continue with time sync
                context.HandleStatusResponse(statusResponse);
                context.CurrentState = new BeaconSendingTimeSyncState(true);
            }
        }
        /// <summary>
        /// Execute status requests, until a successful response was received or shutdown was requested.
        /// </summary>
        /// <param name="context">The state's context</param>
        /// <returns>The last received status response, which might be erroneous if shutdown has been requested.</returns>
        private StatusResponse ExecuteStatusRequest(IBeaconSendingContext context)
        {
            StatusResponse statusResponse;

            while (true)
            {
                var currentTimestamp = context.CurrentTimestamp;
                context.LastOpenSessionBeaconSendTime = currentTimestamp;
                context.LastStatusCheckTime           = currentTimestamp;

                statusResponse = BeaconSendingRequestUtil.SendStatusRequest(context, MAX_INITIAL_STATUS_REQUEST_RETRIES, INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);
                if (context.IsShutdownRequested || BeaconSendingResponseUtil.IsSuccessfulResponse(statusResponse))
                {
                    // shutdown was requested or a successful status response was received
                    break;
                }

                var sleepTime = REINIT_DELAY_MILLISECONDS[reinitializeDelayIndex];
                if (BeaconSendingResponseUtil.IsTooManyRequestsResponse(statusResponse))
                {
                    // in case of too many requests the server might send us a retry-after
                    sleepTime = statusResponse.GetRetryAfterInMilliseconds();
                    // also temporarily disable capturing to avoid further server overloading
                    context.DisableCapture();
                }

                // status request needs to be sent again after some delay
                context.Sleep(sleepTime);
                reinitializeDelayIndex = Math.Min(reinitializeDelayIndex + 1, REINIT_DELAY_MILLISECONDS.Length - 1); // ensure no out of bounds
            }

            return(statusResponse);
        }
        protected override void DoExecute(IBeaconSendingContext context)
        {
            context.DisableCapture();

            var currentTime = context.CurrentTimestamp;

            var delta = sleepTimeInMilliseconds > 0
                ? sleepTimeInMilliseconds
                : (int)(STATUS_CHECK_INTERVAL - (currentTime - context.LastStatusCheckTime));

            if (delta > 0 && !context.IsShutdownRequested)
            {
                // still have some time to sleep
                context.Sleep(delta);
            }

            // send the status request
            var statusResponse = BeaconSendingRequestUtil.SendStatusRequest(context, STATUS_REQUEST_RETRIES, INITIAL_RETRY_SLEEP_TIME_MILLISECONDS);

            // process the response
            HandleStatusResponse(context, statusResponse);

            // update the last status check time in any case
            context.LastStatusCheckTime = currentTime;
        }
        public void SendStatusRequestHandlesNullResponsesSameAsErroneousResponses()
        {
            // given
            mockContext.IsShutdownRequested.Returns(false);
            mockHttpClient.SendStatusRequest(mockContext).Returns((StatusResponse)null);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(mockContext, 3, 1000);

            // then
            Assert.That(obtained, Is.Null);

            mockContext.Received(4).GetHttpClient();
            mockContext.ReceivedWithAnyArgs(3).Sleep(0);
            mockHttpClient.Received(4).SendStatusRequest(mockContext);
        }
        public void SendStatusRequestIsAbortedIfNumberOfRetriesIsExceeded()
        {
            // given
            context.IsShutdownRequested.Returns(false);
            httpClient.SendStatusRequest().Returns((StatusResponse)null);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(context, 3, 1000);

            // then
            Assert.That(obtained, Is.Null);

            context.Received(4).GetHTTPClient();
            context.ReceivedWithAnyArgs(3).Sleep(0);

            httpClient.Received(4).SendStatusRequest();
        }
        public void SendStatusRequestWhenHttpClientReturnsANonNullResponse()
        {
            // given
            context.IsShutdownRequested.Returns(false);
            httpClient.SendStatusRequest().Returns(statusResponse);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(context, 5, 1000);

            // then
            Assert.That(obtained, Is.SameAs(statusResponse));

            context.Received(1).GetHTTPClient();
            context.ReceivedWithAnyArgs(0).Sleep(0);

            httpClient.Received(1).SendStatusRequest();
        }
        public void SendStatusRequestIsDoneWhenHttpClientReturnsASuccessfulResponse()
        {
            // given
            mockContext.IsShutdownRequested.Returns(false);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(mockContext, 5, 1000);

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained, Is.SameAs(mockResponse));

            mockContext.Received(1).GetHttpClient();
            mockContext.ReceivedWithAnyArgs(0).Sleep(0);

            mockHttpClient.Received(1).SendStatusRequest(mockContext);
        }
        public void SendStatusRequestIsAbortedWhenShutDownIsRequested()
        {
            // given
            context.IsShutdownRequested.Returns(true);
            httpClient.SendStatusRequest().Returns((StatusResponse)null);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(context, 5, 1000);

            // then
            Assert.That(obtained, Is.Null);

            context.Received(1).GetHTTPClient();
            context.ReceivedWithAnyArgs(0).Sleep(0);

            httpClient.Received(1).SendStatusRequest();
        }
        public void SendStatusRequestReturnsTooManyRequestsResponseImmediately()
        {
            // given
            mockResponse.ResponseCode.Returns(StatusResponse.HttpTooManyRequests);
            mockResponse.IsErroneousResponse.Returns(true);
            mockContext.IsShutdownRequested.Returns(false);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(mockContext, 3, 1000);

            // then
            Assert.That(obtained, Is.SameAs(mockResponse));

            mockContext.Received(1).GetHttpClient();
            mockContext.DidNotReceiveWithAnyArgs().Sleep(0);
            mockHttpClient.Received(1).SendStatusRequest(mockContext);
        }
Ejemplo n.º 13
0
        public void SendStatusRequestReturnsTooManyRequestsResponseImmediately()
        {
            // given
            var tooManyRequestsResponse = new StatusResponse(Substitute.For <ILogger>(), string.Empty, Response.HttpTooManyRequests, new Dictionary <string, List <string> >());

            context.IsShutdownRequested.Returns(false);
            httpClient.SendStatusRequest().Returns(tooManyRequestsResponse);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(context, 3, 1000);

            // then
            Assert.That(obtained, Is.SameAs(tooManyRequestsResponse));

            context.Received(1).GetHTTPClient();
            context.DidNotReceiveWithAnyArgs().Sleep(0);
            httpClient.Received(1).SendStatusRequest();
        }
        public void SendStatusRequestIsAbortedIfNumberOfRetriesIsExceeded()
        {
            // given
            mockResponse.ResponseCode.Returns(StatusResponse.HttpBadRequest);
            mockResponse.IsErroneousResponse.Returns(true);
            mockContext.IsShutdownRequested.Returns(false);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(mockContext, 3, 1000);

            // then
            Assert.That(obtained, Is.SameAs(mockResponse));

            mockContext.Received(4).GetHttpClient();
            mockContext.ReceivedWithAnyArgs(3).Sleep(0);

            mockHttpClient.Received(4).SendStatusRequest(mockContext);
        }
        public void SendStatusRequestIsAbortedWhenShutDownIsRequested()
        {
            // given
            mockResponse.ResponseCode.Returns(StatusResponse.HttpBadRequest);
            mockResponse.IsErroneousResponse.Returns(true);
            mockContext.IsShutdownRequested.Returns(true);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(mockContext, 5, 1000);

            // then
            Assert.That(obtained, Is.SameAs(mockResponse));

            _ = mockContext.Received(1).IsShutdownRequested;
            mockContext.Received(1).GetHttpClient();
            mockContext.ReceivedWithAnyArgs(0).Sleep(0);

            mockHttpClient.Received(1).SendStatusRequest(mockContext);
        }
Ejemplo n.º 16
0
        public void SendStatusRequestIsAbortedIfNumberOfRetriesIsExceeded()
        {
            // given
            var erroneousResponse = new StatusResponse(Substitute.For <ILogger>(), string.Empty, Response.HttpBadRequest, new Dictionary <string, List <string> >());

            context.IsShutdownRequested.Returns(false);
            httpClient.SendStatusRequest().Returns(erroneousResponse);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(context, 3, 1000);

            // then
            Assert.That(obtained, Is.SameAs(erroneousResponse));

            context.Received(4).GetHTTPClient();
            context.ReceivedWithAnyArgs(3).Sleep(0);

            httpClient.Received(4).SendStatusRequest();
        }
        public void SleepTimeIsDoubledBetweenConsecutiveRetries()
        {
            // given
            context.IsShutdownRequested.Returns(false);
            httpClient.SendStatusRequest().Returns((StatusResponse)null);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(context, 5, 1000);

            // then
            Assert.That(obtained, Is.Null);

            context.Received(6).GetHTTPClient();
            httpClient.Received(6).SendStatusRequest();

            Received.InOrder(() =>
            {
                context.Sleep(1000);
                context.Sleep(2 * 1000);
                context.Sleep(4 * 1000);
                context.Sleep(8 * 1000);
                context.Sleep(16 * 1000);
            });
        }
        public void SleepTimeIsDoubledBetweenConsecutiveRetries()
        {
            // given
            mockResponse.ResponseCode.Returns(StatusResponse.HttpBadRequest);
            mockResponse.IsErroneousResponse.Returns(true);
            mockContext.IsShutdownRequested.Returns(false);

            // when
            var obtained = BeaconSendingRequestUtil.SendStatusRequest(mockContext, 5, 1000);

            // then
            Assert.That(obtained, Is.SameAs(mockResponse));
            mockContext.Received(6).GetHttpClient();
            mockHttpClient.Received(6).SendStatusRequest(mockContext);

            Received.InOrder(() =>
            {
                mockContext.Sleep(1000);
                mockContext.Sleep(2000);
                mockContext.Sleep(4000);
                mockContext.Sleep(8000);
                mockContext.Sleep(16000);
            });
        }