Ejemplo n.º 1
0
        public async Task TimerWorks()
        {
            using var timer = new SlottedTimer(50);
            using var cts   = new CancellationTokenSource(2000);

            var sw1 = Stopwatch.StartNew();
            var sw2 = Stopwatch.StartNew();
            var t1  = Task.Run(async() =>
            {
                await timer.WaitAsync(100, cts.Token);
                sw1.Stop();
            });
            var t2 = Task.Run(async() =>
            {
                await timer.WaitAsync(1000, cts.Token);
                sw2.Stop();
            });
            await Task.WhenAll(t1, t2);

            Assert.IsTrue(sw1.ElapsedMilliseconds > 50 && sw1.ElapsedMilliseconds < 150);
            Assert.IsTrue(sw2.ElapsedMilliseconds > 900 && sw1.ElapsedMilliseconds < 1100);
        }
Ejemplo n.º 2
0
 public async Task CancellationWorks()
 {
     using var timer = new SlottedTimer(50);
     using var cts   = new CancellationTokenSource(900);
     await Assert.ThrowsExceptionAsync <OperationCanceledException>(() => Task.Run(async() => await timer.WaitAsync(1000, cts.Token)));
 }