public void TestClosedConnections() { using (new NATSServer()) { IConnection c = utils.DefaultTestConnection; ISyncSubscription s = c.SubscribeSync("foo"); c.Close(); // While we can annotate all the exceptions in the test framework, // just do it manually. Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Publish("foo", null)); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Publish(new Msg("foo"))); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeAsync("foo")); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeSync("foo")); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeAsync("foo", "bar")); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeSync("foo", "bar")); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Request("foo", null)); Assert.ThrowsAny <NATSConnectionClosedException>(() => s.NextMessage()); Assert.ThrowsAny <NATSConnectionClosedException>(() => s.NextMessage(100)); Assert.ThrowsAny <NATSConnectionClosedException>(() => s.Unsubscribe()); Assert.ThrowsAny <NATSConnectionClosedException>(() => s.AutoUnsubscribe(1)); } }
private static void RequestResponseExplicit() { Console.Clear(); Console.WriteLine("Request/Response (explicit) demo"); Console.WriteLine("================================"); for (int i = 1; i <= _messageCount; i++) { string replySubject = $"_INBOX.{Guid.NewGuid().ToString("N")}"; ISyncSubscription subscription = _connection.SubscribeSync(replySubject); subscription.AutoUnsubscribe(1); // client also has a convenience-method to do this in line: //string replySubject = conn.NewInbox(); string message = $"Message {i}"; Console.WriteLine($"Sending: {message}"); // send with reply subject byte[] data = Encoding.UTF8.GetBytes(message); _connection.Publish("nats.demo.requestresponse", replySubject, data); // wait for response in reply subject var response = subscription.NextMessage(5000); string responseMsg = Encoding.UTF8.GetString(response.Data); Console.WriteLine($"Response: {responseMsg}"); Thread.Sleep(_sendIntervalMs); } }
public void TestClosedConnections() { using (NATSServer.CreateFastAndVerify(Context.Server1.Port)) { IConnection c = Context.OpenConnection(Context.Server1.Port); ISyncSubscription s = c.SubscribeSync("foo"); c.Close(); // While we can annotate all the exceptions in the test framework, // just do it manually. Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Publish("foo", null)); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Publish(new Msg("foo"))); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeAsync("foo")); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeSync("foo")); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeAsync("foo", "bar")); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.SubscribeSync("foo", "bar")); Assert.ThrowsAny <NATSConnectionClosedException>(() => c.Request("foo", null)); Assert.ThrowsAny <NATSConnectionClosedException>(() => s.NextMessage()); Assert.ThrowsAny <NATSConnectionClosedException>(() => s.NextMessage(100)); Assert.ThrowsAny <NATSConnectionClosedException>(() => s.Unsubscribe()); Assert.ThrowsAny <NATSConnectionClosedException>(() => s.AutoUnsubscribe(1)); } }
public void TestRespondWithAutoUnsubscribe() { using (new NATSServer()) { using (IConnection c = utils.DefaultTestConnection) using (ISyncSubscription s = c.SubscribeSync("foo")) { s.AutoUnsubscribe(1); string replyTo = c.NewInbox(); using (ISyncSubscription r = c.SubscribeSync(replyTo)) { c.Publish("foo", replyTo, Encoding.UTF8.GetBytes("message")); Msg m = s.NextMessage(1000); Assert.NotNull(m); Assert.Equal(replyTo, m.Reply); byte[] reply = Encoding.UTF8.GetBytes("reply"); m.Respond(reply); m = r.NextMessage(1000); Assert.NotNull(m); Assert.Equal(replyTo, m.Subject); Assert.Equal(reply, m.Data); r.Unsubscribe(); } } } }
public void TestRespondWithAutoUnsubscribe() { using (NATSServer.CreateFastAndVerify(Context.Server1.Port)) { using (IConnection c = Context.OpenConnection(Context.Server1.Port)) using (ISyncSubscription s = c.SubscribeSync("foo")) { s.AutoUnsubscribe(1); string replyTo = c.NewInbox(); using (ISyncSubscription r = c.SubscribeSync(replyTo)) { c.Publish("foo", replyTo, Encoding.UTF8.GetBytes("message")); Msg m = s.NextMessage(1000); Assert.NotNull(m); Assert.Equal(replyTo, m.Reply); byte[] reply = Encoding.UTF8.GetBytes("reply"); m.Respond(reply); m = r.NextMessage(1000); Assert.NotNull(m); Assert.Equal(replyTo, m.Subject); Assert.Equal(reply, m.Data); r.Unsubscribe(); } } } }
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 (NATSMaxMessagesException) { /* ignore */ } Assert.IsTrue(received == max); Assert.IsFalse(s.IsValid); } } }
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 (NATSServer.CreateFastAndVerify(Context.Server1.Port)) { using (IConnection c = Context.OpenConnection(Context.Server1.Port)) { 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 (NATSMaxMessagesException) { /* ignore */ } Assert.True(received == max); Assert.False(s.IsValid); } } } }