Skip to content

Commit

Permalink
Support multiple session creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Apr 22, 2024
1 parent 0dd7402 commit 977d4d3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ArtemisNetCoreClient/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class Connection : IConnection, IChannel
private readonly ConcurrentDictionary<long, IChannel> _channels = new();
private readonly ConcurrentDictionary<long, TaskCompletionSource<IIncomingPacket>> _completionSources = new();
private readonly SemaphoreSlim _lock = new(1, 1);
private readonly IdGenerator _sessionChannelIdGenerator = new(10);
private volatile bool _disposed;

public Connection(ILoggerFactory loggerFactory, Transport2 transport, Endpoint endpoint)
Expand Down Expand Up @@ -83,7 +84,7 @@ public async Task<ISession> CreateSessionAsync(CancellationToken cancellationTok
var createSessionMessage = new CreateSessionMessage
{
Name = Guid.NewGuid().ToString(),
SessionChannelId = 10,
SessionChannelId = _sessionChannelIdGenerator.GenerateId(),
Version = 135,
Username = _endpoint.User,
Password = _endpoint.Password,
Expand Down
6 changes: 6 additions & 0 deletions src/ArtemisNetCoreClient/IdGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ActiveMQ.Artemis.Core.Client;

internal class IdGenerator(long startId)
{
public long GenerateId() => Interlocked.Increment(ref startId);
}
16 changes: 16 additions & 0 deletions test/ArtemisNetCoreClient.Tests/ConnectionSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,20 @@ public async Task should_create_session()
Assert.NotNull(session);
await session.DisposeAsync();
}

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

// Act
await using var session1 = await connection.CreateSessionAsync();
await using var session2 = await connection.CreateSessionAsync();

// Assert
Assert.NotNull(session1);
Assert.NotNull(session2);
}
}

0 comments on commit 977d4d3

Please sign in to comment.