public void Run(string[] args) { Stopwatch sw = null; parseArgs(args); banner(); Options opts = ConnectionFactory.GetDefaultOptions(); opts.Url = url; using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { sw = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { c.Request(subject, payload); } c.Flush(); sw.Stop(); System.Console.Write("Completed {0} requests in {1} seconds ", count, sw.Elapsed.TotalSeconds); System.Console.WriteLine("({0} requests/second).", (int)(count / sw.Elapsed.TotalSeconds)); printStats(c); } }
public void TestClosedConnections() { IConnection c = new ConnectionFactory().CreateConnection(); ISyncSubscription s = c.SubscribeSync("foo"); c.Close(); // While we can annotate all the exceptions in the test framework, // just do it manually. UnitTestUtilities.testExpectedException( () => { c.Publish("foo", null); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { c.Publish(new Msg("foo")); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { c.SubscribeAsync("foo"); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { c.SubscribeSync("foo"); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { c.SubscribeAsync("foo", "bar"); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { c.SubscribeSync("foo", "bar"); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { c.Request("foo", null); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { s.NextMessage(); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { s.NextMessage(100); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { s.Unsubscribe(); }, typeof(NATSConnectionClosedException)); UnitTestUtilities.testExpectedException( () => { s.AutoUnsubscribe(1); }, typeof(NATSConnectionClosedException)); }
public void TestRequestTimeout() { using (IConnection c = new ConnectionFactory().CreateConnection()) { try { c.Request("foo", null, 500); Assert.Fail("Expected an exception."); } catch (NATSTimeoutException) { Console.WriteLine("Received expected exception."); } } }
public void TestRequestNoBody() { using (IConnection c = new ConnectionFactory().CreateConnection()) { using (IAsyncSubscription s = c.SubscribeAsync("foo")) { byte[] response = Encoding.UTF8.GetBytes("I will help you."); s.MessageHandler += (sender, args) => { c.Publish(args.Message.Reply, response); }; s.Start(); Msg m = c.Request("foo", null, 50000); if (!compare(m.Data, response)) { Assert.Fail("Response isn't valid"); } } } }
public void TestEncodedObjectRequestReply() { using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection()) { Object mu = new Object(); SerializationTestObj origObj = new SerializationTestObj(); EventHandler<EncodedMessageEventArgs> eh = (sender, args) => { SerializationTestObj so = (SerializationTestObj)args.ReceivedObject; Assert.IsTrue(so.Equals(origObj)); String str = "Received"; c.Publish(args.Reply, str); c.Flush(); lock (mu) { Monitor.Pulse(mu); } }; using (IAsyncSubscription s = c.SubscribeAsync("foo", eh)) { Assert.IsTrue("Received".Equals(c.Request("foo", origObj, 1000))); Assert.IsTrue("Received".Equals(c.Request("foo", origObj))); } } }