Esempio n. 1
0
        public void CleanupTest()
        {
            var time   = T(TimeSpan.Zero);
            var target = new EventCounter(() => time, TimeSpan.FromMinutes(1));

            target.Count("a");
            target.Count("a");
            target.Count("a");
            time = T(TimeSpan.FromMinutes(5));

            target.GetSnapshot()
            .Should().Contain(i => i.Key == "a");
            target.Cleanup();
            target.GetSnapshot()
            .Should().NotContain(i => i.Key == "a");
        }
Esempio n. 2
0
        public async Task ConcurrencyTest()
        {
            for (int r = 0; r < 30; r++)
            {
                var target = new EventCounter(() => DateTimeOffset.Now, TimeSpan.FromHours(1));
                var random = new Random();

                var tasks = Enumerable.Range(1, 15)
                            .Select(_ => Task.Run(() =>
                {
                    for (var n = 0; n < 1000; n++)
                    {
                        target.Count(random.Next(30).ToString());
                    }
                })).ToArray();

                await Task.WhenAll(tasks);

                target.GetSnapshot()
                .Sum(i => i.Value)
                .Should().Be(15000);
            }
        }