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 overload for empty requests #502

Merged
merged 2 commits into from
May 29, 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
23 changes: 22 additions & 1 deletion src/NATS.Client.Core/INatsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public interface INatsConnection : IAsyncDisposable
/// <remarks>
/// Response can be (null) or one <see cref="NatsMsg{T}"/>.
/// Reply option's max messages will be set to 1.
/// if reply option's timeout is not defined then it will be set to NatsOpts.RequestTimeout.
/// If reply option's timeout is not defined, then it will be set to NatsOpts.RequestTimeout.
/// </remarks>
ValueTask<NatsMsg<TReply>> RequestAsync<TRequest, TReply>(
string subject,
Expand All @@ -143,6 +143,27 @@ ValueTask<NatsMsg<TReply>> RequestAsync<TRequest, TReply>(
NatsSubOpts? replyOpts = default,
CancellationToken cancellationToken = default);

/// <summary>
/// Send an empty request message and await the reply message asynchronously.
/// </summary>
/// <param name="subject">Subject of the responder</param>
/// <param name="replySerializer">Serializer to use for the reply message type.</param>
/// <param name="replyOpts">Reply handler subscription options</param>
/// <param name="cancellationToken">Cancel this request</param>
/// <typeparam name="TReply">Reply type</typeparam>
/// <returns>Returns the <see cref="NatsMsg{T}"/> received from the responder as reply.</returns>
/// <exception cref="OperationCanceledException">Raised when cancellation token is used</exception>
/// <remarks>
/// Response can be (null) or one <see cref="NatsMsg{T}"/>.
/// Reply option's max messages will be set to 1.
/// If reply option's timeout is not defined, then it will be set to NatsOpts.RequestTimeout.
/// </remarks>
ValueTask<NatsMsg<TReply>> RequestAsync<TReply>(
string subject,
INatsDeserialize<TReply>? replySerializer = default,
NatsSubOpts? replyOpts = default,
CancellationToken cancellationToken = default);

/// <summary>
/// Request and receive zero or more replies from a responder.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions src/NATS.Client.Core/NatsConnection.RequestReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ public async ValueTask<NatsMsg<TReply>> RequestAsync<TRequest, TReply>(
throw new NatsNoReplyException();
}

/// <inheritdoc />
public ValueTask<NatsMsg<TReply>> RequestAsync<TReply>(
string subject,
INatsDeserialize<TReply>? replySerializer = default,
NatsSubOpts? replyOpts = default,
CancellationToken cancellationToken = default) =>
RequestAsync<object, TReply>(
subject: subject,
data: default,
headers: default,
requestSerializer: default,
replySerializer: replySerializer,
requestOpts: default,
replyOpts: replyOpts,
cancellationToken: cancellationToken);

/// <inheritdoc />
public async IAsyncEnumerable<NatsMsg<TReply>> RequestManyAsync<TRequest, TReply>(
string subject,
Expand Down
26 changes: 26 additions & 0 deletions tests/NATS.Client.Core.Tests/RequestReplyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,30 @@ public async Task Request_reply_many_multiple_with_timeout_test()
await sub.DisposeAsync();
await reg;
}

[Fact]
public async Task Simple_empty_request_reply_test()
{
await using var server = NatsServer.Start();
await using var nats = server.CreateClientConnection();

const string subject = "foo";
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var cancellationToken = cts.Token;

var sub = await nats.SubscribeCoreAsync<int>(subject, cancellationToken: cancellationToken);
var reg = sub.Register(async msg =>
{
await msg.ReplyAsync(42, cancellationToken: cancellationToken);
});

var reply1 = await nats.RequestAsync<object, int>(subject, null, cancellationToken: cancellationToken);
var reply2 = await nats.RequestAsync<int>(subject, cancellationToken: cancellationToken);

Assert.Equal(42, reply1.Data);
Assert.Equal(42, reply2.Data);

await sub.DisposeAsync();
await reg;
}
}
2 changes: 2 additions & 0 deletions tests/NATS.Client.JetStream.Tests/NatsJsContextFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public ValueTask<NatsMsg<TReply>> RequestAsync<TRequest, TReply>(
CancellationToken cancellationToken = default)
=> throw new NotImplementedException();

public ValueTask<NatsMsg<TReply>> RequestAsync<TReply>(string subject, INatsDeserialize<TReply>? replySerializer = default, NatsSubOpts? replyOpts = default, CancellationToken cancellationToken = default) => throw new NotImplementedException();

public IAsyncEnumerable<NatsMsg<TReply>> RequestManyAsync<TRequest, TReply>(
string subject,
TRequest? data,
Expand Down
Loading