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)); }
public void Setup() { config = new TestConfiguration(); clientProvider = Substitute.For <IHTTPClientProvider>(); timingProvider = Substitute.For <ITimingProvider>(); nonTerminalStateMock = Substitute.For <AbstractBeaconSendingState>(false); }
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)); }
public void StateTransitionToCaptureOffIsMadeIfTooManyRequestsResponseIsReceived() { // given var responseHeaders = new Dictionary <string, List <string> > { { Response.ResponseKeyRetryAfter, new List <string> { "456" } } }; var response = new TimeSyncResponse(Substitute.For <ILogger>(), string.Empty, Response.HttpTooManyRequests, responseHeaders); httpClient.SendTimeSyncRequest().Returns(response); context.IsTimeSyncSupported.Returns(true); context.IsCaptureOn.Returns(true); AbstractBeaconSendingState capturedState = null; context.NextState = Arg.Do <AbstractBeaconSendingState>(x => capturedState = x); var target = new BeaconSendingTimeSyncState(true); // when target.Execute(context); // then context.ReceivedWithAnyArgs(1).NextState = null; Assert.That(capturedState, Is.Not.Null); Assert.That(capturedState, Is.InstanceOf <BeaconSendingCaptureOffState>()); Assert.That(((BeaconSendingCaptureOffState)capturedState).sleepTimeInMilliseconds, Is.EqualTo(456 * 1000)); }
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 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); }
internal BeaconSendingContext( ILogger logger, IHttpClientConfiguration httpClientConfiguration, IHttpClientProvider httpClientProvider, ITimingProvider timingProvider, IInterruptibleThreadSuspender threadSuspender, AbstractBeaconSendingState initialState ) { this.logger = logger; this.httpClientConfiguration = httpClientConfiguration; serverConfiguration = ServerConfiguration.Default; HttpClientProvider = httpClientProvider; this.timingProvider = timingProvider; this.threadSuspender = threadSuspender; lastResponseAttributes = ResponseAttributes.WithUndefinedDefaults().Build(); CurrentState = initialState; }
internal TestBeaconSendingContextBuilder With(AbstractBeaconSendingState state) { initState = state; return(this); }