public void TestSubDelTaskCountReconnect() { bool disconnected = false; AutoResetEvent reconnectEv = new AutoResetEvent(false); var opts = utils.DefaultTestOptions; opts.SubscriberDeliveryTaskCount = 2; opts.DisconnectedEventHandler = (obj, args) => { disconnected = true; }; opts.ReconnectedEventHandler = (obj, args) => { reconnectEv.Set(); }; using (var server = new NATSServer()) { using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { long received = 0; int max = 10; AutoResetEvent ev = new AutoResetEvent(false); using (var s = c.SubscribeAsync("foo", (obj, args) => { received++; if (received == max) { ev.Set(); } })) { for (int i = 0; i < max / 2; i++) { c.Publish("foo", null); } c.Flush(); // bounce the server, we should reconnect, then // be able to receive messages. server.Bounce(100); Assert.True(reconnectEv.WaitOne(20000)); Assert.True(disconnected); for (int i = 0; i < max / 2; i++) { c.Publish("foo", null); } c.Flush(); Assert.True(ev.WaitOne(10000)); Assert.True(received == max); } } } }
public void TestInfineReconnect() { var reconnectEv = new AutoResetEvent(false); var closedEv = new AutoResetEvent(false); var opts = ConnectionFactory.GetDefaultOptions(); opts.Timeout = 500; opts.ReconnectWait = 10; opts.ReconnectedEventHandler = (obj, args) => { reconnectEv.Set(); }; opts.ClosedEventHandler = (obj, args) => { closedEv.Set(); }; using (var s = new NATSServer()) { // first test a reconnect failure... opts.MaxReconnect = 1; using (var c = new ConnectionFactory().CreateConnection(opts)) { // we should just close - our one reconnect attempt failed. s.Bounce(opts.Timeout * (opts.MaxReconnect + 1) + 500); // we are closed, and not reconnected. Assert.True(closedEv.WaitOne(10000)); Assert.False(reconnectEv.WaitOne(100)); } } closedEv.Reset(); reconnectEv.Reset(); using (var s = new NATSServer()) { // reconnect forever... opts.MaxReconnect = Options.ReconnectForever; using (var c = new ConnectionFactory().CreateConnection(opts)) { // with a timeout of 10ms, and a reconnectWait of 10 ms, we should have many // reconnect attempts. s.Bounce(20000); // Assert that we reconnected and are not closed. Assert.True(reconnectEv.WaitOne(10000)); Assert.False(closedEv.WaitOne(100)); } } }
public void TestPublishErrorsDuringReconnect() { AutoResetEvent connectedEv = new AutoResetEvent(false); using (var server = new NATSServer()) { Task t = Task.Factory.StartNew(() => { connectedEv.WaitOne(10000); Random r = new Random(); // increase this count for a longer running test. for (int i = 0; i < 10; i++) { server.Bounce(r.Next(500)); } }, CancellationToken.None, TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); byte[] payload = Encoding.UTF8.GetBytes("hello"); using (var c = utils.DefaultTestConnection) { connectedEv.Set(); while (t.IsCompleted == false) { try { c.Publish("foo", payload); } catch (Exception e) { Assert.IsNotType <NATSConnectionClosedException>(e); Assert.False(c.IsClosed()); } } } } }
public void TestServerStopDisconnectedHandler() { using (var s = new NATSServer()) { AutoResetEvent ev = new AutoResetEvent(false); Options o = utils.DefaultTestOptions; o.AllowReconnect = false; o.DisconnectedEventHandler += (sender, args) => { ev.Set(); }; IConnection c = new ConnectionFactory().CreateConnection(o); s.Bounce(1000); Assert.True(ev.WaitOne(10000)); c.Close(); } }
//[Fact] // This test works locally, but fails in AppVeyor some of the time // TODO: Work to identify why this happens... private void TestCallbacksOrder() { bool firstDisconnect = true; long orig = DateTime.Now.Ticks; long dtime1 = orig; long dtime2 = orig; long rtime = orig; long atime1 = orig; long atime2 = orig; long ctime = orig; AutoResetEvent reconnected = new AutoResetEvent(false); AutoResetEvent closed = new AutoResetEvent(false); AutoResetEvent asyncErr1 = new AutoResetEvent(false); AutoResetEvent asyncErr2 = new AutoResetEvent(false); AutoResetEvent recvCh = new AutoResetEvent(false); AutoResetEvent recvCh1 = new AutoResetEvent(false); AutoResetEvent recvCh2 = new AutoResetEvent(false); using (NATSServer s = utils.CreateServerWithConfig("auth_1222.conf"), def = new NATSServer()) { Options o = utils.DefaultTestOptions; o.DisconnectedEventHandler += (sender, args) => { Thread.Sleep(100); if (firstDisconnect) { firstDisconnect = false; dtime1 = DateTime.Now.Ticks; } else { dtime2 = DateTime.Now.Ticks; } }; o.ReconnectedEventHandler += (sender, args) => { Thread.Sleep(100); rtime = DateTime.Now.Ticks; reconnected.Set(); }; o.AsyncErrorEventHandler += (sender, args) => { Thread.Sleep(100); if (args.Subscription.Subject.Equals("foo")) { atime1 = DateTime.Now.Ticks; asyncErr1.Set(); } else { atime2 = DateTime.Now.Ticks; asyncErr2.Set(); } }; o.ClosedEventHandler += (sender, args) => { ctime = DateTime.Now.Ticks; closed.Set(); }; o.ReconnectWait = 500; o.NoRandomize = true; o.Servers = new string[] { "nats://localhost:4222", "nats://localhost:1222" }; o.SubChannelLength = 1; using (IConnection nc = new ConnectionFactory().CreateConnection(o), ncp = utils.DefaultTestConnection) { // On hosted environments, some threads/tasks can start before others // due to resource constraints. Allow time to start. Thread.Sleep(1000); def.Bounce(1000); Thread.Sleep(1000); Assert.True(reconnected.WaitOne(3000)); object asyncLock = new object(); EventHandler <MsgHandlerEventArgs> eh = (sender, args) => { lock (asyncLock) { recvCh.Set(); if (args.Message.Subject.Equals("foo")) { recvCh1.Set(); } else { recvCh2.Set(); } } }; IAsyncSubscription sub1 = nc.SubscribeAsync("foo", eh); IAsyncSubscription sub2 = nc.SubscribeAsync("bar", eh); nc.Flush(); ncp.Publish("foo", System.Text.Encoding.UTF8.GetBytes("hello")); ncp.Publish("bar", System.Text.Encoding.UTF8.GetBytes("hello")); ncp.Flush(); recvCh.WaitOne(3000); for (int i = 0; i < 3; i++) { ncp.Publish("foo", System.Text.Encoding.UTF8.GetBytes("hello")); ncp.Publish("bar", System.Text.Encoding.UTF8.GetBytes("hello")); } ncp.Flush(); Assert.True(asyncErr1.WaitOne(3000)); Assert.True(asyncErr2.WaitOne(3000)); def.Shutdown(); Thread.Sleep(1000); closed.Reset(); nc.Close(); Assert.True(closed.WaitOne(3000)); } if (dtime1 == orig || dtime2 == orig || rtime == orig || atime1 == orig || atime2 == orig || ctime == orig) { Console.WriteLine("Error = callback didn't fire: {0}\n{1}\n{2}\n{3}\n{4}\n{5}\n", dtime1, dtime2, rtime, atime1, atime2, ctime); throw new Exception("Callback didn't fire."); } if (rtime < dtime1 || dtime2 < rtime || ctime < atime2) { Console.WriteLine("Wrong callback order:\n" + "dtime1: {0}\n" + "rtime: {1}\n" + "atime1: {2}\n" + "atime2: {3}\n" + "dtime2: {4}\n" + "ctime: {5}\n", dtime1, rtime, atime1, atime2, dtime2, ctime); throw new Exception("Invalid callback order."); } } }
public void TestSubDelTaskCountReconnect() { bool disconnected = false; AutoResetEvent reconnectEv = new AutoResetEvent(false); var opts = utils.DefaultTestOptions; opts.SubscriberDeliveryTaskCount = 2; opts.DisconnectedEventHandler = (obj, args) => { disconnected = true;}; opts.ReconnectedEventHandler = (obj, args) => { reconnectEv.Set(); }; using (var server = new NATSServer()) { using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { long received = 0; int max = 10; AutoResetEvent ev = new AutoResetEvent(false); using (var s = c.SubscribeAsync("foo", (obj, args) => { received++; if (received == max) ev.Set(); })) { for (int i = 0; i < max / 2; i++) { c.Publish("foo", null); } c.Flush(); // bounce the server, we should reconnect, then // be able to receive messages. server.Bounce(100); Assert.True(reconnectEv.WaitOne(20000)); Assert.True(disconnected); for (int i = 0; i < max / 2; i++) { c.Publish("foo", null); } c.Flush(); Assert.True(ev.WaitOne(10000)); Assert.True(received == max); } } } }
public void TestPublishErrorsDuringReconnect() { AutoResetEvent connectedEv = new AutoResetEvent(false); using (var server = new NATSServer()) { Task t = new Task(() => { connectedEv.WaitOne(10000); Random r = new Random(); // increase this count for a longer running test. for (int i = 0; i < 10; i++) { server.Bounce(r.Next(500)); } }, TaskCreationOptions.LongRunning); t.Start(); byte[] payload = Encoding.UTF8.GetBytes("hello"); using (var c = utils.DefaultTestConnection) { connectedEv.Set(); while (t.IsCompleted == false) { try { c.Publish("foo", payload); } catch (Exception e) { Assert.IsNotType<NATSConnectionClosedException>(e); Assert.False(c.IsClosed()); } } } } }