private static void TimestampGeneratorLogDriftingTest( ITimestampGenerator generator, TestHelper.TestLoggerHandler loggerHandler, int logIntervalMs) { var timestamp = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond; TestHelper.ParallelInvoke( () => { generator.Next(); }, 1000000); var elapsed = (DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond) - timestamp; if (elapsed > 3000) { Assert.Ignore("Generated numbers too slowly for this test to work."); } else { var count = elapsed / logIntervalMs; Assert.That(Interlocked.Read(ref loggerHandler.WarningCount), Is.InRange(count + 1, count + 2)); } }
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")); }
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")); }
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); 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(); } } }; TestHelper.ParallelInvoke(action, 2); 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")); }
/// <summary> /// Gets the timestamp of the request or null if not defined. /// </summary> private static long?GetRequestTimestamp(BatchStatement statement, ITimestampGenerator timestampGenerator) { if (statement.Timestamp != null) { return(TypeSerializer.SinceUnixEpoch(statement.Timestamp.Value).Ticks / 10); } var timestamp = timestampGenerator.Next(); return(timestamp != long.MinValue ? (long?)timestamp : null); }
/// <summary> /// Gets the timestamp of the request or null if not defined. /// </summary> /// <exception cref="NotSupportedException" /> private static long?GetRequestTimestamp(ProtocolVersion protocolVersion, BatchStatement statement, ITimestampGenerator timestampGenerator) { if (!protocolVersion.SupportsTimestamp()) { if (statement.Timestamp != null) { throw new NotSupportedException( "Timestamp for BATCH request is supported in Cassandra 2.1 or above."); } return(null); } if (statement.Timestamp != null) { return(TypeSerializer.SinceUnixEpoch(statement.Timestamp.Value).Ticks / 10); } var timestamp = timestampGenerator.Next(); return(timestamp != long.MinValue ? (long?)timestamp : null); }
private static void TimestampGeneratorMonitonicityTest(ITimestampGenerator generator) { // Use a set to determine the amount of different values const int iterations = 5000; const int threads = 8; var values = new ConcurrentDictionary <long, bool>(); TestHelper.ParallelInvoke(() => { var lastValue = 0L; for (var i = 0; i < iterations; i++) { var value = generator.Next(); Assert.Greater(value, lastValue); lastValue = value; values.TryAdd(value, true); } }, threads); Assert.AreEqual(iterations * threads, values.Count); }
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); TestHelper.ParallelInvoke(() => { var stopWatch = new Stopwatch(); stopWatch.Start(); while (stopWatch.Elapsed < maxElapsed) { for (var i = 0; i < 10000; i++) { generator.Next(); } } }, 2); Assert.AreEqual(3, loggerHandler.DequeueAllMessages().Count(i => i.Item1 == "warning")); }
private static void TimestampGeneratorLogAfterCooldownTest(ITimestampGenerator generator, TestHelper.TestLoggerHandler loggerHandler) { // It should generate a warning initially and then 1 more var counter = 0; void Action(TimeSpan maxElapsed) { var stopWatch = new Stopwatch(); stopWatch.Start(); while (stopWatch.Elapsed < maxElapsed) { for (var i = 0; i < 5000; i++) { generator.Next(); // ReSharper disable once AccessToModifiedClosure Interlocked.Increment(ref counter); } } } TestHelper.ParallelInvoke(() => Action(TimeSpan.FromSeconds(1.8)), 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.That(Interlocked.Read(ref loggerHandler.WarningCount), Is.GreaterThanOrEqualTo(2)); // Cooldown: make current time > last generated value Thread.Sleep(4000); // It should generate a warning initially TestHelper.ParallelInvoke(() => Action(TimeSpan.FromSeconds(0.8)), 2); Assert.That(Interlocked.Read(ref loggerHandler.WarningCount), Is.GreaterThanOrEqualTo(1)); }