Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }