Ejemplo n.º 1
0
            public void SetUp()
            {
                _slackGatewaySpy = new SlackGatewaySpy();
                _clock           = new ClockStub(
                    new DateTimeOffset(
                        new DateTime(2019, 03, 01, 10, 30, 0)
                        )
                    );

                _getBillablePeople = new GetBillablePeople(_slackGatewaySpy, _clock);

                _slackGatewayStub = new SlackGatewayStub
                {
                    BillablePeople = new[]
                    {
                        new SlackBillablePerson
                        {
                            Email = "*****@*****.**",
                            Id    = "U8723"
                        },
                        new SlackBillablePerson
                        {
                            Email = "*****@*****.**",
                            Id    = "U9999"
                        },
                        new SlackBillablePerson
                        {
                            Email = "*****@*****.**",
                            Id    = "U9998"
                        }
                    }
                };

                _getBillablePeopleWithExclusions = new GetBillablePeople(_slackGatewayStub, _clock);
            }
        public void CanOnlyRemindBillablePeople()
        {
            var clock = new ClockStub(
                new DateTimeOffset(
                    new DateTime(2019, 03, 01, 10, 30, 0)
                    )
                );

            Environment.SetEnvironmentVariable("NON_BILLABLE_PEOPLE", "[email protected],[email protected]");

            var getBillablePeople = new GetBillablePeople(_slackGateway, clock);

            var remindBillablePeople = new RemindBillablePeople(getBillablePeople, _sendReminder);

            remindBillablePeople.Execute(
                new RemindBillablePeopleRequest
            {
                Message = "Please make sure your timesheet is submitted by 13:30 today."
            }
                );

            _slackApi.ReceivedRequests.Count.Should().Be(3);

            _slackApi.ReceivedRequests.Should()
            .Contain(request => request.Url.ToString() == SlackApiAddress + SlackApiPostMessagePath);

            Environment.SetEnvironmentVariable("NON_BILLABLE_PEOPLE", null);
        }
        public void RequestProxied_FailureMovedOutOfDetectionWindow_MarkDestinationHealthy()
        {
            var options = Options.Create(
                new TransportFailureRateHealthPolicyOptions {
                DefaultFailureRateLimit = 0.5, DetectionWindowSize = TimeSpan.FromSeconds(30), MinimalTotalCountThreshold = 1
            });
            var clock = new ClockStub {
                TickCount = 10000
            };
            var healthUpdater = new Mock <IDestinationHealthUpdater>();
            var policy        = new TransportFailureRateHealthPolicy(options, clock, healthUpdater.Object);

            var cluster = GetClusterInfo("cluster0", destinationCount: 2);

            // Initial failed requests
            for (var i = 0; i < 2; i++)
            {
                policy.RequestProxied(cluster, cluster.DestinationManager.Items[1], GetFailedRequestContext(ProxyError.RequestTimedOut));
                clock.TickCount += 1000;
            }

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Unhealthy, TimeSpan.FromSeconds(60)), Times.Exactly(2));
            healthUpdater.VerifyNoOtherCalls();

            // Successful requests
            for (var i = 0; i < 4; i++)
            {
                policy.RequestProxied(cluster, cluster.DestinationManager.Items[0], new DefaultHttpContext());
                policy.RequestProxied(cluster, cluster.DestinationManager.Items[1], new DefaultHttpContext());
                clock.TickCount += 5000;
            }

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[0], DestinationHealth.Healthy, TimeSpan.FromSeconds(60)), Times.Exactly(4));
            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Healthy, TimeSpan.FromSeconds(60)), Times.Exactly(2));
            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Unhealthy, TimeSpan.FromSeconds(60)), Times.Exactly(4));
            healthUpdater.VerifyNoOtherCalls();

            // Failed requests
            for (var i = 0; i < 2; i++)
            {
                policy.RequestProxied(cluster, cluster.DestinationManager.Items[1], GetFailedRequestContext(ProxyError.RequestTimedOut));
                clock.TickCount += 1;
            }

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Healthy, TimeSpan.FromSeconds(60)), Times.Exactly(3));
            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Unhealthy, TimeSpan.FromSeconds(60)), Times.Exactly(5));
            healthUpdater.VerifyNoOtherCalls();

            // Shift the detection window to the future
            clock.TickCount += 10998;

            // New failed request, but 2 oldest failures have moved out of the detection window
            policy.RequestProxied(cluster, cluster.DestinationManager.Items[1], GetFailedRequestContext(ProxyError.RequestTimedOut));

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Healthy, TimeSpan.FromSeconds(60)), Times.Exactly(4));
            healthUpdater.VerifyNoOtherCalls();
        }
Ejemplo n.º 4
0
        public void GivenANewTusLibrosRestAPI_WhenListingAnExpiredCartWithItems_ThenAnExceptionIsThrown()
        {
            var clockStub = new ClockStub(new DateTime(2020, 4, 28, 5, 0, 0));
            var sut       = new TusLibrosRestAPIStubBuilder()
                            .AuthenticatesWith(new AuthenticatorStubBuilder()
                                               .Returns(true)
                                               .Build())
                            .MeasuresTimeWith(clockStub)
                            .UsesCatalog(new List <object> {
                VALID_ITEM
            })
                            .Build();

            var cartId = sut.CreateCart("validClientId", "validPassword");

            sut.AddToCart(cartId, VALID_ITEM, 1);
            clockStub.AddMinutes(31);

            var exception = Assert.Throws <ArgumentException>(() => sut.ListCart(cartId));

            Assert.Equal(TusLibrosRestAPI.CART_HAS_EXPIRED_ERROR, exception.Message);
        }
        public void CanRemindBillablePeopleEndOfTheMonth()
        {
            var clock = new ClockStub(
                new DateTimeOffset(
                    new DateTime(2019, 07, 31, 10, 30, 0)
                    )
                );

            var getBillablePeople = new GetBillablePeople(_slackGateway, clock);

            var remindBillablePeople = new RemindBillablePeople(getBillablePeople, _sendReminder);

            remindBillablePeople.Execute(
                new RemindBillablePeopleRequest
            {
                Message = "Please make sure your timesheet is submitted by 13:30 today."
            }
                );

            _slackApi.ReceivedRequests.Should()
            .Contain(request => request.RawUrl.ToString() == "/" + SlackApiUsersPath);
            _slackApi.ReceivedRequests.Count(request => request.RawUrl.ToString() == "/" + SlackApiPostMessagePath)
            .Should().Be(4);
        }
        public void CanRemindBillablePeopleOnAFriday()
        {
            var clock = new ClockStub(
                new DateTimeOffset(
                    new DateTime(2019, 03, 01, 10, 30, 0)
                    )
                );

            var getBillablePeople = new GetBillablePeople(_slackGateway, clock);

            var remindBillablePeople = new RemindBillablePeople(getBillablePeople, _sendReminder);

            remindBillablePeople.Execute(
                new RemindBillablePeopleRequest
            {
                Message = "Please make sure your timesheet is submitted by 13:30 today."
            }
                );

            _slackApi.ReceivedRequests.Count.Should().Be(5);

            _slackApi.ReceivedRequests.Should()
            .Contain(request => request.Url.ToString() == SlackApiAddress + SlackApiPostMessagePath);
        }
Ejemplo n.º 7
0
 public CustomerFacts()
 {
     Clock = new ClockStub();
 }
 public BaseDateTimeTests()
 {
     Clock = new ClockStub();
 }
        public void RequestProxied_FailureRateLimitExceeded_MarkDestinationUnhealthy()
        {
            var options = Options.Create(
                new TransportFailureRateHealthPolicyOptions {
                DefaultFailureRateLimit = 0.5, DetectionWindowSize = TimeSpan.FromSeconds(30), MinimalTotalCountThreshold = 1
            });
            var clock = new ClockStub {
                TickCount = 10000
            };
            var healthUpdater = new Mock <IDestinationHealthUpdater>();
            var policy        = new TransportFailureRateHealthPolicy(options, clock, healthUpdater.Object);

            Assert.Equal(HealthCheckConstants.PassivePolicy.TransportFailureRate, policy.Name);

            var reactivationPeriod0 = TimeSpan.FromSeconds(60);
            var reactivationPeriod1 = TimeSpan.FromSeconds(100);
            var cluster0            = GetClusterInfo("cluster0", destinationCount: 2);
            var cluster1            = GetClusterInfo("cluster1", destinationCount: 2, failureRateLimit: 0.61, reactivationPeriod1);

            // Initial state
            Assert.All(cluster0.DestinationManager.Items, d => Assert.Equal(DestinationHealth.Unknown, d.Health.Passive));
            Assert.All(cluster1.DestinationManager.Items, d => Assert.Equal(DestinationHealth.Unknown, d.Health.Passive));

            // Successful requests
            for (var i = 0; i < 3; i++)
            {
                policy.RequestProxied(cluster0, cluster0.DestinationManager.Items[0], new DefaultHttpContext());
                policy.RequestProxied(cluster0, cluster0.DestinationManager.Items[1], new DefaultHttpContext());
                policy.RequestProxied(cluster1, cluster1.DestinationManager.Items[0], new DefaultHttpContext());
                policy.RequestProxied(cluster1, cluster1.DestinationManager.Items[1], new DefaultHttpContext());
                clock.TickCount += 4000;
            }

            healthUpdater.Verify(u => u.SetPassive(cluster0, cluster0.DestinationManager.Items[0], DestinationHealth.Healthy, reactivationPeriod0), Times.Exactly(3));
            healthUpdater.Verify(u => u.SetPassive(cluster0, cluster0.DestinationManager.Items[1], DestinationHealth.Healthy, reactivationPeriod0), Times.Exactly(3));
            healthUpdater.Verify(u => u.SetPassive(cluster1, cluster1.DestinationManager.Items[0], DestinationHealth.Healthy, reactivationPeriod1), Times.Exactly(3));
            healthUpdater.Verify(u => u.SetPassive(cluster1, cluster1.DestinationManager.Items[1], DestinationHealth.Healthy, reactivationPeriod1), Times.Exactly(3));
            healthUpdater.VerifyNoOtherCalls();

            // Failed requests
            for (var i = 0; i < 3; i++)
            {
                policy.RequestProxied(cluster0, cluster0.DestinationManager.Items[1], GetFailedRequestContext(ProxyError.RequestTimedOut));
                policy.RequestProxied(cluster1, cluster1.DestinationManager.Items[0], GetFailedRequestContext(ProxyError.Request));
                clock.TickCount += 4000;
            }

            healthUpdater.Verify(u => u.SetPassive(cluster0, cluster0.DestinationManager.Items[1], DestinationHealth.Healthy, reactivationPeriod0), Times.Exactly(5));
            healthUpdater.Verify(u => u.SetPassive(cluster0, cluster0.DestinationManager.Items[1], DestinationHealth.Unhealthy, reactivationPeriod0), Times.Once);
            healthUpdater.Verify(u => u.SetPassive(cluster1, cluster1.DestinationManager.Items[0], DestinationHealth.Healthy, reactivationPeriod1), Times.Exactly(6));
            healthUpdater.VerifyNoOtherCalls();

            // Two more failed requests
            policy.RequestProxied(cluster1, cluster1.DestinationManager.Items[0], GetFailedRequestContext(ProxyError.Request));
            // End of the detection window
            clock.TickCount += 6000;
            policy.RequestProxied(cluster1, cluster1.DestinationManager.Items[0], GetFailedRequestContext(ProxyError.Request));

            healthUpdater.Verify(u => u.SetPassive(cluster1, cluster1.DestinationManager.Items[0], DestinationHealth.Healthy, reactivationPeriod1), Times.Exactly(7));
            healthUpdater.Verify(u => u.SetPassive(cluster1, cluster1.DestinationManager.Items[0], DestinationHealth.Unhealthy, reactivationPeriod1), Times.Once);
            healthUpdater.VerifyNoOtherCalls();
        }
        public void RequestProxied_MultipleConcurrentRequests_MarkDestinationUnhealthyAndHealthyAgain()
        {
            var options = Options.Create(
                new TransportFailureRateHealthPolicyOptions {
                DefaultFailureRateLimit = 0.5, DetectionWindowSize = TimeSpan.FromSeconds(30), MinimalTotalCountThreshold = 1
            });
            var clock = new ClockStub {
                TickCount = 10000
            };
            var healthUpdater      = new Mock <IDestinationHealthUpdater>();
            var reactivationPeriod = TimeSpan.FromSeconds(15);
            var policy             = new TransportFailureRateHealthPolicy(options, clock, healthUpdater.Object);

            var cluster = GetClusterInfo("cluster0", destinationCount: 2, reactivationPeriod: reactivationPeriod);

            // Initial state
            Assert.All(cluster.DestinationManager.Items, d => Assert.Equal(DestinationHealth.Unknown, d.Health.Passive));

            // Initial sucessful requests
            for (var i = 0; i < 2; i++)
            {
                policy.RequestProxied(cluster, cluster.DestinationManager.Items[1], new DefaultHttpContext());
            }

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Healthy, reactivationPeriod), Times.Exactly(2));
            healthUpdater.VerifyNoOtherCalls();

            // Concurrent failed requests.
            // They are 'concurrent' because the clock is not updated.
            for (var i = 0; i < 2; i++)
            {
                policy.RequestProxied(cluster, cluster.DestinationManager.Items[1], GetFailedRequestContext(ProxyError.RequestTimedOut));
            }

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Healthy, reactivationPeriod), Times.Exactly(3));
            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Unhealthy, reactivationPeriod), Times.Once);
            healthUpdater.VerifyNoOtherCalls();

            // More successful requests
            for (var i = 0; i < 2; i++)
            {
                policy.RequestProxied(cluster, cluster.DestinationManager.Items[1], new DefaultHttpContext());
                clock.TickCount += 100;
            }

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Healthy, reactivationPeriod), Times.Exactly(5));
            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Unhealthy, reactivationPeriod), Times.Once);
            healthUpdater.VerifyNoOtherCalls();

            // More failed requests
            for (var i = 0; i < 2; i++)
            {
                policy.RequestProxied(cluster, cluster.DestinationManager.Items[1], GetFailedRequestContext(ProxyError.RequestTimedOut));
                clock.TickCount += 100;
            }

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Healthy, reactivationPeriod), Times.Exactly(6));
            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[1], DestinationHealth.Unhealthy, reactivationPeriod), Times.Exactly(2));
            healthUpdater.VerifyNoOtherCalls();

            policy.RequestProxied(cluster, cluster.DestinationManager.Items[0], new DefaultHttpContext());

            healthUpdater.Verify(u => u.SetPassive(cluster, cluster.DestinationManager.Items[0], DestinationHealth.Healthy, reactivationPeriod), Times.Once);
            healthUpdater.VerifyNoOtherCalls();
        }