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();
                    }
                }
            });
        }