예제 #1
0
        private static void TimestampGeneratorLogAfterCooldownTest(ITimestampGenerator generator,
                                                                   TestHelper.TestLoggerHandler loggerHandler)
        {
            // It should generate a warning initially and then 1 more
            var    maxElapsed = TimeSpan.FromSeconds(1.8);
            var    counter    = 0;
            Action action     = () =>
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();
                // ReSharper disable once AccessToModifiedClosure
                while (stopWatch.Elapsed < maxElapsed)
                {
                    for (var i = 0; i < 5000; i++)
                    {
                        generator.Next();
                        Interlocked.Increment(ref counter);
                    }
                }
            };

            TestHelper.ParallelInvoke(action, 2);
            if (Volatile.Read(ref counter) < 5000000)
            {
                // if during this time, we weren't able to generate a lot of values, don't mind
                Assert.Ignore("It was not able to generate 5M values");
            }
            Assert.AreEqual(2, loggerHandler.DequeueAllMessages().Count(i => i.Item1 == "warning"));
            // Cooldown: make current time > last generated value
            Thread.Sleep(3000);
            // It should generate a warning initially
            maxElapsed = TimeSpan.FromSeconds(0.8);
            TestHelper.ParallelInvoke(action, 2);
            Assert.AreEqual(1, loggerHandler.DequeueAllMessages().Count(i => i.Item1 == "warning"));
        }
예제 #2
0
        private static void TimestampGeneratorLogDriftingTest(ITimestampGenerator generator,
                                                              TestHelper.TestLoggerHandler loggerHandler)
        {
            // A little less than 3 seconds
            // It should generate a warning initially and then next 2 after 1 second each
            var maxElapsed = TimeSpan.FromSeconds(2.8);
            var counter    = 0;

            TestHelper.ParallelInvoke(() =>
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();
                while (stopWatch.Elapsed < maxElapsed)
                {
                    for (var i = 0; i < 10000; i++)
                    {
                        generator.Next();
                        Interlocked.Increment(ref counter);
                    }
                }
            }, 2);
            if (Volatile.Read(ref counter) < 5000000)
            {
                // if during this time, we weren't able to generate a lot of values, don't mind
                Assert.Ignore("It was not able to generate 5M values");
            }
            Assert.AreEqual(3, loggerHandler.DequeueAllMessages().Count(i => i.Item1 == "warning"));
        }