public void ABeaconSendingCaptureOnStateClearsFinishedSessionsIfSendingIsNotAllowed()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            var statusResponse = Substitute.For <IStatusResponse>();

            statusResponse.ResponseCode.Returns(StatusResponse.HttpOk);
            statusResponse.IsErroneousResponse.Returns(false);

            mockSession3Finished.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>())
            .Returns(statusResponse);
            mockSession3Finished.IsDataSendingAllowed.Returns(false);
            mockSession4Finished.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>())
            .Returns(statusResponse);
            mockSession4Finished.IsDataSendingAllowed.Returns(false);

            // when
            target.Execute(mockContext);

            // then
            mockSession3Finished.Received(0)
            .SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>());
            mockSession4Finished.Received(0)
            .SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>());

            mockContext.Received(1).RemoveSession(mockSession3Finished);
            mockContext.Received(1).RemoveSession(mockSession4Finished);
        }
        public void UnsuccessfulNewSessionRequestDoesNotMergeStatusResponse()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            var sessionRequestResponse = Substitute.For <IStatusResponse>();

            sessionRequestResponse.IsErroneousResponse.Returns(true);
            var contextAttributes = Substitute.For <IResponseAttributes>();

            var mockClient = Substitute.For <IHttpClient>();

            mockClient.SendNewSessionRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(sessionRequestResponse);

            mockContext.GetHttpClient().Returns(mockClient);
            mockContext.LastResponseAttributes.Returns(contextAttributes);
            mockContext.GetAllNotConfiguredSessions().Returns(new List <ISessionInternals> {
                mockSession5New
            });
            mockSession5New.CanSendNewSessionRequest.Returns(true);

            // when
            target.Execute(mockContext);

            // then
            Assert.That(contextAttributes.ReceivedCalls(), Is.Empty);
            mockSession5New.Received(0).UpdateServerConfiguration(Arg.Any <ServerConfiguration>());
        }
Beispiel #3
0
        public void NewSessionRequestsAreMadeForAllNewSessions()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            var sessionOne = new SessionWrapper(CreateValidSession("127.0.0.1"));
            var sessionTwo = new SessionWrapper(CreateEmptySession("127.0.0.2"));

            newSessions.AddRange(new[] { sessionOne, sessionTwo });

            httpClient.SendNewSessionRequest().Returns(new StatusResponse("mp=5", 200, new Dictionary <string, List <string> >()),
                                                       null,
                                                       new StatusResponse("mp=3", 200, new Dictionary <string, List <string> >()));

            // when
            target.Execute(context);

            // verify for both new sessions a new session request has been made
            httpClient.Received(2).SendNewSessionRequest();

            // also verify that sessionOne got a new configuration
            Assert.That(sessionOne.IsBeaconConfigurationSet, Is.True);
            Assert.That(sessionOne.BeaconConfiguration.Multiplicity, Is.EqualTo(5));

            // for session two the number of requests was decremented
            Assert.That(sessionTwo.IsBeaconConfigurationSet, Is.False);
            Assert.That(sessionTwo.NumNewSessionRequestsLeft, Is.EqualTo(3));
        }
        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));
        }
Beispiel #5
0
        public void MultiplicityIsSetToZeroIfNoFurtherNewSessionRequestsAreAllowed()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            var sessionOne = new SessionWrapper(CreateValidSession("127.0.0.1"));
            var sessionTwo = new SessionWrapper(CreateEmptySession("127.0.0.2"));

            newSessions.AddRange(new[] { sessionOne, sessionTwo });

            httpClient.SendNewSessionRequest().Returns(new StatusResponse("mp=5", 200, new Dictionary <string, List <string> >()), null);

            // ensure that it's no longer possible to send session requests for both session wrapper
            while (sessionOne.CanSendNewSessionRequest)
            {
                sessionOne.DecreaseNumNewSessionRequests();
            }
            while (sessionTwo.CanSendNewSessionRequest)
            {
                sessionTwo.DecreaseNumNewSessionRequests();
            }

            // when
            target.Execute(context);

            // verify for no session a new session request has been made
            httpClient.Received(0).SendNewSessionRequest();

            // also ensure that both got a configuration set
            Assert.That(sessionOne.IsBeaconConfigurationSet, Is.True);
            Assert.That(sessionOne.BeaconConfiguration.Multiplicity, Is.EqualTo(0));

            Assert.That(sessionOne.IsBeaconConfigurationSet, Is.True);
            Assert.That(sessionOne.BeaconConfiguration.Multiplicity, Is.EqualTo(0));
        }
        public void OpenSessionsAreSentIfSendIntervalIsExceeded()
        {
            // given
            var clientIp = "127.0.0.1";

            var lastSendTime   = 1;
            var sendInterval   = 1000;
            var statusResponse = new StatusResponse(logger, string.Empty, Response.HttpOk, new Dictionary <string, List <string> >());

            context.LastOpenSessionBeaconSendTime.Returns(lastSendTime);
            context.SendInterval.Returns(sendInterval);
            context.CurrentTimestamp.Returns(lastSendTime + sendInterval + 1);

            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(x => statusResponse);

            var session = new SessionWrapper(CreateValidSession(clientIp));

            session.UpdateBeaconConfiguration(new BeaconConfiguration(1, DataCollectionLevel.OFF, CrashReportingLevel.OFF));
            openSessions.Add(session);

            // when
            var target = new BeaconSendingCaptureOnState();

            target.Execute(context);

            // then
            httpClient.Received(1).SendBeaconRequest(clientIp, Arg.Any <byte[]>());
            context.Received(1).HandleStatusResponse(statusResponse);
            Assert.That(context.LastOpenSessionBeaconSendTime, Is.EqualTo(context.CurrentTimestamp)); // assert send time update
        }
        public void OpenSessionsAreNotSentIfSendIntervalIsNotExceeded()
        {
            // given
            var clientIp = "127.0.0.1";

            var lastSendTime = 1;
            var sendInterval = 1000;

            context.LastOpenSessionBeaconSendTime.Returns(lastSendTime);
            context.SendInterval.Returns(sendInterval);
            context.CurrentTimestamp.Returns(lastSendTime + 1);

            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(x => new StatusResponse(string.Empty, 200));

            openSessions.Add(CreateValidSession(clientIp));

            // when
            var target = new BeaconSendingCaptureOnState();

            target.Execute(context);

            // then
            httpClient.DidNotReceive().SendBeaconRequest(clientIp, Arg.Any <byte[]>());
            context.DidNotReceive().HandleStatusResponse(Arg.Any <StatusResponse>());
        }
        public void OpenSessionsAreSentIfSendIntervalIsExceeded()
        {
            // given
            var clientIp = "127.0.0.1";

            var lastSendTime   = 1;
            var sendInterval   = 1000;
            var statusResponse = new StatusResponse(string.Empty, 200);

            context.LastOpenSessionBeaconSendTime.Returns(lastSendTime);
            context.SendInterval.Returns(sendInterval);
            context.CurrentTimestamp.Returns(lastSendTime + sendInterval + 1);

            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(x => statusResponse);

            openSessions.Add(CreateValidSession(clientIp));

            // when
            var target = new BeaconSendingCaptureOnState();

            target.Execute(context);

            // then
            httpClient.Received(1).SendBeaconRequest(clientIp, Arg.Any <byte[]>());
            context.Received(1).HandleStatusResponse(statusResponse);
            Assert.That(context.LastOpenSessionBeaconSendTime, Is.EqualTo(context.CurrentTimestamp)); // assert send time update
        }
        public void ABeaconSendingCaptureOnStateDoesNotSendOpenSessionsIfSendIntervalIsNotExceeded()
        {
            // given
            const int lastSendTime = 1;
            const int sendInterval = 1000;

            mockContext.LastOpenSessionBeaconSendTime.Returns(lastSendTime);
            mockContext.SendInterval.Returns(sendInterval);
            mockContext.CurrentTimestamp.Returns(lastSendTime + 1);

            var statusResponse = Substitute.For <IStatusResponse>();

            statusResponse.ResponseCode.Returns(StatusResponse.HttpOk);
            statusResponse.IsErroneousResponse.Returns(false);

            mockSession1Open.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>())
            .Returns(statusResponse);
            mockSession1Open.IsDataSendingAllowed.Returns(true);

            var target = new BeaconSendingCaptureOnState();

            // when
            target.Execute(mockContext);

            // then
            mockSession1Open.DidNotReceive()
            .SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>());
            mockContext.DidNotReceive().HandleStatusResponse(Arg.Any <StatusResponse>());
        }
        public void ABeaconSendingCaptureOnStateDoesNotRemoveFinishedSessionIfSendingWasUnsuccessful()
        {
            //given
            var target = new BeaconSendingCaptureOnState();

            var statusResponse = Substitute.For <IStatusResponse>();

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

            mockSession3Finished.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>())
            .Returns(statusResponse);
            mockSession3Finished.IsEmpty.Returns(false);
            mockSession3Finished.IsDataSendingAllowed.Returns(true);

            mockSession4Finished.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>())
            .Returns(statusResponse);
            mockSession4Finished.IsDataSendingAllowed.Returns(true);

            // when
            target.Execute(mockContext);

            mockSession3Finished.Received(1).SendBeacon(Arg.Any <IHttpClientProvider>(), mockContext);
            mockSession4Finished.Received(0)
            .SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>());

            _ = mockContext.Received(1).GetAllFinishedAndConfiguredSessions();
            mockContext.Received(0).RemoveSession(Arg.Any <ISessionInternals>());
        }
Beispiel #11
0
        public void OpenSessionsAreNotSentIfSendIntervalIsNotExceeded()
        {
            // given
            var clientIp = "127.0.0.1";

            var lastSendTime = 1;
            var sendInterval = 1000;

            context.LastOpenSessionBeaconSendTime.Returns(lastSendTime);
            context.SendInterval.Returns(sendInterval);
            context.CurrentTimestamp.Returns(lastSendTime + 1);

            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(x => new StatusResponse(string.Empty, 200, new Dictionary <string, List <string> >()));

            var session = new SessionWrapper(CreateValidSession(clientIp));

            session.UpdateBeaconConfiguration(new BeaconConfiguration(1, DataCollectionLevel.OFF, CrashReportingLevel.OFF));
            openSessions.Add(session);

            // when
            var target = new BeaconSendingCaptureOnState();

            target.Execute(context);

            // then
            httpClient.DidNotReceive().SendBeaconRequest(clientIp, Arg.Any <byte[]>());
            context.DidNotReceive().HandleStatusResponse(Arg.Any <StatusResponse>());
        }
        public void ABeaconSendingCaptureOnStateTransitionToFlushStateIsPerformedOnShutdown()
        {
            // given
            mockContext.IsShutdownRequested.Returns(true);
            var target = new BeaconSendingCaptureOnState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).NextState = Arg.Any <BeaconSendingFlushSessionsState>();
        }
        public void SendingOpenSessionsIsAbortedImmediatelyWhenTooManyRequestsResponseIsReceived()
        {
            //given
            var clientIp = "127.0.0.1";

            var lastSendTime    = 1;
            var sendInterval    = 1000;
            var responseHeaders = new Dictionary <string, List <string> >
            {
                { Response.ResponseKeyRetryAfter, new List <string> {
                      "987654"
                  } }
            };
            var statusResponse = new StatusResponse(logger, string.Empty, Response.HttpTooManyRequests, responseHeaders);

            openSessions.AddRange(new[] {
                new SessionWrapper(CreateValidSession(clientIp)),
                new SessionWrapper(CreateValidSession(clientIp)),
                new SessionWrapper(CreateValidSession(clientIp))
            });
            openSessions.ForEach(s => s.UpdateBeaconConfiguration(new BeaconConfiguration(1, DataCollectionLevel.OFF, CrashReportingLevel.OFF)));

            context.LastOpenSessionBeaconSendTime.Returns(lastSendTime);
            context.SendInterval.Returns(sendInterval);
            context.CurrentTimestamp.Returns(lastSendTime + sendInterval + 1);

            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(x => statusResponse);

            AbstractBeaconSendingState capturedState = null;

            context.NextState = Arg.Do <AbstractBeaconSendingState>(x => capturedState = x);

            var target = new BeaconSendingCaptureOnState();

            // when
            target.Execute(context);

            // then
            // verify only one session request has been made
            httpClient.Received(1).SendBeaconRequest(clientIp, Arg.Any <byte[]>());

            // ensure that state transition has been made
            context.ReceivedWithAnyArgs(1).NextState = null;
            Assert.That(capturedState, Is.Not.Null);
            Assert.That(capturedState, Is.InstanceOf <BeaconSendingCaptureOffState>());
            Assert.That(((BeaconSendingCaptureOffState)capturedState).sleepTimeInMilliseconds, Is.EqualTo(987654 * 1000));

            context.ReceivedWithAnyArgs(0).HandleStatusResponse(null);
        }
        public void ABeaconSendingCaptureOnStateTransitionsToCaptureOffStateWhenCapturingGotDisabled()
        {
            // given
            mockContext.IsCaptureOn.Returns(false);
            var target = new BeaconSendingCaptureOnState();

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).HandleStatusResponse(Arg.Any <IStatusResponse>());
            _ = mockContext.Received(1).IsCaptureOn;

            mockContext.Received(1).NextState = Arg.Any <BeaconSendingCaptureOffState>();
        }
Beispiel #15
0
        public void TransitionToTimeSycnIsPerformed()
        {
            // given
            var lastTimeSync = 1;

            context.LastTimeSyncTime.Returns(lastTimeSync);                                                               // return fixed value
            context.CurrentTimestamp.Returns(lastTimeSync + BeaconSendingTimeSyncState.TIME_SYNC_INTERVAL_IN_MILLIS + 1); // timesync interval + 1 sec

            // when
            var target = new BeaconSendingCaptureOnState();

            target.Execute(context);

            // then
            context.Received(1).NextState = Arg.Any <BeaconSendingTimeSyncState>();
        }
        public void ABeaconSendingCaptureOnStateSendsOpenSessionsIfNotExpired()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            mockSession1Open.IsDataSendingAllowed.Returns(true);
            mockSession2Open.IsDataSendingAllowed.Returns(true);

            // when
            target.Execute(mockContext);

            // then
            mockSession1Open.Received(1).SendBeacon(Arg.Any <IHttpClientProvider>(), mockContext);
            mockSession2Open.Received(1).SendBeacon(Arg.Any <IHttpClientProvider>(), mockContext);
            mockContext.Received(1).LastOpenSessionBeaconSendTime = Arg.Any <long>();
        }
        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));
        }
        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 UnsuccessfulFinishedSessionsAreMovedBackToCache()
        {
            //given
            var target = new BeaconSendingCaptureOnState();

            var finishedSession = CreateValidSession("127.0.0.1");

            context.GetNextFinishedSession().Returns(finishedSession);
            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns((StatusResponse)null);


            //when calling execute
            target.Execute(context);

            context.Received(1).GetNextFinishedSession();
            context.Received(1).PushBackFinishedSession(finishedSession);
        }
        public void NewSessionRequestsAreAbortedWhenTooManyRequestsResponseIsReceived()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            var sessionOne = new SessionWrapper(CreateValidSession("127.0.0.1"));
            var sessionTwo = new SessionWrapper(CreateValidSession("127.0.0.2"));

            newSessions.AddRange(new[] { sessionOne, sessionTwo });

            var responseHeaders = new Dictionary <string, List <string> >
            {
                { Response.ResponseKeyRetryAfter, new List <string> {
                      "1234  "
                  } }
            };

            httpClient.SendNewSessionRequest().Returns(new StatusResponse(logger, string.Empty, Response.HttpTooManyRequests, responseHeaders),
                                                       new StatusResponse(logger, "mp=1", Response.HttpOk, new Dictionary <string, List <string> >()),
                                                       new StatusResponse(logger, "mp=1", Response.HttpOk, new Dictionary <string, List <string> >()));

            AbstractBeaconSendingState capturedState = null;

            context.NextState = Arg.Do <AbstractBeaconSendingState>(x => capturedState = x);

            // when
            target.Execute(context);

            // verify for first new sessions a new session request has been made
            httpClient.Received(1).SendNewSessionRequest();

            // verify no changes on first & second sesion
            Assert.That(sessionOne.CanSendNewSessionRequest, Is.True);
            Assert.That(sessionOne.IsBeaconConfigurationSet, Is.False);

            Assert.That(sessionTwo.CanSendNewSessionRequest, Is.True);
            Assert.That(sessionTwo.IsBeaconConfigurationSet, Is.False);

            // ensure that state transition has been made
            context.ReceivedWithAnyArgs(1).NextState = null;
            Assert.That(capturedState, Is.Not.Null);
            Assert.That(capturedState, Is.InstanceOf <BeaconSendingCaptureOffState>());
            Assert.That(((BeaconSendingCaptureOffState)capturedState).sleepTimeInMilliseconds, Is.EqualTo(1234 * 1000));

            context.ReceivedWithAnyArgs(0).HandleStatusResponse(null);
        }
Beispiel #21
0
        public void UnsuccessfulFinishedSessionsAreNotRemovedFromCache()
        {
            //given
            var target = new BeaconSendingCaptureOnState();

            var finishedSession = new SessionWrapper(CreateValidSession("127.0.0.1"));

            finishedSession.UpdateBeaconConfiguration(new BeaconConfiguration(1, DataCollectionLevel.OFF, CrashReportingLevel.OFF));
            finishedSessions.Add(finishedSession);
            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns((StatusResponse)null);

            //when calling execute
            target.Execute(context);

            var tmp = context.Received(1).FinishedAndConfiguredSessions;

            context.Received(0).RemoveSession(finishedSession);
        }
        public void ABeaconSendingCaptureOnStateContinuesWithNextFinishedSessionIfSendingWasUnsuccessfulButBeaconIsEmtpy()
        {
            //given
            var target = new BeaconSendingCaptureOnState();

            var finishedEmptySession = CreateEmptySession("127.0.0.2");
            var finishedSession      = CreateValidSession("127.0.0.1");

            var statusResponses = new Queue <StatusResponse>();

            context.GetNextFinishedSession().Returns(finishedEmptySession, finishedSession, null);
            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(new StatusResponse(string.Empty, 200));

            //when calling execute
            target.Execute(context);

            context.Received(3).GetNextFinishedSession();
            context.Received(0).PushBackFinishedSession(finishedSession);
        }
        public void ABeaconSendingCaptureOnStateClearsOpenSessionDataIfSendingIsNotAllowed()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            mockSession1Open.IsDataSendingAllowed.Returns(false);
            mockSession2Open.IsDataSendingAllowed.Returns(false);

            // when
            target.Execute(mockContext);

            // then
            mockSession1Open.Received(0).SendBeacon(Arg.Any <IHttpClientProvider>(), mockContext);
            mockSession1Open.Received(1).ClearCapturedData();
            mockSession2Open.Received(0)
            .SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>());
            mockSession2Open.Received(1).ClearCapturedData();
            mockContext.Received(1).LastOpenSessionBeaconSendTime = Arg.Any <long>();
        }
        public void EmptyFinishedSessionsAreNotSent()
        {
            // given
            var clientIp = "127.0.0.1";

            finishedSessions.Enqueue(CreateEmptySession(clientIp));

            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(x => new StatusResponse(string.Empty, 200));

            // when
            var target = new BeaconSendingCaptureOnState();

            target.Execute(context);

            // then
            httpClient.DidNotReceive().SendBeaconRequest(clientIp, Arg.Any <byte[]>());
            Assert.That(finishedSessions.Count, Is.EqualTo(0)); // assert empty sessions
            context.DidNotReceive().HandleStatusResponse(Arg.Any <StatusResponse>());
        }
        public void TransitionToCaptureOffStateIsPerformed()
        {
            // given
            var clientIp = "127.0.0.1";

            context.IsCaptureOn.Returns(false);
            var statusResponse = new StatusResponse(string.Empty, 200);

            finishedSessions.Enqueue(CreateValidSession(clientIp));
            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(x => statusResponse);

            // when
            var target = new BeaconSendingCaptureOnState();

            target.Execute(context);

            // then
            context.Received(1).CurrentState = Arg.Any <BeaconSendingCaptureOffState>();
        }
        public void NewSessionRequestsAreMadeForAllNotConfiguredSessions()
        {
            // given
            const int multiplicity = 5;
            var       target       = new BeaconSendingCaptureOnState();

            var successResponse = StatusResponse.CreateSuccessResponse(mockLogger,
                                                                       ResponseAttributes.WithJsonDefaults().WithMultiplicity(multiplicity).Build(), 200,
                                                                       new Dictionary <string, List <string> >());

            var mockClient = Substitute.For <IHttpClient>();

            mockContext.GetHttpClient().Returns(mockClient);
            mockContext.GetAllNotConfiguredSessions()
            .Returns(new List <ISessionInternals> {
                mockSession5New, mockSession6New
            });
            mockContext.UpdateFrom(Arg.Any <StatusResponse>()).Returns(successResponse.ResponseAttributes);
            mockClient.SendNewSessionRequest(Arg.Any <IAdditionalQueryParameters>())
            .Returns(successResponse,
                     StatusResponse.CreateErrorResponse(mockLogger, StatusResponse.HttpBadRequest));
            mockSession5New.CanSendNewSessionRequest.Returns(true);
            mockSession6New.CanSendNewSessionRequest.Returns(true);

            IServerConfiguration serverConfigCapture = null;

            mockSession5New.UpdateServerConfiguration(Arg.Do <IServerConfiguration>(c => serverConfigCapture = c));

            // when
            target.Execute(mockContext);

            // verify for both new sessions a new session request has been made
            mockClient.Received(2).SendNewSessionRequest(mockContext);

            // verify first new session has been updated
            Assert.That(serverConfigCapture, Is.Not.Null);
            mockSession5New.Received(1).UpdateServerConfiguration(serverConfigCapture);
            Assert.That(serverConfigCapture.Multiplicity, Is.EqualTo(multiplicity));

            // verify second new session decreased number of retries
            mockSession6New.Received(1).DecreaseNumRemainingSessionRequests();
        }
        public void ABeaconSendingCaptureOnStateContinuesWithNextFinishedSessionIfSendingWasUnsuccessfulButBeaconIsEmpty()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            var errorResponse = Substitute.For <IStatusResponse>();

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

            var okResponse = Substitute.For <IStatusResponse>();

            okResponse.ResponseCode.Returns(StatusResponse.HttpOk);
            okResponse.IsErroneousResponse.Returns(false);

            mockSession3Finished.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>())
            .Returns(errorResponse);
            mockSession3Finished.IsEmpty.Returns(true);
            mockSession3Finished.IsDataSendingAllowed.Returns(true);

            mockSession4Finished.SendBeacon(Arg.Any <IHttpClientProvider>(), Arg.Any <IAdditionalQueryParameters>())
            .Returns(okResponse);
            mockSession4Finished.IsDataSendingAllowed.Returns(true);

            // when
            target.Execute(mockContext);

            // then
            mockSession3Finished.Received(1).SendBeacon(Arg.Any <IHttpClientProvider>(), mockContext);
            mockSession3Finished.Received(1).ClearCapturedData();

            mockSession4Finished.Received(1).SendBeacon(Arg.Any <IHttpClientProvider>(), mockContext);
            mockSession4Finished.Received(1).ClearReceivedCalls();


            _ = mockContext.Received(1).GetAllFinishedAndConfiguredSessions();
            mockContext.Received(1).RemoveSession(mockSession3Finished);
            mockContext.Received(1).RemoveSession(mockSession4Finished);
        }
Beispiel #28
0
        public void ABeaconSendingCaptureOnStateContinuesWithNextFinishedSessionIfSendingWasUnsuccessfulButBeaconIsEmtpy()
        {
            //given
            var target = new BeaconSendingCaptureOnState();

            var sessionOne = new SessionWrapper(CreateEmptySession("127.0.0.2"));
            var sessionTwo = new SessionWrapper(CreateValidSession("127.0.0.1"));

            finishedSessions.AddRange(new[] { sessionOne, sessionTwo });

            var statusResponses = new Queue <StatusResponse>();

            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(new StatusResponse(string.Empty, 200, new Dictionary <string, List <string> >()));

            //when calling execute
            target.Execute(context);

            var tmp = context.Received(1).FinishedAndConfiguredSessions;

            context.Received(1).RemoveSession(sessionOne);
            context.Received(1).RemoveSession(sessionTwo);
        }
        public void SuccessfulNewSessionRequestUpdateLastResponseAttributes()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            const int beaconSize         = 73;
            var       responseAttributes = Substitute.For <IResponseAttributes>();

            responseAttributes.MaxBeaconSizeInBytes.Returns(beaconSize);
            var sessionRequestResponse = Substitute.For <IStatusResponse>();

            sessionRequestResponse.ResponseAttributes.Returns(responseAttributes);

            mockContext.UpdateFrom(Arg.Any <IStatusResponse>()).Returns(responseAttributes);

            var mockClient = Substitute.For <IHttpClient>();

            mockClient.SendNewSessionRequest(Arg.Any <IAdditionalQueryParameters>()).Returns(sessionRequestResponse);

            mockContext.GetHttpClient().Returns(mockClient);
            mockContext.GetAllNotConfiguredSessions().Returns(new List <ISessionInternals> {
                mockSession5New
            });
            mockSession5New.CanSendNewSessionRequest.Returns(true);

            IServerConfiguration captureConfig = null;

            mockSession5New.UpdateServerConfiguration(Arg.Do <IServerConfiguration>(c => captureConfig = c));

            // when
            target.Execute(mockContext);

            // then
            mockContext.Received(1).UpdateFrom(sessionRequestResponse);
            mockSession5New.Received(1).UpdateServerConfiguration(Arg.Any <IServerConfiguration>());

            Assert.That(captureConfig, Is.Not.Null);
            Assert.That(captureConfig.BeaconSizeInBytes, Is.EqualTo(beaconSize));
        }
Beispiel #30
0
        public void TransitionToCaptureOffStateIsPerformed()
        {
            // given
            var clientIp = "127.0.0.1";

            context.IsCaptureOn.Returns(false);
            var statusResponse = new StatusResponse(string.Empty, 200, new Dictionary <string, List <string> >());

            var session = new SessionWrapper(CreateValidSession(clientIp));

            session.UpdateBeaconConfiguration(new BeaconConfiguration(1, DataCollectionLevel.OFF, CrashReportingLevel.OFF));
            finishedSessions.Add(session);
            httpClient.SendBeaconRequest(Arg.Any <string>(), Arg.Any <byte[]>()).Returns(x => statusResponse);

            // when
            var target = new BeaconSendingCaptureOnState();

            target.Execute(context);

            // then
            context.Received(1).NextState = Arg.Any <BeaconSendingCaptureOffState>();
        }