Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for delete queue address operation #74

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/ArtemisNetCoreClient/Framing/SessionDeleteQueueMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using ActiveMQ.Artemis.Core.Client.InternalUtilities;

namespace ActiveMQ.Artemis.Core.Client.Framing;

internal readonly struct SessionDeleteQueueMessage : IOutgoingPacket
{
public PacketType PacketType => PacketType.SessionDeleteQueueMessage;

public required string QueueName { get; init; }

public int GetRequiredBufferSize()
{
var byteCount = 0;
byteCount += ArtemisBinaryConverter.GetSimpleStringByteCount(QueueName);
return byteCount;
}

public int Encode(Span<byte> buffer)
{
var offset = 0;
offset += ArtemisBinaryConverter.WriteSimpleString(ref buffer.GetReference(), QueueName);
return offset;
}
}
5 changes: 5 additions & 0 deletions src/ArtemisNetCoreClient/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public interface ISession : IAsyncDisposable
/// Create a queue with the given configuration if it does not already exist.
/// </summary>
Task CreateQueueAsync(QueueConfiguration queueConfiguration, CancellationToken cancellationToken);

/// <summary>
/// Delete a queue.
/// </summary>
Task DeleteQueueAsync(string queueName, CancellationToken cancellationToken);

/// <summary>
/// Get information about a queue.
Expand Down
1 change: 1 addition & 0 deletions src/ArtemisNetCoreClient/PacketType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ internal enum PacketType : byte
SessionConsumerFlowCreditMessage = 70,
SessionReceiveMessage = 75,
Exception = 20,
SessionDeleteQueueMessage = 35
}
42 changes: 35 additions & 7 deletions src/ArtemisNetCoreClient/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,32 @@ public async Task CreateQueueAsync(QueueConfiguration queueConfiguration, Cancel
_lock.Release();
}
}

public async Task DeleteQueueAsync(string queueName, CancellationToken cancellationToken)
{
var request = new SessionDeleteQueueMessage
{
QueueName = queueName
};

await _lock.WaitAsync(cancellationToken);
try
{
var tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
_ = _completionSources.TryAdd(-1, tcs);
connection.Send(request, ChannelId);
await tcs.Task;
}
catch (Exception)
{
_completionSources.TryRemove(-1, out _);
throw;
}
finally
{
_lock.Release();
}
}

public async Task<IConsumer> CreateConsumerAsync(ConsumerConfiguration consumerConfiguration, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -403,13 +429,15 @@ public void OnPacket(in InboundPacket packet)
var response = new SessionQueueQueryResponseMessage(packet.Payload);
if (_completionSources.TryRemove(-1, out var tcs))
{
var queueInfo = new QueueInfo
{
AddressName = response.Address!,
QueueName = response.Name!,
RoutingType = response.RoutingType,
};
tcs.TrySetResult(queueInfo);
var result = response.Exists
? new QueueInfo
{
AddressName = response.Address!,
QueueName = response.Name!,
RoutingType = response.RoutingType,
}
: _emptyResult;
tcs.TrySetResult(result);
}

break;
Expand Down
30 changes: 29 additions & 1 deletion test/ArtemisNetCoreClient.Tests/SessionSpec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ActiveMQ.Artemis.Core.Client.Framing;
using ActiveMQ.Artemis.Core.Client.InternalUtilities;
using ActiveMQ.Artemis.Core.Client.Tests.Utils;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -76,7 +77,7 @@ public async Task Should_not_return_address_info_when_address_does_not_exist()
Assert.Null(addressInfo);
}

[Fact(Skip = "Temporarily disabled")]
[Fact]
public async Task should_not_return_queue_info_when_queue_does_not_exist()
{
// Arrange
Expand Down Expand Up @@ -139,4 +140,31 @@ public async Task Should_create_and_dispose_producer()

await producer.DisposeAsync();
}

[Fact]
public async Task Should_delete_queue()
{
// Arrange
await using var testFixture = await TestFixture.CreateAsync(testOutputHelper);
await using var connection = await testFixture.CreateConnectionAsync();
await using var session = await connection.CreateSessionAsync(testFixture.CancellationToken);

var addressName = Guid.NewGuid().ToString();
await session.CreateAddressAsync(addressName, new [] { RoutingType.Multicast }, testFixture.CancellationToken);

var queueName = Guid.NewGuid().ToString();
await session.CreateQueueAsync(new QueueConfiguration
{
Address = addressName,
Name = queueName,
RoutingType = RoutingType.Multicast
}, testFixture.CancellationToken);

// Act
await session.DeleteQueueAsync(queueName, testFixture.CancellationToken);

// Assert
var queueInfo = await session.GetQueueInfoAsync(queueName, testFixture.CancellationToken);
Assert.Null(queueInfo);
}
}
Loading