public async Task TestConsumerReceiveNoWaitThrowsIfConnectionLost() { ManualResetEvent disconnected = new ManualResetEvent(false); using (TestAmqpPeer testPeer = new TestAmqpPeer()) { NmsConnection connection = (NmsConnection) await EstablishConnectionAsync(testPeer); Mock <INmsConnectionListener> connectionListener = new Mock <INmsConnectionListener>(); connectionListener .Setup(listener => listener.OnConnectionFailure(It.IsAny <NMSException>())) .Callback(() => { disconnected.Set(); }); connection.AddConnectionListener(connectionListener.Object); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue queue = await session.GetQueueAsync("queue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlow(); testPeer.RemotelyCloseConnection(expectCloseResponse: true, errorCondition: ConnectionError.CONNECTION_FORCED, errorMessage: "buba"); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); Assert.True(disconnected.WaitOne(), "Connection should be disconnected"); try { consumer.ReceiveNoWait(); Assert.Fail("An exception should have been thrown"); } catch (NMSException) { // Expected } } }