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

[WIP] Testing failground 2 #203

Closed
wants to merge 6 commits into from
Closed
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
30 changes: 30 additions & 0 deletions tests/NATS.Client.Testing.Failground/CmdArgs.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using Microsoft.Extensions.Logging;

namespace NATS.Client.Testing.Failground;
Expand All @@ -12,6 +13,8 @@ public record CmdArgs

public string? Server { get; set; }

public bool Help { get; set; }

public bool Trace { get; set; }

public bool Debug { get; set; }
Expand Down Expand Up @@ -85,9 +88,36 @@ public static CmdArgs Parse(string[] args)
case "trace":
cmd.Trace = true;
break;
case "help":
cmd.Help = true;
break;
default:
throw new Exception($"Option not found: {key}");
}
}

if (args.Length == 0 || cmd.Help)
{
cmd.Error = $"""
Usage: {Process.GetCurrentProcess().ProcessName} [options]

--server <url> NATS server URL(s)
--max-retry <ms> Maximum time for retries in milliseconds
--id <id> Test ID
--workload <name> Test workload
--params <params> Test workload parameters (JSON file)
--debug Enable debug logging
--trace Enable trace logging
--help Show this help

Examples:

{Process.GetCurrentProcess().ProcessName} --workload stay-connected --id test123
{Process.GetCurrentProcess().ProcessName} --workload stay-connected --debug

""";
}

return cmd;
}
catch (Exception e)
Expand Down
13 changes: 6 additions & 7 deletions tests/NATS.Client.Testing.Failground/ConsumeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;
using NATS.Client.Core;
using NATS.Client.JetStream;
Expand All @@ -21,12 +21,12 @@ public ConsumeTest(ILoggerFactory loggerFactory)

public async Task Run(string runId, CmdArgs args, CancellationToken cancellationToken = default)
{
var natsOpts = NatsOpts.Default with
var natsOpts = NatsOpts.Default with { LoggerFactory = _loggerFactory };

if (args.Server != null)
{
Url = "nats://192.168.0.183:4222",
// Url = "nats://127.0.0.1:4222",
LoggerFactory = _loggerFactory,
};
natsOpts = natsOpts with { Url = args.Server };
}

await using var nats = new NatsConnection(natsOpts);

Expand Down Expand Up @@ -66,7 +66,6 @@ public async Task Run(string runId, CmdArgs args, CancellationToken cancellation
var ack = await js.PublishAsync(
subject: "s1.x",
data: $"data_[{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fff}]_{i:D5}",
opts: new NatsJSPubOpts { MsgId = $"{i:D5}" },
cancellationToken: cts.Token);
ack.EnsureSuccess();

Expand Down
39 changes: 38 additions & 1 deletion tests/NATS.Client.Testing.Failground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using NATS.Client.Core;
using NATS.Client.Testing.Failground;

try
Expand Down Expand Up @@ -60,6 +61,42 @@

try
{
logger.LogDebug("Starting health checks ({Id})...", cmdArgs.Id);

_ = Task.Run(async () =>
{
var natsOpts = NatsOpts.Default;

if (cmdArgs.Server != null)
{
natsOpts = natsOpts with { Url = cmdArgs.Server };
}

await using var nats = new NatsConnection(natsOpts);

var subject = $"healthcheck.{cmdArgs.Id}";

logger.LogTrace("Subscribing to {Subject}...", subject);

await foreach (var msg in nats.SubscribeAsync<NatsMemoryOwner<byte>>(subject))
{
if (msg.Data.Length > 0)
{
var buffer = NatsMemoryOwner<byte>.Allocate(msg.Data.Length);

using (var memoryOwner = msg.Data)
memoryOwner.Memory.CopyTo(buffer.Memory);

logger.LogDebug("Sending health check response...");
await msg.ReplyAsync(buffer);
}
else
{
logger.LogError("Health check failed: no data received");
}
}
});

logger.LogInformation("Starting test {Name} ({RunId})...", test.GetType().Name, runId);

await test.Run(runId, cmdArgs, cts.Token);
Expand Down
3 changes: 0 additions & 3 deletions tests/NATS.Client.Testing.Failground/StayConnectedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,12 @@ public async Task Run(string runId, CmdArgs args, CancellationToken cancellation
if (nats.ConnectionState == NatsConnectionState.Open)
continue;

_logger.LogWarning("Disconnected. Reconnecting...");

var stopwatch = Stopwatch.StartNew();
var connected = false;
while (!cancellationToken.IsCancellationRequested && stopwatch.ElapsedMilliseconds < maxRetry)
{
if (nats.ConnectionState == NatsConnectionState.Open)
{
_logger.LogInformation("Connected!");
connected = true;
break;
}
Expand Down