public async Task Close() { _server .WhenForAnyArgs(x => x.Open()) .Do(x => { _server.Connected += Raise.EventWith(new dotnet_sockets.EventArgs <bool>(true)); }); _server .WhenForAnyArgs(x => x.Close()) .Do(x => { _server.Disconnected += Raise.EventWith(new dotnet_sockets.EventArgs <bool>(false)); }); _server.IsConnected.Returns(false, true); INATS nats = new NATS(_factory, _opts, _log); nats.ShouldNotBe(null); nats.Servers.ShouldBe(1); nats.Connected.ShouldBe(false); var c = await nats.Connect(); c.ShouldBe(true); nats.Connected.ShouldBe(true); _server.Received(1).Connected += Arg.Any <EventHandler <dotnet_sockets.EventArgs <bool> > >(); _server.Received(1).Disconnected += Arg.Any <EventHandler <dotnet_sockets.EventArgs <bool> > >(); nats.Close(); _server.Received(1).Open(); _server.Received(1).Send(Arg.Is <string>(cConnect)); _server.Received(1).Close(); }
public async Task Subscribe() { INATS nats = new NATS(_factory, _opts, _log); nats.ShouldNotBe(null); _server.Received().Sent += Arg.Any <EventHandler <dotnet_sockets.EventArgs <int> > >(); await nats.Connect(); nats.Subscribe("a", Substitute.For <Action <string> >()); _server.Received(1).Send(Arg.Is <string>(makeSubscription("a"))); }
public async Task Publish() { INATS nats = new NATS(_factory, _opts, _log); nats.ShouldNotBe(null); _server.Received().Sent += Arg.Any <EventHandler <dotnet_sockets.EventArgs <int> > >(); await nats.Connect(); nats.Publish("a", "data"); _server.Received(1).Send(Arg.Is <string>(makePublication("a", "data"))); _log.Received().Trace("Sent {0} bytes to server @ {1}", 1, _server.URL); }
void run(CLIOptions opts) { ILog log = null; INATS nats = null; try { log = new dotnet_nats.log.Logger(opts.loglevel); IFactory f = new Factory(log); nats = new NATS(f, opts, log); var t = nats.Connect(); t.Wait(); if (t.Result) { if (opts.mode.Equals("pub", StringComparison.InvariantCultureIgnoreCase)) { publish(nats, opts.subject, opts.data, opts.count, log); } else if (opts.mode.Equals("sub", StringComparison.InvariantCultureIgnoreCase)) { subscribe(nats, opts.subject, opts.count, log); } else { log.Fatal("Unknown mode supplied: {0}", opts.mode); } } else { throw new Exception("Failed to connect to server"); } } catch (Exception ex) { if (log != null) { log.Error("Error processing", ex); } //throw; } finally { if (nats != null) { nats.Close(); } nats = null; } }
public async Task Connect_NoServer() { _server.IsConnected.Returns(false); _opts.uris = new List <string>() { }; _servers.Clear(); INATS nats = new NATS(_factory, _opts, _log); nats.ShouldNotBe(null); nats.Servers.ShouldBe(0); nats.Connected.ShouldBe(false); var c = await nats.Connect(); c.ShouldBe(false); nats.Connected.ShouldBe(false); _server.DidNotReceive().Connected += Arg.Any <EventHandler <dotnet_sockets.EventArgs <bool> > >(); _server.DidNotReceive().Open(); _server.DidNotReceive().Send(Arg.Any <string>()); }
public async Task Connect_Fail() { _server.IsConnected.Returns(false); _server .WhenForAnyArgs(x => x.Open()) .Do(x => { _server.Error += Raise.EventWith(new dotnet_sockets.EventArgs <Exception>(new System.Net.Sockets.SocketException(10061))); }); //_transport.WhenForAnyArgs(x => x.Open()).Do(x => { throw new System.Net.Sockets.SocketException(10061); }); INATS nats = new NATS(_factory, _opts, _log); nats.ShouldNotBe(null); nats.Servers.ShouldBe(1); nats.Connected.ShouldBe(false); var c = await nats.Connect(); c.ShouldBe(true); nats.Connected.ShouldBe(false); _server.Received().Connected += Arg.Any <EventHandler <dotnet_sockets.EventArgs <bool> > >(); _server.Received().Error += Arg.Any <EventHandler <dotnet_sockets.EventArgs <Exception> > >(); _server.Received().Open(); _log.Received().Error("Error with server @ {0}", cURL, Arg.Any <Exception>()); _server.DidNotReceive().Send(Arg.Any <string>()); }
public async Task PublishConfirm() { Action <string> handler = Substitute.For <Action <string> >(); byte[] pong = Encoding.UTF8.GetBytes(PONG); _msgr.When(x => x.Receive(Arg.Is <byte[]>(pong), Arg.Is <int>(pong.Length))) .Do(x => { handler(""); }); _server .When(x => x.Send(Arg.Is <string>("PING\r\n"))) .Do(x => { _server.ReceivedData += Raise.EventWith(new dotnet_sockets.SocketDataArgs(null, pong, pong.Length)); }); INATS nats = new NATS(_factory, _opts, _log); nats.ShouldNotBe(null); _server.Received().Sent += Arg.Any <EventHandler <dotnet_sockets.EventArgs <int> > >(); _server.Received().ReceivedData += Arg.Any <EventHandler <dotnet_sockets.SocketDataArgs> >(); await nats.Connect(); nats.Publish("a", "data", handler); _server.Received(1).Send(Arg.Is <string>(makePublication("a", "data"))); _server.Received(1).Send(Arg.Is <string>("PING\r\n")); _log.Received(2).Trace("Sent {0} bytes to server @ {1}", 1, _server.URL); handler.ReceivedCalls().Count().ShouldBe(1); }