Beispiel #1
0
        public async Task ValidateRedisPendingAsync()
        {
            var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync("localhost");

            var publisher     = new RedisStreamPublisher <SampleMessage, CreateEvent>(connectionMultiplexer);
            var subscriberA   = new RedisStreamSubscriber <SampleMessage, CreateEvent>(connectionMultiplexer, StreamPosition.NewMessages);
            var subscriberB   = new RedisStreamSubscriber <SampleMessage, CreateEvent>(connectionMultiplexer, StreamPosition.NewMessages);
            var sampleMessage = new SampleMessage();
            var original      = await publisher.Database.StreamInfoAsync(publisher.StreamKey);

            _ = await publisher.PublishAsync(sampleMessage);

            var result = await publisher.Database.StreamInfoAsync(publisher.StreamKey);

            Assert.AreEqual(original.Length + 1, result.Length);
            var subscribedMessages = await subscriberA.GetMessagesAsync();

            Assert.AreEqual(1, subscribedMessages.Count());
            subscribedMessages = await subscriberB.GetPendingMessagesAsync();

            Assert.AreNotEqual(0, subscribedMessages.Count());
            var count = 0L;

            foreach (var(Id, Entity) in subscribedMessages)
            {
                count += await subscriberB.AcknowledgeMessageAsync(Id);
            }
            Assert.AreNotEqual(0, count);
        }
Beispiel #2
0
        public async Task ValidateRedisMoqSubscriberGetMessagesAsync()
        {
            var moqConnection = new Mock <IConnectionMultiplexer>();
            var moqDatabase   = new Mock <IDatabase>();

            _ = moqDatabase
                .Setup(x => x.StreamReadGroupAsync(It.IsAny <RedisKey>(), It.IsAny <RedisValue>(), It.IsAny <RedisValue>(), It.IsAny <RedisValue>(), 1, false, CommandFlags.None))
                .Returns(Task.FromResult(Array.Empty <StreamEntry>()));
            _ = moqConnection.Setup(t => t.GetDatabase(-1, null)).Returns(moqDatabase.Object);
            var subscriber = new RedisStreamSubscriber <SampleMessage, CreateEvent>(moqConnection.Object);

            _ = await subscriber.GetMessagesAsync();

            moqDatabase
            .Verify(t => t.StreamReadGroupAsync(subscriber.StreamKey, subscriber.ConsumerGroupName, subscriber.ConsumerName, null, 1, false, CommandFlags.None), Times.Once);
        }
Beispiel #3
0
        public async Task ValidateRedisPublishAsync()
        {
            var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync("localhost");

            var publisher     = new RedisStreamPublisher <SampleMessage, CreateEvent>(connectionMultiplexer);
            var subscriber    = new RedisStreamSubscriber <SampleMessage, CreateEvent>(connectionMultiplexer, StreamPosition.NewMessages);
            var sampleMessage = new SampleMessage();
            var original      = await publisher.Database.StreamInfoAsync(publisher.StreamKey);

            _ = await publisher.PublishAsync(sampleMessage);

            var result = await publisher.Database.StreamInfoAsync(publisher.StreamKey);

            Assert.AreEqual(original.Length + 1, result.Length);
            var subscribedMessages = await subscriber.GetMessagesAsync();

            var(Id, Entity) = subscribedMessages.First();
            Assert.AreEqual(sampleMessage.Name, Entity.Name);
            var count = await subscriber.AcknowledgeMessageAsync(Id);

            Assert.AreEqual(1, count);
        }