public void AsyncManualResetEvent_Error() { AsyncManualResetEvent manualEvent; // Verify that we get and [ObjectDisposedException] for [Set()] and [Reset()] // a disposed event. manualEvent = new AsyncManualResetEvent(); manualEvent.Dispose(); ExtendedAssert.Throws <ObjectDisposedException>(() => manualEvent.Set()); ExtendedAssert.Throws <ObjectDisposedException>(() => manualEvent.Reset()); Task.Run(() => ExtendedAssert.ThrowsAsync <ObjectDisposedException>(async() => await manualEvent.WaitAsync())).Wait(); // Verify that disposing an event causes any waiting tasks // to unblock with an [ObjectDisposedException]. manualEvent = new AsyncManualResetEvent(); 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 manualEvent.WaitAsync(); } catch (ObjectDisposedException) { taskInfo[taskIndex].IsFaulted = true; } catch { badException = true; taskInfo[taskIndex].IsFaulted = true; } taskInfo[taskIndex].IsComplete = true; }, i).Start(); } Helper.WaitFor(() => taskInfo.AllRunning, defaultTimeout); Assert.IsFalse(taskInfo.AnyComplete); manualEvent.Dispose(); Helper.WaitFor(() => taskInfo.AllFaulted, defaultTimeout); Assert.IsFalse(badException); }
public async Task SharedMemIPC_Timeout() { // Verify that the client implements timeout. SharedMemServer <MessageFactory> server = null; SharedMemClient <MessageFactory> client = null; try { // Configure a test server that can delay for 5 seconds before returning. server = new SharedMemServer <MessageFactory>("test-server", rawRequest => { var request = (RequestMessage)rawRequest; if (request.Arg == "DELAY") { Thread.Sleep(TimeSpan.FromSeconds(5)); } return(new ResponseMessage() { Result = request.Arg }); }); // Configure the client. client = new SharedMemClient <MessageFactory>("test-server"); // Verify that we get a timeout. await ExtendedAssert.ThrowsAsync <TimeoutException>( async() => await client.CallAsync <ResponseMessage>(new RequestMessage() { Arg = "DELAY" }, TimeSpan.FromSeconds(1))); // Verify that regular requests still work. var response = await client.CallAsync <ResponseMessage>(new RequestMessage() { Arg = "NODELAY" }, TimeSpan.FromSeconds(1)); Assert.AreEqual("NODELAY", response.Result); } finally { if (server != null) { server.Dispose(); } if (client != null) { client.Dispose(); } } }