PlanTempusApp/PlanTempus.Tests/Logging/SeqTelemetryChannelTest.cs

60 lines
2.1 KiB
C#
Raw Normal View History

using Autofac;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
2025-03-10 15:56:22 +01:00
using PlanTempus.Core.SeqLogging;
using PlanTempus.Core.Telemetry;
2025-03-07 00:17:08 +01:00
namespace PlanTempus.X.TDD.Logging;
[TestClass]
public class SeqTelemetryChannelTest : TestFixture
{
2025-03-07 00:17:08 +01:00
private CancellationTokenSource _cts;
private IMessageChannel<ITelemetry> _messageChannel;
private SeqBackgroundService _service;
private TelemetryClient _telemetryClient;
[TestInitialize]
public void SetupThis()
{
2025-03-07 00:17:08 +01:00
//it is important to use the same MessageChannel as the BackgroundService uses
//we know that IMessageChannel<ITelemetry> _messageChannel; is registered via Autofac and manually injected into SeqBackgroundService
//so we can get it by calling the Autofac Container in this test.
2025-03-07 00:17:08 +01:00
_messageChannel = Container.Resolve<IMessageChannel<ITelemetry>>();
_service = Container.Resolve<SeqBackgroundService>();
_telemetryClient = Container.Resolve<TelemetryClient>();
2025-03-07 00:17:08 +01:00
_cts = new CancellationTokenSource();
}
2025-03-07 00:17:08 +01:00
[TestMethod]
public async Task Messages_ShouldBeProcessedFromQueue()
{
await _service.StartAsync(_cts.Token);
2025-03-07 00:17:08 +01:00
for (var i = 0; i < 5; i++)
{
2025-03-07 00:17:08 +01:00
var eventTelemetry = new EventTelemetry
{
2025-03-07 00:17:08 +01:00
Name = "Test Event 3",
Timestamp = DateTimeOffset.UtcNow
};
2025-03-07 00:17:08 +01:00
eventTelemetry.Properties.Add("TestId", Guid.NewGuid().ToString());
eventTelemetry.Metrics.Add("TestMetric", 42.0);
2025-03-07 00:17:08 +01:00
//we don't write to the _messageChannel.Writer.WriteAsync(eventTelemetry);, but the TelemetryClient which is configured to use SeqTelemetryChannel
_telemetryClient.TrackEvent(eventTelemetry);
}
2025-03-07 00:17:08 +01:00
// wait for processing
await Task.Delay(5000);
2025-03-07 00:17:08 +01:00
await _service.StopAsync(CancellationToken.None);
2025-03-07 00:17:08 +01:00
var hasMoreMessages = await _messageChannel.Reader.WaitToReadAsync();
Assert.IsFalse(hasMoreMessages, "Queue should be empty after 5 seconds");
}
}