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);
            }
                             );
        }
示例#2
0
        public void ShutdownStateIsTerminalState()
        {
            // when
            var target = new BeaconSendingInitState();

            // then
            Assert.That(target.ShutdownState, Is.InstanceOf(typeof(BeaconSendingTerminalState)));
        }
        public void InitStateIsNotATerminalState()
        {
            // given
            var target = new BeaconSendingInitState();

            // then
            Assert.That(target.IsTerminalState, Is.False);
        }
        public void ToStringReturnsTheStateName()
        {
            // given
            var target = new BeaconSendingInitState();

            // then
            Assert.That(target.ToString(), Is.EqualTo("Initial"));
        }
        public void ShutdownStateGivesABeaconSendingTerminalStateInstance()
        {
            // given
            var target = new BeaconSendingInitState();

            // then
            Assert.That(target.ShutdownState, Is.Not.Null.And.InstanceOf(typeof(BeaconSendingTerminalState)));
        }
        public void CurrentStateIsInitializedAccordingly()
        {
            // given, when
            var initState = new BeaconSendingInitState();
            var target    = CreateSendingContext().With(initState).Build();

            // then
            Assert.That(target.CurrentState, Is.Not.Null);
            Assert.That(target.CurrentState, Is.InstanceOf <BeaconSendingInitState>());
        }
示例#7
0
        /// <summary>
        /// Constructor
        ///
        /// Current state is initialized to <see cref="Dynatrace.OpenKit.Core.Communication."/>
        ///
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="configuration"></param>
        /// <param name="httpClientProvider"></param>
        /// <param name="timingProvider"></param>
        public BeaconSendingContext(ILogger logger, OpenKitConfiguration configuration, IHTTPClientProvider httpClientProvider, ITimingProvider timingProvider)
        {
            this.logger        = logger;
            Configuration      = configuration;
            HTTPClientProvider = httpClientProvider;
            TimingProvider     = timingProvider;

            // set current state to init state
            CurrentState = new BeaconSendingInitState();
        }
        public void TransitionToTimeSyncIsPerformedOnSuccess()
        {
            // when
            var target = new BeaconSendingInitState();

            target.Execute(context);

            // then
            context.Received(1).NextState = Arg.Any <BeaconSendingTimeSyncState>();
        }
示例#9
0
        public void InitCompleteIsCalledOnInterrupt()
        {
            // when
            var target = new BeaconSendingInitState();

            target.OnInterrupted(context);

            // then
            context.Received(1).InitCompleted(false);
        }
        public void OnInterruptedCallsInitCompletedInContext()
        {
            // given
            var target = new BeaconSendingInitState();

            // when
            target.OnInterrupted(mockContext);

            // then
            mockContext.Received(1).InitCompleted(false);
        }
示例#11
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>();
        }
        public void ASuccessfulStatusResponseSetsInitCompletedToTrueForCaptureOff()
        {
            // given
            mockAttributes.IsCapture.Returns(false);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).InitCompleted(true);
        }
示例#13
0
        public void OpenKitInitIsCompletedOnSuccessIfCapturingIsDisabled()
        {
            // given
            context.IsCaptureOn.Returns(true);
            var target = new BeaconSendingInitState();

            // when
            target.Execute(context);

            // then
            context.Received(1).InitCompleted(true);
        }
示例#14
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>();
        }
示例#15
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;
        }
示例#16
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>();
        }
示例#17
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;
        }
        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>();
        }
        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 ShutdownStateAlwaysCreatesANewInstance()
        {
            // given
            var target = new BeaconSendingInitState();

            // when
            var obtainedOne = target.ShutdownState;
            var obtainedTwo = target.ShutdownState;

            // then
            Assert.That(obtainedOne, Is.Not.Null);
            Assert.That(obtainedTwo, Is.Not.Null);
            Assert.That(obtainedOne, Is.Not.SameAs(obtainedTwo));
        }
示例#21
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();
        }
示例#22
0
        /// <summary>
        /// Constructor
        ///
        /// Current state is initialized to <see cref="Dynatrace.OpenKit.Core.Communication."/>
        ///
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="httpClientProvider"></param>
        /// <param name="timingProvider"></param>
        public BeaconSendingContext(OpenKitConfiguration configuration, IHTTPClientProvider httpClientProvider, ITimingProvider timingProvider)
        {
            Configuration      = configuration;
            HTTPClientProvider = httpClientProvider;
            TimingProvider     = timingProvider;

            // set time sync supported to true
            IsTimeSyncSupported = true;
            // set last time sync time to -1
            LastTimeSyncTime = -1;

            // set current state to init state
            CurrentState = new BeaconSendingInitState();
        }
示例#23
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();
        }
示例#24
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;
        }
示例#26
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;
        }
示例#27
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;
        }
        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();
        }