public void DisconnectWhileReceiving() { var test = new AsyncTest <ClientDisconnectedEventArgs> (e => { Assert.AreEqual(ConnectionResult.Custom, e.Reason); Assert.IsTrue(e.Requested); }); bool send = true; client.Connected += (sender, e) => { new Thread(() => { MockMessage m = new MockMessage { Content = "asdf" }; for (int i = 0; i < 10000 && send; ++i) { connection.Receive(new MessageEventArgs(connection, m)); } }).Start(); Thread.Sleep(50); client.DisconnectAsync(); }; client.Disconnected += test.PassHandler; client.ConnectAsync(new Target(Target.AnyIP, 0)); test.Assert(10000); send = false; }
public void SendAndRespond() { var cmessage = new MockMessage(); cmessage.Content = "Blah"; var c = GetNewClientConnection(); var test = new AsyncTest <MessageEventArgs <MockMessage> > (e => { Assert.IsNotNull(e.Message); Assert.AreEqual("Response", e.Message.Content); }); IConnection sc = null; ManualResetEvent wait = new ManualResetEvent(false); this.provider.ConnectionMade += (s, e) => { sc = e.Connection; sc.MessageReceived += (ms, me) => { var msg = (me.Message as MockMessage); me.Connection.SendResponseAsync(msg, new MockMessage { Content = "Response" }); }; sc.Disconnected += test.FailHandler; wait.Set(); }; this.provider.Start(MessageTypes); c.Disconnected += test.FailHandler; c.Connected += (sender, e) => { c.SendFor <MockMessage> (cmessage) .ContinueWith(t => test.PassHandler(sc, new MessageEventArgs <MockMessage> (sc, t.Result))); }; c.ConnectAsync(Target, MessageTypes); if (!wait.WaitOne(10000)) { Assert.Fail("Failed to connect"); } test.Assert(10000); }
public void DisconnectWhileReceiving() { var test = new AsyncTest<ClientDisconnectedEventArgs> (e => { Assert.AreEqual (ConnectionResult.Custom, e.Reason); Assert.IsTrue (e.Requested); }); bool send = true; client.Connected += (sender, e) => { new Thread (() => { MockMessage m = new MockMessage { Content = "asdf" }; for (int i = 0; i < 10000 && send; ++i) connection.Receive (new MessageEventArgs (connection, m)); }).Start(); Thread.Sleep (50); client.DisconnectAsync(); }; client.Disconnected += test.PassHandler; client.ConnectAsync (new Target (Target.AnyIP, 0)); test.Assert (10000); send = false; }