Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        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;
            }
        }