public void QuickPulseTelemetryModuleManagesTimersCorrectly() { if (QuickPulseTelemetryModuleTests.Ignored) { return; } // ARRANGE var pollingInterval = TimeSpan.FromSeconds(1); var collectionInterval = TimeSpan.FromMilliseconds(400); var timings = new QuickPulseTimings(pollingInterval, collectionInterval); var collectionTimeSlotManager = new QuickPulseCollectionTimeSlotManagerMock(timings); var serviceClient = new QuickPulseServiceClientMock { ReturnValueFromPing = false, ReturnValueFromSubmitSample = true }; var performanceCollector = new PerformanceCollectorMock(); var topCpuCollector = new QuickPulseTopCpuCollectorMock(); var module = new QuickPulseTelemetryModule(collectionTimeSlotManager, null, serviceClient, performanceCollector, topCpuCollector, timings); // ACT & ASSERT module.Initialize(new TelemetryConfiguration() { InstrumentationKey = "some ikey" }); // initially, the module is in the polling state Thread.Sleep((int)(2.5 * pollingInterval.TotalMilliseconds)); serviceClient.CountersEnabled = false; // 2.5 polling intervals have elapsed, we must have pinged the service 3 times (the first time immediately upon initialization), but no samples yet Assert.AreEqual(3, serviceClient.PingCount, "Ping count 1"); Assert.AreEqual(0, serviceClient.SnappedSamples.Count, "Sample count 1"); // now the service wants the data serviceClient.Reset(); serviceClient.ReturnValueFromPing = true; serviceClient.ReturnValueFromSubmitSample = true; serviceClient.CountersEnabled = true; Thread.Sleep((int)(5 * collectionInterval.TotalMilliseconds)); serviceClient.CountersEnabled = false; // a number of collection intervals have elapsed, we must have pinged the service once, and then started sending samples Assert.AreEqual(1, serviceClient.PingCount, "Ping count 2"); Assert.IsTrue(serviceClient.SnappedSamples.Count > 0, "Sample count 2"); lock (serviceClient.ResponseLock) { // the service doesn't want the data anymore serviceClient.ReturnValueFromPing = false; serviceClient.ReturnValueFromSubmitSample = false; serviceClient.Reset(); serviceClient.CountersEnabled = true; } Thread.Sleep((int)(2.9 * pollingInterval.TotalMilliseconds)); serviceClient.CountersEnabled = false; // 2 polling intervals have elapsed, we must have submitted one batch of samples, stopped collecting and pinged the service twice afterwards Assert.AreEqual(1, serviceClient.SnappedSamples.Count / serviceClient.LastSampleBatchSize, "Sample count 3"); Assert.AreEqual(2, serviceClient.PingCount, "Ping count 3"); }