public void TestExceptionDisconnect() { lock (syncRoot) { var app = MakeEnv(); var manager = app.Make <INetworkManager>(); var fromClient = new byte[] { }; using (var server = new TcpServer((data) => { fromClient = Arr.Merge(fromClient, data); })) { var channel = manager.Make("frame.tcp://127.0.0.1:7777"); Exception err = null; manager.OnClosed += (c, e) => { err = e; }; try { var wait = channel.Connect(); var count = 0; while (!wait.IsDone && count++ < 3000) { Thread.Sleep(1); } channel.Disconnect(new RuntimeException("test exception")); Assert.AreEqual("CatLib.RuntimeException: test exception", err.ToString()); } finally { channel.Disconnect(); } } } }
public void TestRelease() { lock (syncRoot) { var app = MakeEnv(); var manager = app.Make <INetworkManager>(); var fromClient = new byte[] { }; using (var server = new TcpServer((data) => { fromClient = Arr.Merge(fromClient, data); })) { var channel = manager.Make("frame.tcp://127.0.0.1:7777", "test"); channel.Connect(); var disconnect = false; channel.OnDisconnect += (c) => { disconnect = true; }; var closed = false; channel.OnClosed += (c, e) => { closed = true; }; manager.Release("test"); Assert.AreEqual(false, channel.Socket.Connected); Assert.AreEqual(true, disconnect); Assert.AreEqual(true, closed); var socket = app.Make <ISocketManager>().Make("tcp://127.0.0.1:7777", "test"); Assert.AreNotEqual(socket, channel.Socket); } } }
public void TestChannelPackerErrorEncode() { var fromClient = new byte[] { }; using (var server = new TcpServer((data) => { fromClient = Arr.Merge(fromClient, data); })) { var socket = MakeEnv().Make <ISocketManager>().Make("tcp://127.0.0.1", "TestChannelPackerErrorEncode"); var channel = new NetworkChannel(socket, new PackerError(PackerError.ErrorType.Encode), "TestChannelPackerErrorEncode"); var wait = channel.Connect("127.0.0.1", 7777); var count = 0; while (!wait.IsDone && count++ < 3000) { Thread.Sleep(1); } try { var isThrow = false; try { channel.Send(Encoding.Default.GetBytes("helloworld")); } catch (RuntimeException) { isThrow = true; } Assert.AreEqual(true, isThrow); } finally { channel.Disconnect(); } } }
public void TestAllEventRegister() { lock (syncRoot) { var app = MakeEnv(); var manager = app.Make <INetworkManager>(); var fromClient = new byte[] { }; using (var server = new TcpServer((data) => { fromClient = Arr.Merge(fromClient, data); })) { var channel = manager.Make("frame.tcp://127.0.0.1:7777"); Exception err = null; manager.OnError += (c, e) => { err = e; }; Exception closeErr = new Exception(); manager.OnClosed += (c, e) => { closeErr = e; }; INetworkChannel connected = null; manager.OnConnected += (c) => { connected = c; }; INetworkChannel disconnected = null; manager.OnDisconnect += (c) => { disconnected = c; }; INetworkChannel sent = null; manager.OnSent += (c, b) => { sent = c; }; INetworkChannel released = null; manager.OnReleased += (c) => { released = c; }; try { var wait = channel.Connect(); var count = 0; while (!wait.IsDone && count++ < 3000) { Thread.Sleep(1); } channel.Send(Encoding.Default.GetBytes("helloworld")); } finally { manager.Release(); } Assert.AreEqual(null, err); Assert.AreEqual(null, closeErr); Assert.AreEqual(channel, connected); Assert.AreEqual(channel, disconnected); Assert.AreEqual(channel, sent); Assert.AreEqual(channel, released); } } }
public void HeatBeatDisconnectTests() { lock (syncRoot) { var app = MakeEnv(); var manager = app.Make <INetworkManager>(); var fromClient = new byte[] { }; using (var server = new TcpServer((data) => { fromClient = Arr.Merge(fromClient, data); })) { var channel = manager.Make("frame.tcp://127.0.0.1:7777"); channel.SetHeartBeat(500); channel.Connect(); var disconnect = false; channel.OnDisconnect += (c) => { disconnect = true; }; var closed = false; channel.OnClosed += (c, e) => { closed = true; }; try { var missCount = 0; channel.OnMissHeartBeat += (c, num) => { missCount++; channel.Disconnect(); }; while (missCount < 1) { (manager as NetworkManager).Tick(1); Thread.Sleep(1); } while (!disconnect) { (manager as NetworkManager).Tick(1); Thread.Sleep(1); } while (!closed) { (manager as NetworkManager).Tick(1); Thread.Sleep(1); } Assert.AreEqual(1, missCount); Assert.AreEqual(true, disconnect); Assert.AreEqual(true, closed); } finally { channel.Disconnect(); } } } }