public void TestMissingMessages()
        {
            // arrange
            var          connectionMultiplexer = _redis.GetConnection();
            var          publisher             = new ReliablePublisher(connectionMultiplexer);
            var          subscriber            = new TogglingSubscriber(connectionMultiplexer, new MessageParser());
            int          receivedMessagesCount = 0;
            const string channelName           = "test-channel-" + nameof(TestMissingMessages);
            var          messageHandler        = new MessageHandler((channel, msg) => Interlocked.Increment(ref receivedMessagesCount));

            subscriber.Subscribe(channelName, messageHandler);

            // act
            publisher.Publish(channelName, "message1");
            Wait(10);
            subscriber.IsEnabled = false; // simulation of disability to process messages
            publisher.Publish(channelName, "message2");
            Wait(10);
            subscriber.IsEnabled = true;
            publisher.Publish(channelName, "message3");
            Wait(10);

            // assert
            Assert.Equal(3, receivedMessagesCount);
        }
        public void GetSavedMessages()
        {
            // arrange
            var connectionMultiplexer = _redis.GetConnection();
            var publisher             = new ReliablePublisher(connectionMultiplexer);
            var loader = new MessageLoader(connectionMultiplexer);

            // act
            const string channelName = "test-channel-" + nameof(GetSavedMessages);

            publisher.Publish(channelName, "message1");
            publisher.Publish(channelName, "message2");
            var savedMessages = loader.GetMessages(channelName, 0)
                                .ToList();

            // assert
            Assert.Equal(2, savedMessages.Count);
        }
Exemplo n.º 3
0
        public void PublishTest()
        {
            // arrange
            var channelName     = "test-channel-" + nameof(PublishTest);
            var publisher       = new ReliablePublisher(_redis.GetConnection());
            var receivedMessage = "";
            var subscriber      = _redis.GetConnection().GetSubscriber(); // standard, not-reliable subscriber

            subscriber.Subscribe(channelName, (channel, message) => receivedMessage += message);

            // act
            publisher.Publish(channelName, "test message:my message");
            Thread.Sleep(50);

            // assert
            Assert.Equal("1:test message:my message", receivedMessage);
        }
Exemplo n.º 4
0
        public void PublishWillSaveMessageInRedis()
        {
            // arrange
            var channelName = "test-channel-" + nameof(PublishWillSaveMessageInRedis);
            var publisher   = new ReliablePublisher(_redis.GetConnection());

            // act
            var messageExpiration = TimeSpan.FromSeconds(30);

            publisher.Publish(channelName, "test message:my message", messageExpiration);

            // assert
            var database     = _redis.GetConnection().GetDatabase();
            var savedMessage = database.StringGetWithExpiry($"ch:{{{channelName}}}:1");

            Assert.Equal("test message:my message", savedMessage.Value);
            Assert.NotNull(savedMessage.Expiry);
            Assert.True(savedMessage.Expiry > TimeSpan.Zero && savedMessage.Expiry < TimeSpan.FromSeconds(60), $"Timespan {savedMessage.Expiry} is not within given range.");
        }
 public void Publish(string channel, string message, TimeSpan messageExpiration = default)
 {
     _publisher.Publish(channel, message, messageExpiration);
 }