public void ReceiveRemove() { var person = new Person("remove"); list.Add(person); var test = new AsyncTest(); list.CollectionChanged += (sender, args) => { Assert.AreEqual(NotifyCollectionChangedAction.Remove, args.Action); Assert.IsNull(args.NewItems); Assert.IsNotNull(args.OldItems); CollectionAssert.Contains(args.OldItems, person); CollectionAssert.DoesNotContain(list, person); test.PassHandler(null, EventArgs.Empty); }; this.server.SendAsync(new BuddyListMessage { ChangeAction = NotifyCollectionChangedAction.Remove, People = new[] { person } }); test.Assert(10000); }
public void Multiple_Passes_FailIfNotPassed() { var test = new AsyncTest (multiple: true); test.PassHandler (this, EventArgs.Empty); // failIfNotPassed refers to explicit Assert.Pass() calls only, not PassHandler calls. Assert.Throws<AssertionException> (() => test.Assert (10, failIfNotPassed: true)); }
public void Multiple_Passes_NoFailures() { var test = new AsyncTest (multiple: true); test.PassHandler (this, EventArgs.Empty); test.PassHandler (this, EventArgs.Empty); test.Assert (10, failIfNotPassed: false); }
public void Multiple_Passes_Failure() { var test = new AsyncTest (multiple: true); test.PassHandler (this, EventArgs.Empty); test.PassHandler (this, EventArgs.Empty); test.FailHandler (this, EventArgs.Empty); Assert.Throws<AssertionException> (() => test.Assert (10, failIfNotPassed: false)); }
public void Connected() { var test = new AsyncTest(); client.Connected += test.PassHandler; client.Disconnected += test.FailHandler; client.ConnectAsync (new IPEndPoint (IPAddress.Any, 0)); test.Assert (10000); }
public void Connected() { var test = new AsyncTest (); client.Connected += test.PassHandler; client.Disconnected += test.FailHandler; client.ConnectAsync (new Target (Target.AnyIP, 0)); test.Assert (10000); }
public void ServerConnectionConnected() { var provider = new MockConnectionProvider (MockProtocol.Instance); provider.Start (MessageTypes.Reliable); var test = new AsyncTest<ConnectionMadeEventArgs> (e => Assert.AreEqual (true, e.Connection.IsConnected)); provider.ConnectionMade += test.PassHandler; var client = provider.GetClientConnection (MockProtocol.Instance); client.ConnectAsync (new Target (Target.AnyIP, 0), MessageTypes.Reliable); test.Assert (5000); }
public void ReceiveReset() { Add(); var test = new AsyncTest(); list.CollectionChanged += (sender, args) => { Assert.AreEqual(NotifyCollectionChangedAction.Reset, args.Action); Assert.IsNull(args.NewItems); Assert.IsNull(args.OldItems); test.PassHandler(null, EventArgs.Empty); }; this.server.SendAsync(new BuddyListMessage { ChangeAction = NotifyCollectionChangedAction.Reset }); test.Assert(10000); }
public void ReceiveAdd() { var test = new AsyncTest(); var p = new Person("id"); list.CollectionChanged += (sender, args) => { Assert.AreEqual(NotifyCollectionChangedAction.Add, args.Action); Assert.IsNotNull(args.NewItems); Assert.AreEqual(p, args.NewItems[0]); CollectionAssert.Contains(list, p); test.PassHandler(null, EventArgs.Empty); }; this.server.SendAsync(new BuddyListMessage { ChangeAction = NotifyCollectionChangedAction.Add, People = new[] { p } }); test.Assert(10000); Assert.AreEqual(p, list.FirstOrDefault()); }
public void MultiProtocolGenericMessageHandling() { var test = new AsyncTest (e => { var me = (MessageEventArgs<MockMessage2>)e; Assert.AreSame (connection, me.Connection); Assert.AreEqual ("hi", me.Message.Content); }); Action<MessageEventArgs<MockMessage2>> handler = e => test.PassHandler (test, e); client.RegisterMessageHandler (handler); client.RegisterMessageHandler (handler); client.ConnectAsync (new Target (Target.AnyIP, 0)); connection.Receive (new MessageEventArgs (connection, new MockMessage2 { Content = "hi" })); test.Assert (10000); }
public void IsConnected() { var test = new AsyncTest (passTimes: 2); bool first = true; client.PropertyChanged += (o, e) => { if (e.PropertyName != "IsConnected") return; if (first) { Assert.IsTrue (client.IsConnected); first = false; client.DisconnectAsync(); } else Assert.IsFalse (client.IsConnected); test.PassHandler (null, EventArgs.Empty); }; client.ConnectAsync (new Target (Target.AnyIP, 0)); 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; }
public void DisconnectFromHandlerThread() { var test = new AsyncTest(); Action<MessageEventArgs<MockMessage>> handler = e => { client.DisconnectAsync().ContinueWith (t => { test.PassHandler (null, EventArgs.Empty); }); }; client.RegisterMessageHandler (handler); client.ConnectAsync (new Target (Target.AnyIP, 0)); connection.Receive (new MessageEventArgs (connection, new MockMessage { Content = "hi" })); test.Assert (10000); }
public void DisconnectedFromConnection() { var test = new AsyncTest<ClientDisconnectedEventArgs> (e => { Assert.IsFalse (e.Requested); Assert.AreEqual (ConnectionResult.EncryptionMismatch, e.Reason); }); client.Connected += (s, e) => connection.Disconnect (ConnectionResult.EncryptionMismatch); client.Disconnected += test.PassHandler; client.ConnectAsync (new Target (Target.AnyIP, 0)); test.Assert (1000); }
public void DisconnectFromHandlerThread() { var test = new AsyncTest(); Action<MessageEventArgs<MockMessage>> handler = e => { client.Disconnect (true); test.PassHandler (null, EventArgs.Empty); }; client.RegisterMessageHandler (handler); client.ConnectAsync (new IPEndPoint (IPAddress.Any, 0)); connection.Receive (new MessageEventArgs (connection, new MockMessage { Content = "hi" })); test.Assert (10000); }
public void FailIfNotPassed() { var test = new AsyncTest(); Assert.Throws<AssertionException> (() => test.Assert (10, failIfNotPassed: true)); }
public void DisconnectWithReason() { var test = new AsyncTest<ClientDisconnectedEventArgs> (e => { Assert.AreEqual ("reason", e.CustomReason); Assert.AreEqual (ConnectionResult.Custom, e.Reason); Assert.IsTrue (e.Requested); }); client.Connected += (sender, e) => client.DisconnectWithReason ("reason"); client.Disconnected += test.PassHandler; client.ConnectAsync (new IPEndPoint (IPAddress.Any, 0)); test.Assert (10000); }
public void PingPong() { AsyncTest test = new AsyncTest(); IServerConnection connection = null; provider.ConnectionMade += (sender, e) => { if (connection == null) connection = e.Connection; }; provider.Start (MessageTypes); ((NetworkConnectionProvider)provider).PingFrequency = 1000; var client = GetNewClientConnection(); client.Disconnected += test.FailHandler; client.ConnectAsync (Target, MessageTypes); test.Assert (30000, false); Assert.IsNotNull (connection); Assert.IsTrue (connection.IsConnected); Assert.IsTrue (client.IsConnected); }
public void Disconnect() { var test = new AsyncTest<ClientDisconnectedEventArgs> (e => { Assert.AreEqual (ConnectionResult.Custom, e.Reason); Assert.IsTrue (e.Requested); }); client.Connected += (sender, e) => client.DisconnectAsync(); client.Disconnected += test.PassHandler; client.ConnectAsync (new Target (Target.AnyIP, 0)); test.Assert (10000); }
public void GenericMessageHandling() { var test = new AsyncTest (e => { var me = (MessageEventArgs<MockMessage>)e; Assert.AreSame (connection, me.Connection); Assert.AreEqual ("hi", me.Message.Content); }); Action<MessageEventArgs<MockMessage>> handler = e => test.PassHandler (test, e); client.RegisterMessageHandler (handler); client.ConnectAsync (new IPEndPoint (IPAddress.Any, 0)); connection.Receive (new MessageEventArgs (connection, new MockMessage { Content = "hi" })); test.Assert (10000); }
public void MessageHandling() { var test = new AsyncTest (e => { var me = (MessageEventArgs)e; Assert.AreSame (connection, me.Connection); Assert.IsInstanceOf (typeof(MockMessage), me.Message); Assert.AreEqual ("hi", ((MockMessage)me.Message).Content); }); Action<MessageEventArgs> handler = e => test.PassHandler (test, e); ((IContext)client).RegisterMessageHandler (MockProtocol.Instance, 1, handler); client.ConnectAsync (new IPEndPoint (IPAddress.Any, 0)); connection.Receive (new MessageEventArgs (connection, new MockMessage { Content = "hi" })); test.Assert (10000); }
public void MultiProtocolMessageHandling() { var test = new AsyncTest (e => { var me = (MessageEventArgs)e; Assert.AreSame (connection, me.Connection); Assert.IsTrue (me.Message is MockMessage2); Assert.AreEqual ("hi", ((MockMessage2)me.Message).Content); }); Action<MessageEventArgs> handler = e => test.PassHandler (test, e); ((IContext)client).RegisterMessageHandler (MockProtocol.Instance, 1, handler); ((IContext)client).RegisterMessageHandler (MockProtocol2.Instance, 1, handler); client.ConnectAsync (new Target (Target.AnyIP, 0)); connection.Receive (new MessageEventArgs (connection, new MockMessage2 { Content = "hi" })); test.Assert (10000); }
public void ReceiveAdd() { var test = new AsyncTest(); var p = new Person ("id"); list.CollectionChanged += (sender, args) => { Assert.AreEqual (NotifyCollectionChangedAction.Add, args.Action); Assert.IsNotNull (args.NewItems); Assert.AreEqual (p, args.NewItems[0]); CollectionAssert.Contains (list, p); test.PassHandler (null, EventArgs.Empty); }; this.server.SendAsync (new BuddyListMessage { ChangeAction = NotifyCollectionChangedAction.Add, People = new[] { p } }); test.Assert (10000); Assert.AreEqual (p, list.FirstOrDefault()); }
public void RejectSha1() { TearDown(); provider = new NetworkConnectionProvider (new [] { MockProtocol.Instance }, (IPEndPoint)EndPoint, MaxConnections, () => new RSACrypto(), new string[] { "SHA256" } ); provider.Start (MessageTypes); var test = new AsyncTest<DisconnectedEventArgs> (d => Assert.AreEqual (ConnectionResult.FailedHandshake, d.Result)); var client = new NetworkClientConnection (new[] { MockProtocol.Instance }, () => new MockSha1OnlyCrypto()); client.ConnectAsync (EndPoint, MessageTypes); client.Connected += test.FailHandler; client.Disconnected += test.PassHandler; test.Assert (10000); }
public void FailIfNotPassed_False() { var test = new AsyncTest(); test.Assert (10, failIfNotPassed: false); }
public void ConnectionLimitRestartListening() { IServerConnection connection = null; provider.ConnectionMade += (sender, e) => { if (connection == null) connection = e.Connection; }; ConnectionLimit(); Assert.IsNotNull (connection); connection.DisconnectAsync(); AsyncTest test = new AsyncTest(); provider.ConnectionMade += test.PassHandler; IClientConnection client = GetNewClientConnection(); client.Disconnected += test.FailHandler; client.ConnectAsync (Target, MessageTypes); test.Assert (10000); }
public void TryGetBufferBlocksAndExisting() { var buffer = new BufferPool (128, 1); SocketAsyncEventArgs e; buffer.TryGetBuffer (out e); DateTime now = DateTime.Now; SocketAsyncEventArgs second = null; var test = new AsyncTest (args => { Assert.That (DateTime.Now - now, Is.GreaterThan (TimeSpan.FromSeconds (1))); Assert.That (second, Is.SameAs (e)); }); Task.Run (() => { Assert.IsTrue (buffer.TryGetBuffer (out second)); }).ContinueWith (t => { test.PassHandler (null, EventArgs.Empty); }); Task.Delay (1000).ContinueWith (t => { buffer.PushBuffer (e); }); test.Assert (2000); }
public void ConnectionLimit() { provider.Start (MessageTypes); AutoResetEvent wait = new AutoResetEvent (false); int counter = 0; provider.ConnectionMade += (s, e) => { if (Interlocked.Increment (ref counter) == MaxConnections) wait.Set(); }; IClientConnection client; AsyncTest test; for (int i = 0; i < MaxConnections; ++i) { test = new AsyncTest(); client = GetNewClientConnection(); client.Connected += test.PassHandler; client.Disconnected += test.FailHandler; client.ConnectAsync (Target, MessageTypes); test.Assert (10000); } if (!wait.WaitOne (30000)) Assert.Fail ("MaxConnections was not reached in time"); test = new AsyncTest(); provider.ConnectionMade += test.FailHandler; client = GetNewClientConnection(); client.Disconnected += test.PassHandler; client.ConnectAsync (Target, MessageTypes); test.Assert (10000, false); }
public void GenericMessageHandling() { IServerConnection connection = null; var test = new AsyncTest (e => { var me = (MessageEventArgs<MockMessage>)e; Assert.AreSame (connection, me.Connection); Assert.AreEqual ("hi", me.Message.Content); }); var server = new MockServer (provider, MessageTypes.Reliable); server.Start(); Action<MessageEventArgs<MockMessage>> handler = e => test.PassHandler (test, e); server.RegisterMessageHandler (handler); provider.ConnectionMade += (sender, e) => connection = e.Connection; var c = provider.GetClientConnection (protocol); c.ConnectAsync (new IPEndPoint (IPAddress.Any, 0), MessageTypes.Reliable); c.Send (new MockMessage { Content = "hi" }); test.Assert (10000); }
public void MessageHandlingGlobalOrder() { IServerConnection connection = null; var test = new AsyncTest(e => { var me = (MessageEventArgs)e; Assert.AreSame (connection, me.Connection); Assert.IsInstanceOf (typeof(MockMessage), me.Message); Assert.AreEqual ("hi", ((MockMessage)me.Message).Content); }); var server = new MockServer (MessageTypes.Reliable); server.AddConnectionProvider (provider, ExecutionMode.GlobalOrder); server.Start(); Action<MessageEventArgs> handler = e => test.PassHandler (test, e); ((IContext)server).RegisterMessageHandler (MockProtocol.Instance, 1, handler); provider.ConnectionMade += (sender, e) => connection = e.Connection; var c = provider.GetClientConnection (protocol); c.ConnectAsync (new IPEndPoint (IPAddress.Any, 0), MessageTypes.Reliable); c.Send (new MockMessage { Content = "hi" }); test.Assert (10000); }
public void ConnectionMade() { var server = new MockServer (provider, MessageTypes.Reliable); server.Start(); var test = new AsyncTest(); server.ConnectionMade += test.PassHandler; var client = provider.GetClientConnection (protocol); client.ConnectAsync (new IPEndPoint (IPAddress.Any, 0), MessageTypes.Reliable); test.Assert (5000); }
public void ISerializable() { byte[] buffer = new byte[20480]; var writer = new BufferValueWriter (buffer); SerializableTester tester = new SerializableTester { Name = "MONKEY!", Numbers = new[] { 1, 2, 4, 8, 16, 32 } }; var test = new AsyncTest(); tester.SerializeCalled += test.PassHandler; writer.Write (context, tester); writer.Flush(); var reader = new BufferValueReader (buffer); var serialized = SerializerExtensions.Read<SerializableTester> (reader, context); Assert.IsNotNull (serialized); Assert.AreEqual (tester.Name, serialized.Name); Assert.IsTrue (tester.Numbers.SequenceEqual (serialized.Numbers), "Numbers does not match"); test.Assert (1000); }
public void ReceiveRemove() { var person = new Person ("remove"); list.Add (person); var test = new AsyncTest(); list.CollectionChanged += (sender, args) => { Assert.AreEqual (NotifyCollectionChangedAction.Remove, args.Action); Assert.IsNull (args.NewItems); Assert.IsNotNull (args.OldItems); CollectionAssert.Contains (args.OldItems, person); CollectionAssert.DoesNotContain (list, person); test.PassHandler (null, EventArgs.Empty); }; this.server.SendAsync(new BuddyListMessage { ChangeAction = NotifyCollectionChangedAction.Remove, People = new[] { person } }); test.Assert (10000); }