public void Maintenance_Cancellation() { var testTask = new TestMaintenanceTask { WaitingSeconds = 6 }; // define tasks and start the service var maintenanceService = new SnMaintenance(new[] { testTask }, null) { // very short cycle TimerInterval = 3 }; maintenanceService.StartAsync(CancellationToken.None).Wait(); // wait for a few cycles (a 6-second task should be executed 2 times) Thread.Sleep(14 * 1000); maintenanceService.StopAsync(CancellationToken.None).Wait(TimeSpan.FromSeconds(30)); var callCount1 = testTask.Calls; // wait for a while and get the count again Thread.Sleep(7000); var callCount2 = testTask.Calls; // the task should have been executed at least a few times Assert.IsTrue(callCount1 >= 2 && callCount1 < 4, $"call count: {callCount1}"); // there may be a small difference, but not bigger than 1 Assert.IsTrue(callCount2 - callCount1 < 2); }
private int MaintenanceTest(double taskDelayMinutes, int sleep) { // activate the service if needed var running = SnMaintenance.Running(); var maintenanceService = new SnMaintenance(); if (!running) { maintenanceService.Start(); } // activate the test task TestMaintenanceTask.Enabled = false; TestMaintenanceTask.Calls = 0; TestMaintenanceTask.WaitingMinutesForTests = taskDelayMinutes; TestMaintenanceTask.Enabled = true; Thread.Sleep(sleep); TestMaintenanceTask.Enabled = false; if (!running) { maintenanceService.Shutdown(); } return(TestMaintenanceTask.Calls); }
public void Maintenance_Cancellation() { Test(() => { var hackedDataProvider = new HackedInMemoryBlobStorageMetaDataProvider(); BlobStorageComponents.DataProvider = hackedDataProvider; var running = SnMaintenance.Running(); var maintenanceService = new SnMaintenance(); if (!running) { maintenanceService.Start(); } try { // Start the real maintenance immediatelly on a hacked system System.Threading.Tasks.Task.Run(() => SnMaintenanceAcc.InvokeStatic("CleanupFiles")); // Let it work hard Thread.Sleep(1000); // Simulate system's cooldown maintenanceService.Shutdown(); // Get count of deletions var count1 = hackedDataProvider.CountOfCleanupCalled; // Wait for a while and get the count again Thread.Sleep(1000); var count2 = hackedDataProvider.CountOfCleanupCalled; // Repeat again Thread.Sleep(1000); var count3 = hackedDataProvider.CountOfCleanupCalled; // Maybe a deletion cycle ran at the moment of shutdown Assert.IsTrue(count2 - count1 < 2); // The system certainly stopped at the second time. Assert.AreEqual(count2, count3); } finally { TestMaintenanceTask.Enabled = false; if (!running) { maintenanceService.Shutdown(); } } }); }
private int MaintenanceTest(int taskDelaySeconds, int sleepSeconds) { var testTask = new TestMaintenanceTask { WaitingSeconds = taskDelaySeconds }; // define tasks and start the service var maintenanceService = new SnMaintenance(new[] { testTask }, null) { // very short cycle TimerInterval = 3 }; maintenanceService.StartAsync(CancellationToken.None).Wait(); // wait for a predefined period Thread.Sleep(sleepSeconds * 1000); maintenanceService.StopAsync(CancellationToken.None).Wait(TimeSpan.FromSeconds(30)); return(testTask.Calls); }