Skip to content

Commit

Permalink
Add integration tests for StorageBus MessageStateHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasarbin committed Apr 15, 2024
1 parent b4f2ce1 commit 688061a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageIconUrl>https://raw.githubusercontent.com/BookBeat/knightbus/master/documentation/media/images/knighbus-64.png</PackageIconUrl>
<PackageIcon>knighbus-64.png</PackageIcon>
<RepositoryUrl>https://github.com/BookBeat/knightbus</RepositoryUrl>
<Version>16.0.0</Version>
<Version>16.0.1</Version>
<PackageTags>knightbus;azure storage;blob;queues;messaging</PackageTags>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Task AbandonByErrorAsync(Exception e)
}

_message.Properties["Error"] = errorMessage;
return _queueClient.AbandonByErrorAsync(_message, TimeSpan.FromSeconds(2));
return _queueClient.AbandonByErrorAsync(_message, null);
}

public Task DeadLetterAsync(int deadLetterLimit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using KnightBus.Azure.Storage.Management;
using KnightBus.Core;
using KnightBus.Core.PreProcessors;
using KnightBus.Newtonsoft;
using KnightBus.Shared.Tests.Integration;
using NUnit.Framework;

namespace KnightBus.Azure.Storage.Tests.Integration;

[TestFixture]
public class StorageQueueMessageStateHandlerTests : MessageStateHandlerTests<TestCommand>
{
private readonly StorageBusConfiguration _configuration = new("UseDevelopmentStorage=true");
public override async Task Setup()
{
var queueManager = new StorageQueueManager(_configuration, Array.Empty<IMessagePreProcessor>());

var queues = await queueManager.List(CancellationToken.None);
foreach (var queue in queues)
{
await queueManager.Delete(queue.Name, CancellationToken.None);
}
}

protected override async Task<List<TestCommand>> GetMessages(int count)
{
var qc = new StorageQueueClient(_configuration, _configuration.MessageSerializer, Array.Empty<IMessagePreProcessor>(), AutoMessageMapper.GetQueueName<TestCommand>());
var messages = await qc.PeekMessagesAsync<TestCommand>(count);
return messages.Select(m => (TestCommand)m.Message).ToList();
}

protected override async Task<List<TestCommand>> GetDeadLetterMessages(int count)
{
var qc = new StorageQueueClient(_configuration, _configuration.MessageSerializer, Array.Empty<IMessagePreProcessor>(), AutoMessageMapper.GetQueueName<TestCommand>());
var messages = await qc.PeekDeadLettersAsync<TestCommand>(count);
return messages.Select(m => (TestCommand)m.Message).ToList();
}

protected override async Task SendMessage(string message)
{
var client = new StorageQueueClient(_configuration, new NewtonsoftSerializer(), Array.Empty<IMessagePreProcessor>(), AutoMessageMapper.GetQueueName<TestCommand>());
await client.CreateIfNotExistsAsync();
await client.SendAsync(new TestCommand { Message = message }, TimeSpan.Zero, CancellationToken.None);
}

protected override async Task<IMessageStateHandler<TestCommand>> GetMessageStateHandler()
{
var client = new StorageQueueClient(_configuration, new NewtonsoftSerializer(), Array.Empty<IMessagePreProcessor>(), AutoMessageMapper.GetQueueName<TestCommand>());
var message = await client.GetMessagesAsync<TestCommand>(1, TimeSpan.FromSeconds(5));
return new StorageQueueMessageStateHandler<TestCommand>(client, message.First(), 5, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ public abstract class MessageStateHandlerTests<TCommand> where TCommand : class,
protected abstract Task SendMessage(string message);
protected abstract Task<IMessageStateHandler<TCommand>> GetMessageStateHandler();


[Test]
public async Task Should_lock_the_message_and_make_it_invisible_when_processing()
{
//arrange
await SendMessage("Testing Visibility");

//act
await GetMessageStateHandler();


//assert
var messages = await GetMessages(1);
messages.Should().BeEmpty();
}

[Test]
public async Task Should_complete_the_message_and_remove_it_from_the_queue()
{
Expand Down

0 comments on commit 688061a

Please sign in to comment.