public async Task OutgoingMessages_LimitedToOnePerSecond()
        {
            var evt    = new CountdownEvent(3);
            var driver = new CountdownDriver(evt);

            var bot = await SlackBot.InitializeAsync(driver, this.bus);

            await bot.SendAsync("test", "test", false, null);

            await bot.SendAsync("test", "test", false, null);

            await bot.SendAsync("test", "test", false, null);

            if (!evt.Wait(TimeSpan.FromSeconds(4)))
            {
                Assert.True(false, "3 messages were not recorded as being sent");
            }

            Assert.Equal(3, driver.RecordedTimings.Count);

            TimeSpan TimeBetween(int p1, int p2)
            => driver
            .RecordedTimings[Math.Max(p1, p2)]
            .Subtract(
                driver
                .RecordedTimings[Math.Min(p1, p2)]);

            Assert.True(TimeBetween(0, 1) > TimeSpan.FromSeconds(1));
            Assert.True(TimeBetween(1, 2) > TimeSpan.FromSeconds(1));
        }
Exemple #2
0
        public async Task OutgoingMessages_LimitedToOnePerSecond()
        {
            var evt    = new CountdownEvent(3);
            var driver = new CountdownDriver(evt);

            var bot = await SlackBot.InitializeAsync(driver, this.bus);

            await bot.SendAsync("test", "test", false, null);

            await bot.SendAsync("test", "test", false, null);

            await bot.SendAsync("test", "test", false, null);

            if (!evt.Wait(TimeSpan.FromSeconds(6)))
            {
                Assert.True(false, "3 messages were not recorded as being sent");
            }

            Assert.Equal(3, driver.RecordedTimings.Count);

            TimeSpan TimeBetween(int p1, int p2)
            => driver
            .RecordedTimings[Math.Max(p1, p2)]
            .Subtract(
                driver
                .RecordedTimings[Math.Min(p1, p2)]);

            var first  = TimeBetween(0, 1);
            var second = TimeBetween(1, 2);

            // allow some wiggle room with timing
            var measure = TimeSpan.FromSeconds(0.92);

            Assert.True(TimeBetween(0, 1) >= measure, $"Expected {first} >= {measure}");
            Assert.True(TimeBetween(1, 2) >= measure, $"Expected {second} >= {measure}");
        }