private CreateServerWithConfig ( string configFile ) : |
||
configFile | string | |
Результат |
public void TestAuthServers() { string[] plainServers = new string[] { "nats://*****:*****@localhost:1224" }; opts.Servers = authServers; using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { Assert.Equal(authServers[1], c.ConnectedUrl); } } }
public void TestAuthServers() { string[] plainServers = new string[] { "nats://*****:*****@localhost:1224" }; opts.Servers = authServers; using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { Assert.IsTrue(c.ConnectedUrl.Equals(authServers[1])); } } }
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 TestAuthSuccess() { using (NATSServer s = util.CreateServerWithConfig(TestContext, "auth_1222.conf")) { IConnection c = new ConnectionFactory().CreateConnection("nats://*****:*****@localhost:1222"); c.Close(); } }
public void TestNKey() { using (utils.CreateServerWithConfig("nkey.conf")) { Options opts = ConnectionFactory.GetDefaultOptions(); // See nkey.conf opts.SetNkey("UCKKTOZV72L3NITTGNOCRDZUI5H632XCT4ZWPJBC2X3VEY72KJUWEZ2Z", "./config/certs/user.nk"); new ConnectionFactory().CreateConnection(opts).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 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; ConditionalObj reconnected = new ConditionalObj(); ConditionalObj closed = new ConditionalObj(); ConditionalObj asyncErr1 = new ConditionalObj(); ConditionalObj asyncErr2 = new ConditionalObj(); ConditionalObj recvCh = new ConditionalObj(); ConditionalObj recvCh1 = new ConditionalObj(); ConditionalObj recvCh2 = new ConditionalObj(); using (NATSServer s = utils.CreateServerWithConfig(TestContext, "auth_1222.conf")) { Options o = ConnectionFactory.GetDefaultOptions(); o.DisconnectedEventHandler += (sender, args) => { Thread.Sleep(100); if (firstDisconnect) { System.Console.WriteLine("First disconnect."); firstDisconnect = false; dtime1 = DateTime.Now.Ticks; } else { System.Console.WriteLine("Second disconnect."); dtime2 = DateTime.Now.Ticks; } }; o.ReconnectedEventHandler += (sender, args) => { System.Console.WriteLine("Reconnected."); Thread.Sleep(50); rtime = DateTime.Now.Ticks; reconnected.notify(); }; o.AsyncErrorEventHandler += (sender, args) => { if (args.Subscription.Subject.Equals("foo")) { System.Console.WriteLine("Error handler foo."); Thread.Sleep(200); atime1 = DateTime.Now.Ticks; asyncErr1.notify(); } else { System.Console.WriteLine("Error handler bar."); atime2 = DateTime.Now.Ticks; asyncErr2.notify(); } }; o.ClosedEventHandler += (sender, args) => { System.Console.WriteLine("Closed handler."); ctime = DateTime.Now.Ticks; closed.notify(); }; o.ReconnectWait = 50; o.NoRandomize = true; o.Servers = new string[] { "nats://localhost:4222", "nats:localhost:1222" }; o.SubChannelLength = 1; using (IConnection nc = new ConnectionFactory().CreateConnection(o), ncp = new ConnectionFactory().CreateConnection()) { utils.StopDefaultServer(); Thread.Sleep(1000); utils.StartDefaultServer(); reconnected.wait(3000); EventHandler <MsgHandlerEventArgs> eh = (sender, args) => { System.Console.WriteLine("Received message on subject: " + args.Message.Subject); recvCh.notify(); if (args.Message.Subject.Equals("foo")) { recvCh1.notify(); } else { recvCh2.notify(); } }; 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.wait(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(); asyncErr1.wait(3000); asyncErr2.wait(3000); utils.StopDefaultServer(); Thread.Sleep(1000); closed.reset(); nc.Close(); closed.wait(3000); } if (dtime1 == orig || dtime2 == orig || rtime == orig || atime1 == orig || atime2 == orig || ctime == orig) { System.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 || atime2 < atime1 || ctime < atime2) { System.Console.WriteLine("Wrong callback order:{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n", dtime1, rtime, atime1, atime2, dtime2, ctime); throw new Exception("Invalid callback order."); } } }