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(logger, "mp=5", Response.HttpOk, new Dictionary <string, List <string> >()),
                                                       new StatusResponse(logger, "mp=5", Response.HttpBadRequest, new Dictionary <string, List <string> >()),
                                                       new StatusResponse(logger, "mp=3", Response.HttpOk, 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 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(logger, "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(sessionTwo.IsBeaconConfigurationSet, Is.True);
            Assert.That(sessionTwo.BeaconConfiguration.Multiplicity, Is.EqualTo(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(logger, string.Empty, Response.HttpOk, 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>());
        }
Example #4
0
        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);

            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
        }
Example #5
0
        public void AfterDecreasingNumNewSessionRequestsFourTimesSendingRequestsIsNoLongerAllowed()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // when decreasing first time
            target.DecreaseNumNewSessionRequests();

            // then sending is still allowed
            Assert.That(target.CanSendNewSessionRequest, Is.True);

            // when decreasing second time
            target.DecreaseNumNewSessionRequests();

            // then sending is still allowed
            Assert.That(target.CanSendNewSessionRequest, Is.True);

            // when decreasing third time
            target.DecreaseNumNewSessionRequests();

            // then sending is still allowed
            Assert.That(target.CanSendNewSessionRequest, Is.True);

            // when decreasing fourth time
            target.DecreaseNumNewSessionRequests();

            // then sending is no longer allowed
            Assert.That(target.CanSendNewSessionRequest, Is.False);
        }
Example #6
0
        public void ADefaultConstructedSessionWrapperCanSendRequests()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // then
            Assert.That(target.CanSendNewSessionRequest, Is.True);
        }
Example #7
0
        public void GetBeaconConfigurationCallsWrappedSession()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // when, then
            Assert.That(target.BeaconConfiguration, Is.SameAs(wrappedSession.BeaconConfiguration));
        }
Example #8
0
        public void ByDefaultTheSessionIsNotFinished()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // then
            Assert.That(target.IsSessionFinished, Is.False);
        }
Example #9
0
        public void ByDefaultBeaconConfigurationIsNotSet()
        {
            // wrappedSession
            var target = new SessionWrapper(wrappedSession);

            // then
            Assert.That(target.IsBeaconConfigurationSet, Is.False);
        }
Example #10
0
        public void WhenBeaconConfigurationIsNotSetSendingIsNotAllowed()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // when, then
            Assert.That(target.IsDataSendingAllowed, Is.False);
        }
Example #11
0
        public void WhenBeaconConfigurationIsSetSendingIsDisallowedIfMultiplicityIsLessThanZero()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // when
            target.UpdateBeaconConfiguration(new BeaconConfiguration(-1, DataCollectionLevel.OFF, CrashReportingLevel.OFF));

            // then
            Assert.That(target.IsDataSendingAllowed, Is.False);
        }
Example #12
0
        public void GetSessionReturnsWrappedSession()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // when
            var obtained = target.Session;

            // then
            Assert.That(obtained, Is.SameAs(wrappedSession));
        }
Example #13
0
        public void IsEmptyCallsWrappedSession()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            target.ClearCapturedData();

            // when, then
            Assert.That(wrappedSession.IsEmpty, Is.True);
            Assert.That(target.IsEmpty, Is.True);
        }
Example #14
0
        public void TheSessionIsFinishedAfterCallingFinishSession()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // when
            target.FinishSession();

            // then
            Assert.That(target.IsSessionFinished, Is.True);
        }
Example #15
0
        public void EndCallsWrappedSession()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            // ensure that session is not ended
            Assert.That(wrappedSession.IsSessionEnded, Is.False);

            // when
            target.End();

            // verify forwarded calls
            Assert.That(wrappedSession.IsSessionEnded, Is.True);
        }
Example #16
0
        public void AfterUpdatingTheBeaconConfigurationTheBeaconConfigurationIsSet()
        {
            // given
            var target           = new SessionWrapper(wrappedSession);
            var newConfiguration = new BeaconConfiguration(42, DataCollectionLevel.OFF, CrashReportingLevel.OFF);

            // when updating
            target.UpdateBeaconConfiguration(newConfiguration);

            // then
            Assert.That(target.IsBeaconConfigurationSet, Is.True);

            // also verify that Session has been invoked
            Assert.That(wrappedSession.BeaconConfiguration, Is.SameAs(newConfiguration));
        }
Example #17
0
        public void ClearCapturedDataClearsDataFromSession()
        {
            // given
            var target = new SessionWrapper(wrappedSession);

            wrappedSession.EnterAction("foo").LeaveAction();

            // ensure data is not empty
            Assert.That(wrappedSession.IsEmpty, Is.False);

            // when
            target.ClearCapturedData();

            // verify forwarded calls - now wrapped session must be empty
            Assert.That(wrappedSession.IsEmpty, Is.True);
        }
        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);
        }
Example #19
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);
        }
Example #20
0
        public void ABeaconSendingFlushSessionsStateClosesOpenSessions()
        {
            // given
            var target = new BeaconSendingFlushSessionsState();

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

            sessionOne.UpdateBeaconConfiguration(new BeaconConfiguration(1, DataCollectionLevel.OFF, CrashReportingLevel.OFF));
            var sessionTwo = new SessionWrapper(CreateValidSession("127.0.0.2"));

            sessionTwo.UpdateBeaconConfiguration(new BeaconConfiguration(1, DataCollectionLevel.OFF, CrashReportingLevel.OFF));

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

            // when
            target.Execute(context);

            // verify that open sessions are closed
            context.Received(1).FinishSession(sessionOne.Session);
            context.Received(1).FinishSession(sessionTwo.Session);
        }
Example #21
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);
        }
Example #22
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>();
        }
Example #23
0
        public void ABeaconSendingFlushSessionsStateConfiguresAllUnconfiguredSessions()
        {
            // given
            var target = new BeaconSendingFlushSessionsState();

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

            // end one session to demonstrate that those which are already ended are also configured
            sessionThree.End();
            newSessions.AddRange(new[] { sessionOne, sessionTwo, sessionThree });

            // when
            target.Execute(context);

            // verify that all three sessions are configured
            Assert.That(sessionOne.IsBeaconConfigurationSet, Is.True);
            Assert.That(sessionOne.BeaconConfiguration.Multiplicity, Is.EqualTo(1));
            Assert.That(sessionTwo.IsBeaconConfigurationSet, Is.True);
            Assert.That(sessionTwo.BeaconConfiguration.Multiplicity, Is.EqualTo(1));
            Assert.That(sessionThree.IsBeaconConfigurationSet, Is.True);
            Assert.That(sessionThree.BeaconConfiguration.Multiplicity, Is.EqualTo(1));
        }