-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an INatsJSMsg<T> interface (#266)
* Created an INatsJSMsg interface An INatsJSMsg interface can help with unit testing in common .net * There maybe performance implications due to struct to class change assuming not noticeable. Struct to class change should not introduce functional change sine it is immutable. * This would be a breaking change but the impact would be relatively small in most code bases. * Made NatsJSMsg.ctor public This could help with unit testing * Warnings and format fixes * Revert "Created an INatsJSMsg interface" This reverts commit bac3b14. * Revert "Warnings and format fixes" This reverts commit 88f68fe. * Use message interfaces only when required * Message interface docs * Fixed interface after merge
- Loading branch information
Showing
4 changed files
with
308 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace NATS.Client.Core.Tests; | ||
|
||
public class MessageInterfaceTest | ||
{ | ||
[Fact] | ||
public async Task Sub_custom_builder_test() | ||
{ | ||
await using var server = NatsServer.Start(); | ||
await using var nats = server.CreateClientConnection(); | ||
|
||
var sync = 0; | ||
var sub = Task.Run(async () => | ||
{ | ||
var count = 0; | ||
await foreach (var natsMsg in nats.SubscribeAsync<string>("foo.*")) | ||
{ | ||
if (natsMsg.Subject == "foo.sync") | ||
{ | ||
Interlocked.Increment(ref sync); | ||
continue; | ||
} | ||
|
||
if (natsMsg.Subject == "foo.end") | ||
{ | ||
break; | ||
} | ||
|
||
// Boxing allocation: conversion from 'NatsMsg<string>' to 'INatsMsg<string>' requires boxing of the value type | ||
// vvvvvvv | ||
ProcessMessage(count++, natsMsg); | ||
} | ||
}); | ||
|
||
await Retry.Until( | ||
reason: "subscription is ready", | ||
condition: () => Volatile.Read(ref sync) > 0, | ||
action: async () => await nats.PubAsync("foo.sync"), | ||
retryDelay: TimeSpan.FromSeconds(1)); | ||
|
||
for (var i = 0; i < 10; i++) | ||
await nats.PublishAsync(subject: $"foo.{i}", data: $"test_msg_{i}"); | ||
|
||
await nats.PubAsync("foo.end"); | ||
|
||
await sub; | ||
} | ||
|
||
private void ProcessMessage(int count, INatsMsg<string> natsMsg) => natsMsg.Data.Should().Be($"test_msg_{count}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using NATS.Client.Core.Tests; | ||
|
||
namespace NATS.Client.JetStream.Tests; | ||
|
||
public class MessageInterfaceTest | ||
{ | ||
[Fact] | ||
public async Task Using_message_interface() | ||
{ | ||
await using var server = NatsServer.StartJS(); | ||
await using var nats = server.CreateClientConnection(); | ||
var js = new NatsJSContext(nats); | ||
|
||
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); | ||
|
||
await js.CreateStreamAsync("s1", new[] { "s1.*" }, cts.Token); | ||
|
||
var ack = await js.PublishAsync("s1.foo", "test_msg", cancellationToken: cts.Token); | ||
ack.EnsureSuccess(); | ||
|
||
var consumer = await js.CreateConsumerAsync("s1", "c1", cancellationToken: cts.Token); | ||
|
||
await foreach (var natsJSMsg in consumer.ConsumeAsync<string>(cancellationToken: cts.Token)) | ||
{ | ||
// Boxing allocation: conversion from 'NatsJSMsg<string>' to 'INatsJSMsg<string>' requires boxing of the value type | ||
// vvvvvvvvv | ||
await ProcessMessageAsync(natsJSMsg, cts.Token); | ||
break; | ||
} | ||
|
||
await Retry.Until( | ||
"ack pending 0", | ||
async () => | ||
{ | ||
var c = await js.GetConsumerAsync("s1", "c1", cts.Token); | ||
return c.Info.NumAckPending == 0; | ||
}, | ||
retryDelay: TimeSpan.FromSeconds(1), | ||
timeout: TimeSpan.FromSeconds(20)); | ||
await consumer.RefreshAsync(cts.Token); | ||
Assert.Equal(0, consumer.Info.NumAckPending); | ||
} | ||
|
||
private async Task ProcessMessageAsync(INatsJSMsg<string> natsJSMsg, CancellationToken cancellationToken = default) | ||
{ | ||
natsJSMsg.Data.Should().Be("test_msg"); | ||
await natsJSMsg.AckAsync(cancellationToken: cancellationToken); | ||
} | ||
} |