예제 #1
0
        public void Error()
        {
            AsyncAutoResetEvent autoEvent;

            // Verify that we get and [ObjectDisposedException] for [Set()] and [Reset()]
            // a disposed event.

            autoEvent = new AsyncAutoResetEvent();

            autoEvent.Dispose();
            Assert.Throws <ObjectDisposedException>(() => autoEvent.Set());
            Assert.Throws <ObjectDisposedException>(() => autoEvent.Reset());
            Task.Run(() => Assert.ThrowsAsync <ObjectDisposedException>(async() => await autoEvent.WaitAsync())).Wait();

            // Verify that disposing an event causes any waiting tasks
            // to unblock with an [ObjectDisposedException].

            autoEvent = new AsyncAutoResetEvent();

            var taskInfo     = new TaskStateCollection();
            var badException = false;

            for (int i = 0; i < taskInfo.Count; i++)
            {
                new Task(
                    async state =>
                {
                    int taskIndex = (int)state;

                    taskInfo[taskIndex].IsRunning = true;

                    try
                    {
                        await autoEvent.WaitAsync();
                    }
                    catch (ObjectDisposedException)
                    {
                        taskInfo[taskIndex].IsFaulted = true;
                    }
                    catch
                    {
                        badException = true;
                        taskInfo[taskIndex].IsFaulted = true;
                    }

                    taskInfo[taskIndex].IsComplete = true;
                },
                    i).Start();
            }

            NeonHelper.WaitFor(() => taskInfo.AllRunning, defaultTimeout);
            Assert.False(taskInfo.AnyComplete);

            autoEvent.Dispose();

            NeonHelper.WaitFor(() => taskInfo.AllFaulted, defaultTimeout);
            Assert.False(badException);
        }
        public void WhenEventIsNonSignaled_ItShouldMakeClientsWaitedAsyncWithTimeout()
        {
            var sut = new AsyncAutoResetEvent();

            sut.Reset();

            var task = sut.WaitAsync(TimeSpan.FromMilliseconds(300));

            Assert.ThrowsExceptionAsync <TimeoutException>(() => task);
        }
        public void WhenEventIsNonSignaled_ItShouldMakeClientsWaitedWithCancel()
        {
            var sut = new AsyncAutoResetEvent();

            sut.Reset();
            var ct = new CancellationTokenSource();

            ct.Cancel();

            Assert.ThrowsException <TaskCanceledException>(() => sut.Wait(ct.Token));
        }
예제 #4
0
        public void SetReset()
        {
            var Event = new AsyncAutoResetEvent();

            Event.Set();

            Assert.IsTrue(Event.IsSet);

            Event.Reset();

            Assert.IsFalse(Event.IsSet);
        }
        public void WhenEventIsNonSignaled_ItShouldMakeClientsWaitedAsyncWithTimeoutAndCancel()
        {
            var sut = new AsyncAutoResetEvent();

            sut.Reset();
            var ct   = new CancellationTokenSource();
            var task = sut.WaitAsync(TimeSpan.FromMilliseconds(1000), ct.Token);

            ConcurrentAssert.EnsureThatTaskIsNotCompletedIn(task, TimeSpan.FromMilliseconds(500));

            ct.Cancel();

            ConcurrentAssert.EnsureThatTaskIsCompleted(task);
            Assert.ThrowsExceptionAsync <TaskCanceledException>(() => task);
        }
        public void WhenEventIsNonSignaled_ItShouldMakeClientsWaitedAsyncWithCancel()
        {
            var sut = new AsyncAutoResetEvent();

            sut.Reset();
            var ct   = new CancellationTokenSource();
            var task = sut.WaitAsync(ct.Token);

            ConcurrentAssert.EnsureThatTaskIsNeverCompleted(task);

            ct.Cancel();

            ConcurrentAssert.EnsureThatTaskIsCompleted(task);
            Assert.ThrowsExceptionAsync <TaskCanceledException>(() => task);
        }
예제 #7
0
        public void SetResetWait()
        {
            var Event = new AsyncAutoResetEvent();

            Event.Set();

            Assert.IsTrue(Event.IsSet);

            Event.Reset();

            Assert.IsFalse(Event.IsSet);

            var Task = Event.Wait();

            Assert.IsFalse(Task.IsCompleted);
        }
예제 #8
0
        public void Basic()
        {
            // Verify that an event that starts out unsignalled doesn't allow
            // any tasks to execute.

            using (var autoEvent = new AsyncAutoResetEvent(false))
            {
                var taskInfo = new TaskStateCollection();

                for (int i = 0; i < taskInfo.Count; i++)
                {
                    new Task(
                        async state =>
                    {
                        int taskIndex = (int)state;

                        taskInfo[taskIndex].IsRunning = true;

                        try
                        {
                            await autoEvent.WaitAsync();
                            taskInfo[taskIndex].IsComplete = true;
                        }
                        catch (ObjectDisposedException)
                        {
                            // Ignore these
                        }
                    },
                        i).Start();
                }

                NeonHelper.WaitFor(() => taskInfo.AllRunning, defaultTimeout);
                Thread.Sleep(TimeSpan.FromSeconds(5));
                Assert.False(taskInfo.AnyComplete);
            }

            // Verify that an event that starts out signalled but then
            // resetting it doesn't allow any tasks to execute.

            using (var autoEvent = new AsyncAutoResetEvent(true))
            {
                autoEvent.Reset();

                var taskInfo = new TaskStateCollection();

                for (int i = 0; i < taskInfo.Count; i++)
                {
                    new Task(
                        async state =>
                    {
                        int taskIndex = (int)state;

                        taskInfo[taskIndex].IsRunning = true;

                        try
                        {
                            await autoEvent.WaitAsync();
                            taskInfo[taskIndex].IsComplete = true;
                        }
                        catch (ObjectDisposedException)
                        {
                            // Ignore these
                        }
                    },
                        i).Start();
                }

                NeonHelper.WaitFor(() => taskInfo.AllRunning, defaultTimeout);
                Thread.Sleep(TimeSpan.FromSeconds(5));
                Assert.False(taskInfo.AnyComplete);
            }

            // Verify that an event that starts out unsignalled doesn't allow
            // any tasks to execute and then that every time the event is signalled,
            // a single task is unblocked.

            using (var autoEvent = new AsyncAutoResetEvent(false))
            {
                var taskInfo = new TaskStateCollection();

                for (int i = 0; i < taskInfo.Count; i++)
                {
                    new Task(
                        async state =>
                    {
                        int taskIndex = (int)state;

                        taskInfo[taskIndex].IsRunning = true;

                        try
                        {
                            await autoEvent.WaitAsync();
                            taskInfo[taskIndex].IsComplete = true;
                        }
                        catch (ObjectDisposedException)
                        {
                            // Ignore these
                        }
                    },
                        i).Start();
                }

                NeonHelper.WaitFor(() => taskInfo.AllRunning, defaultTimeout);
                Thread.Sleep(TimeSpan.FromSeconds(5));
                Assert.False(taskInfo.AnyComplete);

                for (int i = 0; i < taskInfo.Count; i++)
                {
                    autoEvent.Set();

                    NeonHelper.WaitFor(
                        () =>
                    {
                        return(taskInfo.Where(ti => ti.IsComplete).Count() == i + 1);
                    },
                        defaultTimeout);
                }

                // Also verify that disposing the event multiple time isn't a problem.

                autoEvent.Dispose();
            }
        }