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); }
/// <inheritdoc/> public void Dispose() { if (eventQueue != null) { eventQueue = null; } if (eventReady != null) { eventReady.Dispose(); eventReady = null; } }
public void DisposeWait() { var Event = new AsyncAutoResetEvent(); Event.Dispose(); try { _ = Event.Wait(); Assert.Fail("Did not throw"); } catch (ObjectDisposedException) { } }
public void ReleaseObject() { "在调用释放之后,所有的等待将会被释放,同时释放的值是 false 值".Test(() => { var asyncAutoResetEvent = new AsyncAutoResetEvent(false); var manualResetEvent = new ManualResetEvent(false); var task = Task.Run(async() => { // ReSharper disable once AccessToDisposedClosure var t = asyncAutoResetEvent.WaitOneAsync(); manualResetEvent.Set(); return(await t); }); // 解决单元测试里面 Task.Run 启动太慢 manualResetEvent.WaitOne(); asyncAutoResetEvent.Dispose(); task.Wait(); var taskResult = task.Result; Assert.AreEqual(false, taskResult); }); }
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(); } }