Skip to content
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
4 changes: 4 additions & 0 deletions tests/NATS.Net.DocsExamples/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]
csharp_style_var_for_built_in_types = false:warning
csharp_style_var_when_type_is_apparent = false:warning
csharp_style_var_elsewhere = false:warning
24 changes: 12 additions & 12 deletions tests/NATS.Net.DocsExamples/Advanced/IntroPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public async Task Run()

{
#region lowlevel-sub
await using var nc = new NatsConnection();
await using NatsConnection nc = new NatsConnection();

// Connections are lazy, so we need to connect explicitly
// to avoid any races between subscription and publishing.
await nc.ConnectAsync();

await using var sub = await nc.SubscribeCoreAsync<int>("foo");
await using INatsSub<int> sub = await nc.SubscribeCoreAsync<int>("foo");

for (var i = 0; i < 10; i++)
for (int i = 0; i < 10; i++)
{
Console.WriteLine($" Publishing {i}...");
await nc.PublishAsync<int>("foo", i);
Expand All @@ -41,7 +41,7 @@ public async Task Run()

// Messages have been collected in the subscription internal channel
// now we can drain them
await foreach (var msg in sub.Msgs.ReadAllAsync())
await foreach (NatsMsg<int> msg in sub.Msgs.ReadAllAsync())
{
Console.WriteLine($"Received {msg.Subject}: {msg.Data}\n");
if (msg.Data == -1)
Expand All @@ -56,7 +56,7 @@ public async Task Run()

{
#region ping
await using var nc = new NatsClient();
await using NatsClient nc = new NatsClient();

TimeSpan rtt = await nc.PingAsync();

Expand All @@ -66,18 +66,18 @@ public async Task Run()

{
#region logging
using var loggerFactory = LoggerFactory.Create(configure: builder => builder.AddConsole());
using ILoggerFactory loggerFactory = LoggerFactory.Create(configure: builder => builder.AddConsole());

var opts = new NatsOpts { LoggerFactory = loggerFactory };
NatsOpts opts = new NatsOpts { LoggerFactory = loggerFactory };

await using var nc = new NatsClient(opts);
await using NatsClient nc = new NatsClient(opts);
#endregion
}

{
#region opts

var opts = new NatsOpts
NatsOpts opts = new NatsOpts
{
// You need to set pending in the constructor and not use
// the option here, as it will be ignored.
Expand All @@ -89,19 +89,19 @@ public async Task Run()
// ...
};

await using var nc = new NatsClient(opts, pending: BoundedChannelFullMode.DropNewest);
await using NatsClient nc = new NatsClient(opts, pending: BoundedChannelFullMode.DropNewest);
#endregion
}

{
#region opts2

var opts = new NatsOpts
NatsOpts opts = new NatsOpts
{
// Your custom options
};

await using var nc = new NatsConnection(opts);
await using NatsConnection nc = new NatsConnection(opts);
#endregion
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/NATS.Net.DocsExamples/Advanced/SecurityPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task Run()

{
#region user-pass
var opts = new NatsOpts
NatsOpts opts = new NatsOpts
{
AuthOpts = NatsAuthOpts.Default with
{
Expand All @@ -24,27 +24,27 @@ public async Task Run()
},
};

await using var nats = new NatsClient(opts);
await using NatsClient nats = new NatsClient(opts);
#endregion
}

{
#region tls-implicit
var opts = new NatsOpts
NatsOpts opts = new NatsOpts
{
TlsOpts = new NatsTlsOpts
{
Mode = TlsMode.Implicit,
},
};

await using var nats = new NatsClient(opts);
await using NatsClient nats = new NatsClient(opts);
#endregion
}

{
#region tls-mutual
var opts = new NatsOpts
NatsOpts opts = new NatsOpts
{
TlsOpts = new NatsTlsOpts
{
Expand All @@ -54,7 +54,7 @@ public async Task Run()
},
};

await using var nats = new NatsClient(opts);
await using NatsClient nats = new NatsClient(opts);
#endregion
}
}
Expand Down
54 changes: 27 additions & 27 deletions tests/NATS.Net.DocsExamples/Advanced/SerializationPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public async Task Run()

#region default
// Set your custom serializer registry as the default for the connection.
var opts = NatsOpts.Default with { SerializerRegistry = new MyProtoBufSerializerRegistry() };
NatsOpts opts = NatsOpts.Default with { SerializerRegistry = new MyProtoBufSerializerRegistry() };

await using var nc = new NatsClient(opts);
await using NatsClient nc = new NatsClient(opts);
#endregion
}

Expand All @@ -42,15 +42,15 @@ public async Task Run()

#region my-data-usage
// Set the custom serializer registry as the default for the connection.
var myRegistry = new NatsJsonContextSerializerRegistry(MyJsonContext.Default, OtherJsonContext.Default);
NatsJsonContextSerializerRegistry myRegistry = new NatsJsonContextSerializerRegistry(MyJsonContext.Default, OtherJsonContext.Default);

var opts = new NatsOpts { SerializerRegistry = myRegistry };
NatsOpts opts = new NatsOpts { SerializerRegistry = myRegistry };

await using var nc = new NatsClient(opts);
await using NatsClient nc = new NatsClient(opts);

var subscriber = Task.Run(async () =>
Task subscriber = Task.Run(async () =>
{
await foreach (var msg in nc.SubscribeAsync<MyData>("foo"))
await foreach (NatsMsg<MyData> msg in nc.SubscribeAsync<MyData>("foo"))
{
// Outputs 'MyData { Id = 1, Name = bar }'
Console.WriteLine(msg.Data);
Expand All @@ -71,13 +71,13 @@ public async Task Run()
Console.WriteLine(" #region my-data-publish");

#region my-data-publish
await using var nc = new NatsClient();
await using NatsClient nc = new NatsClient();

var serializer = new NatsJsonContextSerializer<MyData>(MyJsonContext.Default);
NatsJsonContextSerializer<MyData> serializer = new NatsJsonContextSerializer<MyData>(MyJsonContext.Default);

var subscriber = Task.Run(async () =>
Task subscriber = Task.Run(async () =>
{
await foreach (var msg in nc.SubscribeAsync<MyData>("foo", serializer: serializer))
await foreach (NatsMsg<MyData> msg in nc.SubscribeAsync<MyData>("foo", serializer: serializer))
{
// Outputs 'MyData { Id = 1, Name = bar }'
Console.WriteLine(msg.Data);
Expand All @@ -98,13 +98,13 @@ public async Task Run()
Console.WriteLine(" #region custom");

#region custom
var opts = new NatsOpts { SerializerRegistry = new MyProtoBufSerializerRegistry() };
NatsOpts opts = new NatsOpts { SerializerRegistry = new MyProtoBufSerializerRegistry() };

await using var nc = new NatsClient(opts);
await using NatsClient nc = new NatsClient(opts);

var subscriber = Task.Run(async () =>
Task subscriber = Task.Run(async () =>
{
await foreach (var msg in nc.SubscribeAsync<Greeting>("foo"))
await foreach (NatsMsg<Greeting> msg in nc.SubscribeAsync<Greeting>("foo"))
{
// Outputs '{ "id": 42, "name": "Marvin" }'
Console.WriteLine(msg.Data);
Expand All @@ -125,23 +125,23 @@ public async Task Run()
Console.WriteLine(" #region chain");

#region chain
var opts = new NatsOpts { SerializerRegistry = new MixedSerializerRegistry() };
NatsOpts opts = new NatsOpts { SerializerRegistry = new MixedSerializerRegistry() };

await using var nc = new NatsClient(opts);
await using NatsClient nc = new NatsClient(opts);

var subscriber1 = Task.Run(async () =>
Task subscriber1 = Task.Run(async () =>
{
await foreach (var msg in nc.SubscribeAsync<Greeting>("greet"))
await foreach (NatsMsg<Greeting> msg in nc.SubscribeAsync<Greeting>("greet"))
{
// Outputs '{ "id": 42, "name": "Marvin" }'
Console.WriteLine(msg.Data);
break;
}
});

var subscriber2 = Task.Run(async () =>
Task subscriber2 = Task.Run(async () =>
{
await foreach (var msg in nc.SubscribeAsync<MyData>("data"))
await foreach (NatsMsg<MyData> msg in nc.SubscribeAsync<MyData>("data"))
{
// Outputs 'MyData { Id = 1, Name = bar }'
Console.WriteLine(msg.Data);
Expand All @@ -166,19 +166,19 @@ public async Task Run()
#region buffers
// The default serializer knows how to deal with binary data types like NatsMemoryOwner<byte>.
// So, you can use it without specifying a serializer.
await using var nc = new NatsClient();
await using NatsClient nc = new NatsClient();

var subscriber = Task.Run(async () =>
Task subscriber = Task.Run(async () =>
{
// The default serializer knows how to deal with binary data types like NatsMemoryOwner<byte>.
await foreach (var msg in nc.SubscribeAsync<NatsMemoryOwner<byte>>("foo"))
await foreach (NatsMsg<NatsMemoryOwner<byte>> msg in nc.SubscribeAsync<NatsMemoryOwner<byte>>("foo"))
{
// Check for the end of messages.
if (msg.Data.Length == 0)
break;

// Dispose the memory owner after using it so it can be returned to the pool.
using var memoryOwner = msg.Data;
using NatsMemoryOwner<byte> memoryOwner = msg.Data;

// Outputs 'Hi'
Console.WriteLine(Encoding.ASCII.GetString(memoryOwner.Memory.Span));
Expand All @@ -190,8 +190,8 @@ public async Task Run()

// Don't reuse NatsBufferWriter, it's disposed and returned to the pool
// by the publisher after being written to the network.
var bw = new NatsBufferWriter<byte>();
var memory = bw.GetMemory(2);
NatsBufferWriter<byte> bw = new NatsBufferWriter<byte>();
Memory<byte> memory = bw.GetMemory(2);
memory.Span[0] = (byte)'H';
memory.Span[1] = (byte)'i';
bw.Advance(2);
Expand Down
12 changes: 5 additions & 7 deletions tests/NATS.Net.DocsExamples/Core/IntroPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#pragma warning disable SA1124
#pragma warning disable SA1509

using Microsoft.Extensions.Logging;
using NATS.Client.Core;
using NATS.Client.Serializers.Json;

namespace NATS.Net.DocsExamples.Core;

Expand All @@ -20,9 +18,9 @@ public async Task Run()
subscription = Task.Run(async () =>
{
#region sub
await using var nc = new NatsClient();
await using NatsClient nc = new NatsClient();

await foreach (var msg in nc.SubscribeAsync<Bar>("bar.>"))
await foreach (NatsMsg<Bar> msg in nc.SubscribeAsync<Bar>("bar.>"))
{
if (msg.Subject == "bar.exit")
break;
Expand All @@ -37,9 +35,9 @@ public async Task Run()

{
#region pub
await using var nc = new NatsClient();
await using NatsClient nc = new NatsClient();

for (var i = 0; i < 10; i++)
for (int i = 0; i < 10; i++)
{
Console.WriteLine($" Publishing {i}...");
await nc.PublishAsync<Bar>($"bar.baz.{i}", new Bar(Id: i, Name: "Baz"));
Expand All @@ -48,7 +46,7 @@ public async Task Run()
await nc.PublishAsync("bar.exit");
#endregion

for (var i = 0; i < 3; i++)
for (int i = 0; i < 3; i++)
{
await Task.Delay(250);
await nc.PublishAsync("bar.exit");
Expand Down
8 changes: 4 additions & 4 deletions tests/NATS.Net.DocsExamples/Core/PubSubPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public async Task Run()

{
#region pubsub
await using var nc = new NatsClient();
await using NatsClient nc = new NatsClient();

var subscription = Task.Run(async () =>
Task subscription = Task.Run(async () =>
{
await foreach (var msg in nc.SubscribeAsync<int>("foo"))
await foreach (NatsMsg<int> msg in nc.SubscribeAsync<int>("foo"))
{
Console.WriteLine($"Received {msg.Subject}: {msg.Data}\n");

Expand All @@ -32,7 +32,7 @@ public async Task Run()
// Give subscription time to start
await Task.Delay(1000);

for (var i = 0; i < 10; i++)
for (int i = 0; i < 10; i++)
{
Console.WriteLine($" Publishing {i}...");
await nc.PublishAsync<int>("foo", i);
Expand Down
14 changes: 7 additions & 7 deletions tests/NATS.Net.DocsExamples/Core/QueuePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ public async Task Run()
Console.WriteLine("NATS.Net.DocsExamples.Core.QueuePage");

#region queue
await using var nc = new NatsClient();
await using NatsClient nc = new NatsClient();

// Create a cancellation token source to stop the subscriptions
using var cts = new CancellationTokenSource();
using CancellationTokenSource cts = new CancellationTokenSource();

var replyTasks = new List<Task>();
List<Task> replyTasks = new List<Task>();

for (var i = 0; i < 3; i++)
for (int i = 0; i < 3; i++)
{
// Create three subscriptions all on the same queue group
// Create a background message loop for every subscription
var replyTaskId = i;
int replyTaskId = i;
replyTasks.Add(Task.Run(async () =>
{
// Retrieve messages until unsubscribed
await foreach (var msg in nc.SubscribeAsync<int>("math.double", queueGroup: "maths-service", cancellationToken: cts.Token))
await foreach (NatsMsg<int> msg in nc.SubscribeAsync<int>("math.double", queueGroup: "maths-service", cancellationToken: cts.Token))
{
Console.WriteLine($"[{replyTaskId}] Received request: {msg.Data}");
await msg.ReplyAsync($"Answer is: {2 * msg.Data}");
Expand All @@ -48,7 +48,7 @@ public async Task Run()
await Task.Delay(1000);

// Send a few requests
for (var i = 0; i < 10; i++)
for (int i = 0; i < 10; i++)
{
NatsMsg<string> reply = await nc.RequestAsync<int, string>("math.double", i);
Console.WriteLine($"Reply: '{reply.Data}'");
Expand Down
Loading