Example #1
0
        public async Task GetSubscriptionTest()
        {
            const string subject        = "some_subject";
            const string paload         = "some payload";
            var          encodedPayload = Encoding.UTF8.GetBytes(paload);
            string       subscriptionId = null;
            var          resetEvent     = new ManualResetEvent(false);

            var messageSubject = new Subject <Message <IncomingMessage> >();

            var mockNatsConnection = new Mock <INatsConnection>();

            mockNatsConnection
            .Setup(x => x.Write(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns <byte[], CancellationToken>((data, _) =>
            {
                var message = Encoding.UTF8.GetString(data);
                var match   = Regex.Match(message, $"^SUB {subject} ([^ \r\n]+)\r\n$");
                if (match.Success)
                {
                    subscriptionId = match.Groups[1].Value;
                    resetEvent.Set();
                }

                return(Task.CompletedTask);
            });

            mockNatsConnection.Setup(x => x.Messages).Returns(messageSubject);
            mockNatsConnection.Setup(x => x.OnConnect).Returns(Observable.Return(new ServerInfo()));
            mockNatsConnection.Setup(x => x.ConnectionState).Returns(NatsConnectionState.Connected);

            using (var client = new NatsClient(mockNatsConnection.Object))
            {
                var subscription = client.GetSubscription(subject)
                                   .FirstAsync()
                                   .Timeout(_timeout)
                                   .ToTask();

                resetEvent.WaitOne(_timeout);
                var incomingMessage = new IncomingMessage(subject, subscriptionId, string.Empty, encodedPayload.Length, encodedPayload);
                messageSubject.OnNext(Message.From("MSG", incomingMessage));

                var message = await subscription;
                Assert.Equal(incomingMessage, message);
            }

            mockNatsConnection.Verify(x =>
                                      x.Write(
                                          It.Is <byte[]>(data => Encoding.UTF8.GetString(data).StartsWith($"SUB {subject} ")),
                                          It.IsAny <CancellationToken>()), Times.Once);

            mockNatsConnection.Verify(x =>
                                      x.Write(
                                          It.Is <byte[]>(data => Encoding.UTF8.GetString(data).Contains($"UNSUB {subscriptionId}\r\n")),
                                          It.IsAny <CancellationToken>()));
        }
Example #2
0
        public void ResubscribeTest()
        {
            const int    reconnectCount = 3;
            const string subject        = "some_subject";
            string       subscriptionId = null;
            var          resetEvent     = new AutoResetEvent(false);

            var messageSubject     = new Subject <Message <IncomingMessage> >();
            var mockNatsConnection = new Mock <INatsConnection>();

            mockNatsConnection
            .Setup(x => x.Write(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns <byte[], CancellationToken>((data, _) =>
            {
                var message = Encoding.UTF8.GetString(data);
                var match   = Regex.Match(message, $"^SUB {subject} ([^ \r\n]+)\r\n$");
                if (match.Success)
                {
                    subscriptionId = match.Groups[1].Value;
                    resetEvent.Set();
                }

                return(Task.CompletedTask);
            });

            var onConnectMock = new Subject <ServerInfo>();

            mockNatsConnection.Setup(x => x.Messages).Returns(messageSubject);
            mockNatsConnection.Setup(x => x.OnConnect).Returns(onConnectMock);
            mockNatsConnection.Setup(x => x.ConnectionState).Returns(NatsConnectionState.Connected);

            using (var client = new NatsClient(mockNatsConnection.Object))
                using (client.GetSubscription(subject).Subscribe())
                {
                    for (var i = 0; i < reconnectCount; i++)
                    {
                        onConnectMock.OnNext(new ServerInfo());
                        resetEvent.WaitOne(_timeout);
                    }
                }

            mockNatsConnection.Verify(x =>
                                      x.Write(
                                          It.Is <byte[]>(data => Encoding.UTF8.GetString(data).StartsWith($"SUB {subject} ")),
                                          It.IsAny <CancellationToken>()), Times.Exactly(reconnectCount));

            mockNatsConnection.Verify(x =>
                                      x.Write(
                                          It.Is <byte[]>(data => Encoding.UTF8.GetString(data).Contains($"UNSUB {subscriptionId}\r\n")),
                                          It.IsAny <CancellationToken>()), Times.Once);
        }
Example #3
0
        public async Task AutoUnsubscribeTest(int messageCount, int take)
        {
            const string subject        = "some_subject";
            const string paload         = "some payload";
            var          encodedPayload = Encoding.UTF8.GetBytes(paload);
            string       subscriptionId = null;
            var          resetEvent     = new ManualResetEvent(false);

            var messageSubject = new Subject <Message <IncomingMessage> >();

            var mockNatsConnection = new Mock <INatsConnection>();

            mockNatsConnection
            .Setup(x => x.Write(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns <byte[], CancellationToken>((data, _) =>
            {
                var message = Encoding.UTF8.GetString(data);
                var match   = Regex.Match(message, $"^SUB {subject} ([^ \r\n].+)\r\n");
                if (match.Success)
                {
                    subscriptionId = match.Groups[1].Value;
                    resetEvent.Set();
                }

                return(Task.CompletedTask);
            });

            mockNatsConnection.Setup(x => x.Messages).Returns(messageSubject);
            mockNatsConnection.Setup(x => x.OnConnect).Returns(Observable.Return(new ServerInfo()));
            mockNatsConnection.Setup(x => x.ConnectionState).Returns(NatsConnectionState.Connected);

            using (var client = new NatsClient(mockNatsConnection.Object))
            {
                var subscription = client.GetSubscription(subject, messageCount)
                                   .Take(take)
                                   .ToArray()
                                   .Timeout(_timeout)
                                   .ToTask();

                resetEvent.WaitOne(_timeout);

                var otherMessage = new IncomingMessage(subject, "other_subscription", string.Empty, encodedPayload.Length, encodedPayload);
                messageSubject.OnNext(Message.From("MSG", otherMessage));
                var incomingMessage = new IncomingMessage(subject, subscriptionId, string.Empty, encodedPayload.Length, encodedPayload);
                for (var i = 0; i < messageCount + 1; i++)
                {
                    messageSubject.OnNext(Message.From("MSG", incomingMessage));
                }

                var messages = await subscription;
                Assert.Equal(Enumerable.Repeat(incomingMessage, Math.Min(take, messageCount)), messages);
            }

            mockNatsConnection.Verify(x =>
                                      x.Write(
                                          It.Is <byte[]>(data => Encoding.UTF8.GetString(data).Contains($"UNSUB {subscriptionId} {messageCount}\r\n")),
                                          It.IsAny <CancellationToken>()));

            mockNatsConnection.Verify(
                x => x.Write(
                    It.Is <byte[]>(data => Encoding.UTF8.GetString(data).Contains($"UNSUB {subscriptionId}\r\n")),
                    It.IsAny <CancellationToken>()),
                Times.Exactly(take < messageCount ? 1 : 0));
        }