public void WaitOne_ThrowsArgNullException_WhenWaitHandleIsNull()
        {
            var exception = Assert.Throws <ArgumentNullException>(
                () => TaskExtensions.WaitOne(null, TimeSpan.Zero, CancellationToken.None));

            Assert.Equal("waitHandle", exception.ParamName);
        }
        public void WaitOne_ReturnsTrue_WhenWaitHandleIsSignaled_AndTimeoutIsZero()
        {
            _mre.Set();

            var result = TaskExtensions.WaitOne(_mre, TimeSpan.Zero, _cts.Token);

            Assert.True(result);
        }
        public void WaitOne_ReturnsTrue_WhenWaitHandleIsSignaled()
        {
            _mre.Set();

            var result = TaskExtensions.WaitOne(_mre, Timeout.InfiniteTimeSpan, _cts.Token);

            Assert.True(result);
        }
        public void WaitOne_ThrowsOpCanceledException_WhenCancellationTokenIsCanceled()
        {
            _cts.Cancel();

            var exception = Assert.Throws <OperationCanceledException>(
                () => TaskExtensions.WaitOne(_mre, TimeSpan.Zero, _cts.Token));

            Assert.Equal(_cts.Token, exception.CancellationToken);
        }
        public void WaitOne_WaitsAndReturnsFalse_WhenNotSignaled_AndNonNullTimeout()
        {
            var sw     = Stopwatch.StartNew();
            var result = TaskExtensions.WaitOne(_mre, TimeSpan.FromMilliseconds(100), CancellationToken.None);

            sw.Stop();

            Assert.False(result);
            Assert.True(sw.Elapsed > TimeSpan.FromMilliseconds(95), sw.Elapsed.ToString());
        }
        public void WaitOne_ThrowsOpCanceledException_EvenWhenWaitHandleIsSignaled()
        {
            _cts.Cancel();
            _mre.Set();

            var exception = Assert.Throws <OperationCanceledException>(
                () => TaskExtensions.WaitOne(_mre, Timeout.InfiniteTimeSpan, _cts.Token));

            Assert.Equal(_cts.Token, exception.CancellationToken);
        }
Exemplo n.º 7
0
        public void WaitOne_WaitsAndReturnsFalse_WhenNotSignaled_AndNonNullTimeout()
        {
            using (var mre = new ManualResetEvent(false))
            {
                var sw     = Stopwatch.StartNew();
                var result = TaskExtensions.WaitOne(mre, TimeSpan.FromMilliseconds(500), CancellationToken.None);
                sw.Stop();

                Assert.False(result, "result != false");
                Assert.False(mre.WaitOne(TimeSpan.Zero), "mre is signaled");
                Assert.True(sw.Elapsed > TimeSpan.FromMilliseconds(450), $"Elapsed: {sw.Elapsed.TotalMilliseconds} ms, Expected: 450 ms");
            }
        }
        public void WaitOne_WaitsAndThrowsTaskCanceled_WhenNotSignaled_AndCancellationTokenIsCanceled()
        {
            var sw = Stopwatch.StartNew();

            _cts.CancelAfter(TimeSpan.FromMilliseconds(100));

            var exception = Assert.ThrowsAny <OperationCanceledException>(
                () => TaskExtensions.WaitOne(_mre, Timeout.InfiniteTimeSpan, _cts.Token));

            sw.Stop();

#if !NET452
            Assert.Equal(_cts.Token, exception.CancellationToken);
#else
            Assert.NotNull(exception);
#endif
            Assert.True(sw.Elapsed > TimeSpan.FromMilliseconds(95), sw.Elapsed.ToString());
        }
        public void WaitOne_ReturnsFalseImmediately_WhenNotSignaled_AndTimeoutIsZero()
        {
            var result = TaskExtensions.WaitOne(_mre, TimeSpan.Zero, CancellationToken.None);

            Assert.False(result);
        }