public void ConnectionLostTest() { AsyncPump.Run( async() => { var readResult = new TaskCompletionSource <int>(); using (var client = new S101Client( new MemoryStream(), (b, o, c, t) => readResult.Task, (b, o, c, t) => Task.FromResult(false), new S101Logger(GlowTypes.Instance, Console.Out))) { var exception = new IOException(); var connectionLost = new TaskCompletionSource <bool>(); client.ConnectionLost += (s, e) => { Assert.AreEqual(exception, e.Exception); connectionLost.SetResult(true); }; readResult.SetException(exception); await connectionLost.Task; await AssertThrowAsync <ObjectDisposedException>( () => client.SendMessageAsync(new S101Message(0x00, new KeepAliveRequest()))); } }); }
public void ExceptionTest() { using (var dummy = new MemoryStream()) { ReadAsyncCallback fakeRead = (b, o, c, t) => Task.FromResult(0); WriteAsyncCallback fakeWrite = (b, o, c, t) => Task.FromResult(false); AssertThrow <NotSupportedException>(() => new S101Client(dummy, fakeRead, fakeWrite).Dispose()); AsyncPump.Run( async() => { using (var connection = new CompleteOnDispose()) using (var client = new S101Client(connection, (b, o, c, t) => connection.Task, fakeWrite)) { await AssertThrowAsync <InvalidOperationException>( () => Task.Run(() => client.SendMessageAsync(new S101Message(0x00, new KeepAliveRequest())))); } AssertThrow <ArgumentNullException>( () => new S101Client(null, fakeRead, fakeWrite).Dispose(), () => new S101Client(dummy, null, fakeWrite).Dispose(), () => new S101Client(dummy, fakeRead, null).Dispose()); AssertThrow <ArgumentOutOfRangeException>( () => new S101Client(dummy, fakeRead, fakeWrite, null, 3000, 0).Dispose(), () => new S101Client(dummy, fakeRead, fakeWrite, null, -2, 1).Dispose()); using (var connection = new CompleteOnDispose()) using (var client = new S101Client( connection, (b, o, c, t) => connection.Task, fakeWrite, null, 3000, 1)) { await AssertThrowAsync <ArgumentNullException>( () => client.SendMessageAsync(null)); await AssertThrowAsync <ArgumentException>(() => client.SendMessageAsync(EmberDataMessage)); await AssertThrowAsync <ArgumentException>(() => client.SendOutOfFrameByteAsync(0xFE)); client.Dispose(); await AssertThrowAsync <ObjectDisposedException>( () => client.SendMessageAsync(new S101Message(0x00, new KeepAliveRequest()))); } }); } }