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 TestClientAutoUnsub() { using (IConnection c = new ConnectionFactory().CreateConnection()) { long received = 0; int max = 10; using (ISyncSubscription s = c.SubscribeSync("foo")) { s.AutoUnsubscribe(max); for (int i = 0; i < max * 2; i++) { c.Publish("foo", null); } c.Flush(); Thread.Sleep(100); try { while (true) { s.NextMessage(0); received++; } } catch (NATSBadSubscriptionException) { /* ignore */ } Assert.IsTrue(received == max); Assert.IsFalse(s.IsValid); } } }
// TODO [TestMethod] public void TestSlowSubscriber() { Options opts = ConnectionFactory.GetDefaultOptions(); opts.SubChannelLength = 10; using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { using (ISyncSubscription s = c.SubscribeSync("foo")) { for (int i =0; i < (opts.SubChannelLength+100); i++) { c.Publish("foo", null); } try { c.Flush(); } catch (Exception ex) { System.Console.WriteLine(ex); if (ex.InnerException != null) System.Console.WriteLine(ex.InnerException); throw ex; } try { s.NextMessage(); } catch (NATSSlowConsumerException) { return; } Assert.Fail("Did not receive an exception."); } } }
public void TestValidSubscriber() { using (IConnection c = new ConnectionFactory().CreateConnection()) { using (ISyncSubscription s = c.SubscribeSync("foo")) { Assert.IsTrue(s.IsValid); try { s.NextMessage(100); } catch (NATSTimeoutException) { } Assert.IsTrue(s.IsValid); s.Unsubscribe(); Assert.IsFalse(s.IsValid); try { s.NextMessage(100); } catch (NATSBadSubscriptionException) { } } } }
public void TestCloseSubRelease() { using (IConnection c = new ConnectionFactory().CreateConnection()) { using (ISyncSubscription s = c.SubscribeSync("foo")) { Stopwatch sw = new Stopwatch(); sw.Start(); try { new Task(() => { Thread.Sleep(100); c.Close(); }).Start(); s.NextMessage(10000); } catch (Exception) { /* ignore */ } sw.Stop(); Assert.IsTrue(sw.ElapsedMilliseconds < 10000); } } }
public void TestSyncSubscribe() { using (IConnection c = new ConnectionFactory().CreateConnection()) { using (ISyncSubscription s = c.SubscribeSync("foo")) { c.Publish("foo", omsg); Msg m = s.NextMessage(1000); if (compare(omsg, m) == false) Assert.Fail("Messages are not equal."); } } }
public void TestSyncReplyArg() { using (IConnection c = new ConnectionFactory().CreateConnection()) { using (ISyncSubscription s = c.SubscribeSync("foo")) { c.Publish("foo", "bar", null); c.Flush(30000); Msg m = s.NextMessage(1000); if ("bar".Equals(m.Reply) == false) Assert.Fail("Expected \"bar\", received: " + m); } } }
public void TestStats() { using (IConnection c = new ConnectionFactory().CreateConnection()) { byte[] data = Encoding.UTF8.GetBytes("The quick brown fox jumped over the lazy dog"); int iter = 10; for (int i = 0; i < iter; i++) { c.Publish("foo", data); } c.Flush(1000); IStatistics stats = c.Stats; Assert.AreEqual(iter, stats.OutMsgs); Assert.AreEqual(iter * data.Length, stats.OutBytes); c.ResetStats(); // Test both sync and async versions of subscribe. IAsyncSubscription s1 = c.SubscribeAsync("foo"); s1.MessageHandler += (sender, arg) => { }; s1.Start(); ISyncSubscription s2 = c.SubscribeSync("foo"); for (int i = 0; i < iter; i++) { c.Publish("foo", data); } c.Flush(1000); stats = c.Stats; Assert.AreEqual(2 * iter, stats.InMsgs); Assert.AreEqual(2 * iter * data.Length, stats.InBytes); } }
public void TestQueueSubscriber() { using (IConnection c = new ConnectionFactory().CreateConnection()) { using (ISyncSubscription s1 = c.SubscribeSync("foo", "bar"), s2 = c.SubscribeSync("foo", "bar")) { c.Publish("foo", omsg); c.Flush(1000); if (s1.QueuedMessageCount + s2.QueuedMessageCount != 1) Assert.Fail("Invalid message count in queue."); // Drain the messages. try { s1.NextMessage(100); } catch (NATSTimeoutException) { } try { s2.NextMessage(100); } catch (NATSTimeoutException) { } int total = 1000; for (int i = 0; i < 1000; i++) { c.Publish("foo", omsg); } c.Flush(1000); Thread.Sleep(1000); int r1 = s1.QueuedMessageCount; int r2 = s2.QueuedMessageCount; if ((r1 + r2) != total) { Assert.Fail("Incorrect number of messages: {0} vs {1}", (r1 + r2), total); } if (Math.Abs(r1 - r2) > (total * .15)) { Assert.Fail("Too much variance between {0} and {1}", r1, r2); } } } }
public void TestFlush() { using (IConnection c = new ConnectionFactory().CreateConnection()) { using (ISyncSubscription s = c.SubscribeSync("foo")) { c.Publish("foo", "reply", omsg); c.Flush(); } } }
public void TestDoubleUnsubscribe() { using (IConnection c = new ConnectionFactory().CreateConnection()) { using (ISyncSubscription s = c.SubscribeSync("foo")) { s.Unsubscribe(); try { s.Unsubscribe(); Assert.Fail("No Exception thrown."); } catch (Exception e) { System.Console.WriteLine("Expected exception {0}: {1}", e.GetType(), e.Message); } } } }
public void TestSubDelTaskCountWithSyncSub() { var opts = utils.DefaultTestOptions; opts.SubscriberDeliveryTaskCount = 1; using (new NATSServer()) { using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { ISyncSubscription s = c.SubscribeSync("foo"); c.Publish("foo", null); s.NextMessage(10000); } } }
public void TestSlowSubscriber() { Options opts = utils.DefaultTestOptions; opts.SubChannelLength = 10; using (new NATSServer()) { using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { using (ISyncSubscription s = c.SubscribeSync("foo")) { Assert.ThrowsAny<Exception>(() => { for (int i = 0; i < (opts.SubChannelLength + 100); i++) { c.Publish("foo", null); } try { c.Flush(); } catch (Exception) { // ignore } s.NextMessage(); }); } } } }
public void TestTlsSuccessSecureConnect() { using (NATSServer srv = util.CreateServerWithConfig(TestContext, "tls_1222.conf")) { // we can't call create secure connection w/ the certs setup as they are // so we'll override the Options opts = ConnectionFactory.GetDefaultOptions(); opts.Secure = true; opts.TLSRemoteCertificationValidationCallback = verifyServerCert; opts.Url = "nats://localhost:1222"; using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { using (ISyncSubscription s = c.SubscribeSync("foo")) { c.Publish("foo", null); c.Flush(); Msg m = s.NextMessage(); System.Console.WriteLine("Received msg over TLS conn."); } } } }
public void TestTlsSuccessWithCert() { using (NATSServer srv = util.CreateServerWithConfig(TestContext, "tls_1222_verify.conf")) { Options opts = ConnectionFactory.GetDefaultOptions(); opts.Secure = true; opts.Url = "nats://localhost:1222"; opts.TLSRemoteCertificationValidationCallback = verifyServerCert; // .NET requires the private key and cert in the // same file. 'client.pfx' is generated from: // // openssl pkcs12 -export -out client.pfx // -inkey client-key.pem -in client-cert.pem X509Certificate2 cert = new X509Certificate2( UnitTestUtilities.GetFullCertificatePath( TestContext, "client.pfx"), "password"); opts.AddCertificate(cert); using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { using (ISyncSubscription s = c.SubscribeSync("foo")) { c.Publish("foo", null); c.Flush(); Msg m = s.NextMessage(); System.Console.WriteLine("Received msg over TLS conn."); } } } }
public void TestTlsReconnect() { AutoResetEvent ev = new AutoResetEvent(false); using (NATSServer srv = util.CreateServerWithConfig("tls_1222.conf"), srv2 = util.CreateServerWithConfig("tls_1224.conf")) { Thread.Sleep(1000); Options opts = util.DefaultTestOptions; opts.Secure = true; opts.NoRandomize = true; opts.TLSRemoteCertificationValidationCallback = verifyServerCert; opts.Servers = new string[]{ "nats://localhost:1222" , "nats://localhost:1224" }; opts.ReconnectedEventHandler += (sender, obj) => { ev.Set(); }; using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { // send a message to be sure. using (ISyncSubscription s = c.SubscribeSync("foo")) { c.Publish("foo", null); c.Flush(); s.NextMessage(); // shutdown the server srv.Shutdown(); // wait for reconnect Assert.True(ev.WaitOne(30000)); c.Publish("foo", null); c.Flush(); s.NextMessage(); } } } }