Example #1
0
        public void OnRateLimitChangedIsTriggeredCorrectly()
        {
            var rateLimitChanges = new List <RateLimitChangedNotification <int> >();

            Action <RateLimitChangedNotification <int> > onRateLimitChanged = rateLimitChanges.Add;

            var rateLimit1 = new RateLimit(1, TimeSpan.FromSeconds(1));
            var rateLimit2 = new RateLimit(2, TimeSpan.FromMinutes(1));

            var rateLimiter = new RateLimiter <int>(new[]
            {
                new KeyValuePair <int, RateLimit>(1, rateLimit1),
                new KeyValuePair <int, RateLimit>(2, rateLimit2)
            }, onRateLimitChanged: onRateLimitChanged);

            rateLimiter.SetRateLimit(1, rateLimit1);

            rateLimitChanges.Should().BeEmpty();

            rateLimiter.SetRateLimit(1, rateLimit2);

            var expected1 = new RateLimitChangedNotification <int>(
                1,
                rateLimit1.AsLocal(1),
                rateLimit2.AsLocal(1),
                rateLimit1,
                rateLimit2);

            rateLimitChanges.Should().ContainSingle()
            .And.BeEquivalentTo(expected1);

            rateLimiter.SetRateLimit(2, rateLimit1);

            var expected2 = new RateLimitChangedNotification <int>(
                2,
                rateLimit2.AsLocal(1),
                rateLimit1.AsLocal(1),
                rateLimit2,
                rateLimit1);

            rateLimitChanges.Should().HaveCount(2)
            .And.HaveElementAt(1, expected2);
        }