-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shared and redis tests for MessageStateHandler
- Loading branch information
niklasarbin
committed
Apr 12, 2024
1 parent
f1ec844
commit b4f2ce1
Showing
3 changed files
with
134 additions
and
11 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
knightbus-redis/tests/KnightBus.Redis.Tests.Integration/RedisMessageStateHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using KnightBus.Core; | ||
using KnightBus.Core.PreProcessors; | ||
using KnightBus.Redis.Management; | ||
using KnightBus.Shared.Tests.Integration; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace KnightBus.Redis.Tests.Integration; | ||
|
||
[TestFixture] | ||
public class RedisMessageStateHandlerTests : MessageStateHandlerTests<TestCommand> | ||
{ | ||
private RedisBus _bus; | ||
|
||
public override async Task Setup() | ||
{ | ||
_bus = new RedisBus(RedisTestBase.Configuration.ConnectionString, Array.Empty<IMessagePreProcessor>()); | ||
var logger = new Mock<ILogger<RedisManagementClient>>(); | ||
var managementClient = new RedisManagementClient(RedisTestBase.Configuration, logger.Object); | ||
var qm = new RedisQueueManager(managementClient, RedisTestBase.Configuration); | ||
_bus = new RedisBus(RedisTestBase.Configuration.ConnectionString, Array.Empty<IMessagePreProcessor>()); | ||
await qm.Delete(AutoMessageMapper.GetQueueName<TestCommand>(), CancellationToken.None); | ||
} | ||
|
||
protected override async Task<List<TestCommand>> GetMessages(int count) | ||
{ | ||
var queueName = AutoMessageMapper.GetQueueName<TestCommand>(); | ||
var q = new RedisQueueClient<TestCommand>(RedisTestBase.Database, queueName, RedisTestBase.Configuration.MessageSerializer, Mock.Of<ILogger>()); | ||
var m = await q.PeekMessagesAsync(count).ToListAsync(); | ||
return m.Select(x => x.Message).ToList(); | ||
} | ||
|
||
protected override async Task<List<TestCommand>> GetDeadLetterMessages(int count) | ||
{ | ||
var queueName = AutoMessageMapper.GetQueueName<TestCommand>(); | ||
var q = new RedisQueueClient<TestCommand>(RedisTestBase.Database, queueName, RedisTestBase.Configuration.MessageSerializer, Mock.Of<ILogger>()); | ||
var m = await q.PeekDeadlettersAsync(count).ToListAsync(); | ||
return m.Select(x => x.Message.Body).ToList(); | ||
} | ||
|
||
protected override async Task<string> SendMessage(string message) | ||
{ | ||
await _bus.SendAsync(new TestCommand(message)); | ||
return AutoMessageMapper.GetQueueName<TestCommand>(); | ||
} | ||
|
||
protected override async Task<IMessageStateHandler<TestCommand>> GetMessageStateHandler() | ||
{ | ||
var queueName = AutoMessageMapper.GetQueueName<TestCommand>(); | ||
var q = new RedisQueueClient<TestCommand>(RedisTestBase.Database, queueName, RedisTestBase.Configuration.MessageSerializer, Mock.Of<ILogger>()); | ||
var m = await q.GetMessagesAsync(1); | ||
return new RedisMessageStateHandler<TestCommand>(RedisTestBase.Multiplexer, RedisTestBase.Configuration, m.First(), 5, null, Mock.Of<ILogger>()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
knightbus/tests/KnightBus.Shared.Tests.Integration/MessageStateHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using KnightBus.Core; | ||
using KnightBus.Messages; | ||
using NUnit.Framework; | ||
|
||
namespace KnightBus.Shared.Tests.Integration; | ||
|
||
[TestFixture] | ||
public abstract class MessageStateHandlerTests<TCommand> where TCommand : class, IMessage | ||
{ | ||
[SetUp] | ||
public abstract Task Setup(); | ||
protected abstract Task<List<TCommand>> GetMessages(int count); | ||
protected abstract Task<List<TCommand>> GetDeadLetterMessages(int count); | ||
protected abstract Task SendMessage(string message); | ||
protected abstract Task<IMessageStateHandler<TCommand>> GetMessageStateHandler(); | ||
|
||
[Test] | ||
public async Task Should_complete_the_message_and_remove_it_from_the_queue() | ||
{ | ||
//arrange | ||
await SendMessage("Testing Complete"); | ||
var stateHandler = await GetMessageStateHandler(); | ||
|
||
//act | ||
await stateHandler.CompleteAsync(); | ||
|
||
//assert | ||
var messages = await GetMessages(1); | ||
messages.Should().BeEmpty(); | ||
} | ||
|
||
[Test] | ||
public async Task Should_abandon_the_message_and_return_it_to_the_queue() | ||
{ | ||
//arrange | ||
await SendMessage("Testing Abandon"); | ||
var stateHandler = await GetMessageStateHandler(); | ||
|
||
//act | ||
await stateHandler.AbandonByErrorAsync(new Exception()); | ||
|
||
//assert | ||
var messages = await GetMessages(10); | ||
messages.Should().HaveCount(1); | ||
} | ||
[Test] | ||
public async Task Should_dead_letter_the_message_and_move_it_to_the_dl_queue() | ||
{ | ||
//arrange | ||
await SendMessage("Testing Dead Letters"); | ||
var stateHandler = await GetMessageStateHandler(); | ||
|
||
//act | ||
await stateHandler.DeadLetterAsync(0); | ||
|
||
//assert | ||
var messages = await GetMessages(1); | ||
messages.Should().HaveCount(0); | ||
var deadLetters = await GetDeadLetterMessages(10); | ||
deadLetters.Count.Should().Be(1); | ||
} | ||
} |