예제 #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();
        }
예제 #2
0
        public void Ping()
        {
            INATS nats = new NATS(_factory, _opts, _log);

            nats.ShouldNotBe(null);
            _msgr.Ping += Raise.Event();
            _server.Received(1).Send(Arg.Is <string>(PONG));
            _log.Received().Trace("Sent {0} bytes to server @ {1}", 1, _server.URL);
        }
예제 #3
0
        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")));
        }
예제 #4
0
        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);
        }
예제 #5
0
        public void Unsubscribe()
        {
            INATS nats = new NATS(_factory, _opts, _log);

            nats.ShouldNotBe(null);
            _server.Received().Sent += Arg.Any <EventHandler <dotnet_sockets.EventArgs <int> > >();
            nats.Subscribe("a", Substitute.For <Action <string> >());
            _server.Received(1).Send(Arg.Is <string>(makeSubscription("a")));
            var sid = _sid - 1;

            nats.Unsubscribe("a");
            _server.Received(1).Send(Arg.Is <string>(makeUnsubscription(sid)));
        }
예제 #6
0
        public void Error()
        {
            INATS nats = new NATS(_factory, _opts, _log);

            nats.ShouldNotBe(null);

            _msgr.Error += Raise.Event();

            _server.Received(1).Close();
            _log.Received(1).Info("Disconnecting from Server @ {0}", _server.URL);
            _log.Received(1).Warn("Disconnected from server @ {0}", _server.URL);
            _log.Received(1).Debug("Reconnecting to server");
            _server.Received(1).Open();
        }
예제 #7
0
        public void Instantiate_SingleServer()
        {
            Options opts = new Options();

            opts.uris = new List <string>()
            {
                cURL
            };

            INATS nats = new NATS(_factory, opts, _log);

            nats.ShouldNotBe(null);
            nats.Servers.ShouldBe(1);
            nats.Connected.ShouldBe(false);
        }
예제 #8
0
        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>());
        }
예제 #9
0
        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>());
        }
예제 #10
0
        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);
        }