예제 #1
0
        public async Task Send_AllMessagesAreSent_PublisherIsDisconnectedMultipleTimes(int numberOfMessagesToSend,
                                                                                       float chanceOfClientFailure)
        {
            // in this test we will try to send n messages to server while keep disconnecting the client
            // to make sure that messages will be received by the server and the acknowledge will be received
            // by the client

            // declare variables
            var topicName     = RandomGenerator.GenerateString(10);
            var loggerFactory = new LoggerFactory();
            var messageStore  = new MessageStore(loggerFactory.CreateLogger <MessageStore>());

            messageStore.Setup(topicName, numberOfMessagesToSend);
            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8002);
            var clientConnectionConfiguration = new ClientConnectionConfiguration
            {
                AutoReconnect = true,
                EndPoint      = serverEndPoint
            };

            // setup server
            var brokerBuilder = new BrokerBuilder();

            using var broker = brokerBuilder
                               .UseMemoryStore()
                               .UseEndPoint(serverEndPoint)
                               .Build();

            broker.Start();

            var clientFactory = new BrokerClientFactory();

            // setup publisher
            await using var publisherClient = clientFactory.GetClient();
            publisherClient.Connect(clientConnectionConfiguration);

            // declare topic
            var declareResult = await publisherClient.DeclareTopicAsync(topicName, topicName);

            Assert.True(declareResult.IsSuccess);

            while (messageStore.SentCount < numberOfMessagesToSend)
            {
                if (RandomGenerator.GenerateDouble() < chanceOfClientFailure)
                {
                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        publisherClient.ConnectionManager.Socket.SimulateInterrupt();
                    });
                }

                var msg = messageStore.NewMessage();

                var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));

                var publishResult = await publisherClient.PublishAsync(topicName, msg.Data.ToArray(), cancellationTokenSource.Token);

                if (publishResult.IsSuccess)
                {
                    messageStore.OnMessageSent(msg.Id);
                }
            }

            messageStore.WaitForAllMessageToBeSent();
        }
예제 #2
0
        public async Task Receive_AllMessagesAreReceived_SomeMessagesAreNacked(int numberOfMessagesToBeReceived,
                                                                               float changeForMessageToBeNacked)
        {
            // declare variables
            var topicName     = RandomGenerator.GenerateString(10);
            var loggerFactory = new LoggerFactory();
            var messageStore  = new MessageStore(loggerFactory.CreateLogger <MessageStore>());

            messageStore.Setup(topicName, numberOfMessagesToBeReceived);
            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8001);
            var clientConnectionConfiguration = new ClientConnectionConfiguration
            {
                AutoReconnect = true,
                EndPoint      = serverEndPoint
            };

            // setup server
            var brokerBuilder = new BrokerBuilder();

            using var broker = brokerBuilder
                               .UseMemoryStore()
                               .UseEndPoint(serverEndPoint)
                               .Build();

            broker.Start();

            var clientFactory = new BrokerClientFactory();

            // setup subscriber
            await using var subscriberClient = clientFactory.GetClient();
            subscriberClient.Connect(clientConnectionConfiguration);

            // declare topic
            var declareResult = await subscriberClient.DeclareTopicAsync(topicName, topicName);

            Assert.True(declareResult.IsSuccess);

            // setup a topic with test data
            PopulateTopicWithMessage(topicName, numberOfMessagesToBeReceived, messageStore, broker);

            // get new subscription
            var subscription = await subscriberClient.GetTopicSubscriptionAsync(topicName);

            // setup subscriber
            subscription.MessageReceived += msg =>
            {
                if (RandomGenerator.GenerateDouble() < changeForMessageToBeNacked)
                {
                    msg.Nack();
                    return;
                }

                // note: the id of msg has changed
                var messageData = new Guid(msg.Data.Span);

                messageStore.OnMessageReceived(messageData);

                msg.Ack();
            };

            messageStore.WaitForAllMessageToBeReceived();
        }
        public async Task BenchmarkTest(int messageCount)
        {
            var topicName      = RandomGenerator.GenerateString(10);
            var messageStore   = new MessageStore(NullLogger <MessageStore> .Instance);
            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8100);
            var clientConnectionConfiguration = new ClientConnectionConfiguration
            {
                AutoReconnect = true,
                EndPoint      = serverEndPoint
            };

            // setup message store
            messageStore.Setup(topicName, messageCount);

            // setup server
            using var broker = new BrokerBuilder()
                               .UseMemoryStore()
                               .UseEndPoint(serverEndPoint)
                               .Build();

            broker.Start();

            var clientFactory = new BrokerClientFactory();

            // setup publisher
            await using var publisherClient = clientFactory.GetClient();
            publisherClient.Connect(clientConnectionConfiguration);

            // setup subscriber
            await using var subscriberClient = clientFactory.GetClient();
            subscriberClient.Connect(clientConnectionConfiguration);

            // declare topic
            var declareResult = await publisherClient.DeclareTopicAsync(topicName, topicName);

            Assert.True(declareResult.IsSuccess);

            // create subscription
            var subscription = await subscriberClient.GetTopicSubscriptionAsync(topicName);

            subscription.MessageReceived += msg =>
            {
                var messageIdentifier = new Guid(msg.Data.Span);

                messageStore.OnMessageReceived(messageIdentifier);

                msg.Ack();
            };

            // send messages to server
            while (messageStore.SentCount < messageCount)
            {
                var msg = messageStore.NewMessage();

                var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));

                var publishResult = await publisherClient.PublishAsync(topicName, msg.Data.ToArray(), cancellationTokenSource.Token);

                if (publishResult.IsSuccess)
                {
                    messageStore.OnMessageSent(msg.Id);
                }
            }

            // wait for messages to be sent
            messageStore.WaitForAllMessageToBeSent();

            // wait for messages to be received
            messageStore.WaitForAllMessageToBeReceived();
        }