Skip to content

Commit

Permalink
Create Address
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Mar 20, 2024
1 parent bfb0ab4 commit 72c98b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/ArtemisNetCoreClient/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void WriteString(string value)
}
else
{
WriteAsBytes(value);
WriteStringAsBytes(value);
}
}

Expand All @@ -128,7 +128,7 @@ private void WriteAsShorts(string value)
}
}

private void WriteAsBytes(string value)
public void WriteStringAsBytes(string value)
{
var chars = value.AsSpan();
WriteInt(chars.Length << 1);
Expand Down
2 changes: 1 addition & 1 deletion src/ArtemisNetCoreClient/Framing/CreateAddressMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class CreateAddressMessage : Packet

public override void Encode(ByteBuffer buffer)
{
buffer.WriteString(Address);
buffer.WriteStringAsBytes(Address);
buffer.WriteInt(RoutingTypes.Length);
foreach (var routingType in RoutingTypes)
{
Expand Down
10 changes: 5 additions & 5 deletions src/ArtemisNetCoreClient/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ public async Task CreateAddress(string address, IEnumerable<RoutingType> routing
AutoCreated = autoCreated,
RequiresResponse = true
};
_ = await SendBlockingAsync<CreateAddressMessage, NullResponse>(createAddressMessage, 11, cancellationToken);
_ = await SendBlockingAsync<CreateAddressMessage, NullResponse>(createAddressMessage, cancellationToken);
}

public async ValueTask DisposeAsync()
{
_ = await SendBlockingAsync<SessionStop, NullResponse>(new SessionStop(), ChannelId, default);
_ = await SendBlockingAsync<SessionCloseMessage, NullResponse>(new SessionCloseMessage(),ChannelId, default);
_ = await SendBlockingAsync<SessionStop, NullResponse>(new SessionStop(), default);
_ = await SendBlockingAsync<SessionCloseMessage, NullResponse>(new SessionCloseMessage(), default);
await _transport.DisposeAsync().ConfigureAwait(false);
}

private async Task<TResponse> SendBlockingAsync<TRequest, TResponse>(TRequest request, long channelId, CancellationToken cancellationToken) where TRequest : Packet
private async Task<TResponse> SendBlockingAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken) where TRequest : Packet
{
var tcs = new TaskCompletionSource<Packet>();

// TODO: Handle scenario when we cannot CorrelationId
_ = _completionSources.TryAdd(request.CorrelationId, tcs);

await _transport.SendAsync(request, channelId, cancellationToken);
await _transport.SendAsync(request, ChannelId, cancellationToken);
var responsePacket = await tcs.Task;
if (responsePacket is TResponse response)
{
Expand Down
36 changes: 29 additions & 7 deletions test/ArtemisNetCoreClient.Tests/SessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public async Task should_establish_session()
{
// Arrange
var connectionFactory = new SessionFactory();

// Act
var session = await connectionFactory.CreateAsync(new Endpoint
{
Expand All @@ -24,20 +24,42 @@ public async Task should_establish_session()
await session.DisposeAsync();
}

[Test, Ignore("WIP")]
public async Task should_create_address()
[TestCase(new[] { RoutingType.Anycast })]
[TestCase(new[] { RoutingType.Multicast })]
[TestCase(new[] { RoutingType.Anycast, RoutingType.Multicast })]
public async Task should_create_address_with_selected_routing_type(RoutingType[] routingTypes)
{
// Arrange
var connectionFactory = new SessionFactory();
var session = await connectionFactory.CreateAsync(new Endpoint
await using var session = await connectionFactory.CreateAsync(new Endpoint
{
Host = "localhost",
Port = 5445,
User = "artemis",
Password = "artemis"
});

// Act
var addressName = $"{Guid.NewGuid().ToString()}-{string.Join("-", routingTypes)}";
await session.CreateAddress(addressName, routingTypes, false, default);
}

[TestCase(false)]
[TestCase(true)]
public async Task should_create_address_with_autoCreated_flag(bool autoCreated)
{
// Arrange
var connectionFactory = new SessionFactory();
await using var session = await connectionFactory.CreateAsync(new Endpoint
{
Host = "localhost",
Port = 5445,
User = "artemis",
Password = "artemis"
});

// Act && Assert
await session.CreateAddress("myaddress", Enumerable.Empty<RoutingType>(), false, default);

// Act
var addressName = $"{Guid.NewGuid().ToString()}";
await session.CreateAddress(addressName, new[] { RoutingType.Multicast }, autoCreated: autoCreated, default);
}
}

0 comments on commit 72c98b9

Please sign in to comment.