public void OperationState_Can_Concurrently_Get_Timeout_And_Response() { var counter = 0; var timedOutReceived = 0; var clientCallbackCounter = 0; const int times = 40; var expectedTimedout = 0; TestHelper.Invoke(() => { Action <Exception, Response> clientCallback = (ex, r) => { Interlocked.Increment(ref clientCallbackCounter); }; var state = new OperationState(clientCallback); var actions = new Action[] { () => { var timedout = state.MarkAsTimedOut( new OperationTimedOutException(new IPEndPoint(0, 1), 200), () => Interlocked.Increment(ref timedOutReceived)); Interlocked.Add(ref expectedTimedout, timedout ? 1 : 0); }, () => { state.InvokeCallback(null); } }; if ((counter++) % 2 == 0) { //invert order actions = actions.Reverse().ToArray(); } TestHelper.ParallelInvoke(actions); }, times); //Allow callbacks to be called using the default scheduler Thread.Sleep(1000); Assert.AreEqual(times, clientCallbackCounter); Assert.AreEqual(expectedTimedout, timedOutReceived); }