Skip to content

Commit

Permalink
Use the new IHostedService model for integration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Dec 16, 2024
1 parent 896cf1b commit 647fcbc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Logzio.DotNet.NLog" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="Formitable.BetterStack.Logger.Microsoft" Version="0.1.2" />
<PackageReference Include="Microsoft.Extensions.Diagnostics" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
84 changes: 32 additions & 52 deletions Source/Picton.Messaging.IntegrationTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using Logzio.DotNet.NLog;
using Formitable.BetterStack.Logger.Microsoft;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Config;
using NLog.Extensions.Logging;
using NLog.Targets;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
Expand All @@ -17,7 +13,7 @@ namespace Picton.Messaging.IntegrationTests
{
class Program
{
public static async Task Main(string[] args)
public static async Task Main()
{
var source = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
Expand All @@ -29,22 +25,44 @@ public static async Task Main(string[] args)
var services = new ServiceCollection();
ConfigureServices(services);
using var serviceProvider = services.BuildServiceProvider();
var app = serviceProvider.GetService<TestsRunner>();
return await app.RunAsync(source.Token).ConfigureAwait(false);
var app = serviceProvider.GetService<IHostedService>();
await app.StartAsync(source.Token).ConfigureAwait(false);
}

builder.Services.AddPictonMessageHandlers();
builder.Services.AddHostedService<TestsRunner>();
private static void ConfigureServices(ServiceCollection services)
{
services.AddHostedService<TestsRunner>();
services.AddPictonMessageHandlers();
services.AddMetrics();

// Configure logging
builder.Logging.ClearProviders(); // Remove the built-in providers (which include the Console)
builder.Logging.AddNLog(GetNLogConfiguration()); // Add our desired custom providers (which include the Colored Console)
services
.AddLogging(logging =>
{
var betterStackToken = Environment.GetEnvironmentVariable("BETTERSTACK_TOKEN");
if (!string.IsNullOrEmpty(betterStackToken))
{
logging.AddBetterStackLogger(options =>
{
options.SourceToken = betterStackToken;
options.Context["source"] = "Picton_messaging_integration_tests";
options.Context["Picton-Version"] = typeof(CloudMessage).Assembly.GetName().Version.ToString(3);
});
}

logging.AddSimpleConsole(options =>
{
options.SingleLine = true;
options.TimestampFormat = "yyyy-MM-dd HH:mm:ss ";
});

logging.AddFilter("*", LogLevel.Debug);
});

// Configure metrics
var logzioMetricsToken = Environment.GetEnvironmentVariable("LOGZIO_METRICS_TOKEN");
if (!string.IsNullOrEmpty(logzioMetricsToken))
{
builder.Services.AddOpenTelemetry()
services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddMeter("Picton.Messaging");
Expand Down Expand Up @@ -83,44 +101,6 @@ public static async Task Main(string[] args)
});
});
}

var host = builder.Build();
await host.StartAsync(CancellationToken.None).ConfigureAwait(false);

// Stop NLog (which has the desirable side-effect of flushing any pending logs)
LogManager.Shutdown();
}

private static LoggingConfiguration GetNLogConfiguration()
{
// Configure logging
var nLogConfig = new LoggingConfiguration();

// Send logs to logz.io
var logzioToken = Environment.GetEnvironmentVariable("LOGZIO_TOKEN");
if (!string.IsNullOrEmpty(logzioToken))
{
var logzioTarget = new LogzioTarget
{
Name = "Logzio",
Token = logzioToken,
LogzioType = "nlog",
JsonKeysCamelCase = true,
// ProxyAddress = "http://localhost:8888",
};
logzioTarget.ContextProperties.Add(new TargetPropertyWithContext("Source", "PictonMessaging_integration_tests"));
logzioTarget.ContextProperties.Add(new TargetPropertyWithContext("PictonMessaging-Version", typeof(AsyncMessagePump).Assembly.GetName().Version.ToString(3)));

nLogConfig.AddTarget("Logzio", logzioTarget);
nLogConfig.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, "Logzio", "*");
}

// Send logs to console
var consoleTarget = new ColoredConsoleTarget();
nLogConfig.AddTarget("ColoredConsole", consoleTarget);
nLogConfig.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, "ColoredConsole", "*");

return nLogConfig;
}
}
}
25 changes: 10 additions & 15 deletions Source/Picton.Messaging.IntegrationTests/TestsRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,23 @@ public TestsRunner(ILogger<TestsRunner> logger, IServiceProvider serviceProvider

public async Task StartAsync(CancellationToken cancellationToken)
{
ServicePointManager.DefaultConnectionLimit = 1000;
ServicePointManager.UseNagleAlgorithm = false;

// Start Azurite before running the tests. It will be automaticaly stopped when "emulator" goes out of scope
using (var emulator = new AzuriteManager())
{
var connectionString = "UseDevelopmentStorage=true";
var queueName = "myqueue";
var concurrentTasks = 5;

// Run the integration tests
await RunAsyncMessagePumpTests(connectionString, queueName, concurrentTasks, 25, _meterFactory, cancellationToken).ConfigureAwait(false);
await RunAsyncMessagePumpWithHandlersTests(connectionString, queueName, concurrentTasks, 25, _meterFactory, cancellationToken).ConfigureAwait(false);
await RunMultiTenantAsyncMessagePumpTests(connectionString, queueName, concurrentTasks, [6, 12, 18, 24], _meterFactory, cancellationToken).ConfigureAwait(false);
}
using var emulator = new AzuriteManager();
var connectionString = "UseDevelopmentStorage=true";
var queueName = "myqueue";
var concurrentTasks = 5;

// Run the integration tests
await RunAsyncMessagePumpTests(connectionString, queueName, concurrentTasks, 25, _meterFactory, cancellationToken).ConfigureAwait(false);
await RunAsyncMessagePumpWithHandlersTests(connectionString, queueName, concurrentTasks, 25, _meterFactory, cancellationToken).ConfigureAwait(false);
await RunMultiTenantAsyncMessagePumpTests(connectionString, queueName, concurrentTasks, [6, 12, 18, 24], _meterFactory, cancellationToken).ConfigureAwait(false);
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

private async Task RunAsyncMessagePumpTests(string connectionString, string queueName, int concurrentTasks, int numberOfMessages, IMeterFactory meterFactory, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested) return;
Expand Down
4 changes: 2 additions & 2 deletions Source/Picton.Messaging/Picton.Messaging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="OpenTelemetry.Api" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Api" Version="1.10.0" />
<PackageReference Include="Picton" Version="9.3.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" PrivateAssets="All" />
</ItemGroup>
Expand All @@ -53,7 +53,7 @@

<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Threading.Channels" Version="8.*" />
<PackageReference Include="System.Threading.Channels" Version="9.*" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 647fcbc

Please sign in to comment.