WIP
This commit is contained in:
parent
54b057886c
commit
7fc1ae0650
204 changed files with 4345 additions and 134 deletions
71
PlanTempus.Tests/Logging/SeqBackgroundServiceTest.cs
Normal file
71
PlanTempus.Tests/Logging/SeqBackgroundServiceTest.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
using System.Net;
|
||||
using Autofac;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using PlanTempus.Core.SeqLogging;
|
||||
using PlanTempus.Core.Telemetry;
|
||||
|
||||
namespace PlanTempus.X.TDD.Logging;
|
||||
|
||||
[TestClass]
|
||||
public class SeqBackgroundServiceTest : TestFixture
|
||||
{
|
||||
private CancellationTokenSource _cts;
|
||||
private IMessageChannel<ITelemetry> _messageChannel;
|
||||
private SeqBackgroundService _service;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetupThis()
|
||||
{
|
||||
_messageChannel = new MessageChannel();
|
||||
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
|
||||
var config = new SeqConfiguration("http://localhost:5341", null, "MSTEST");
|
||||
|
||||
var httpClient = new SeqHttpClient(config);
|
||||
var logger = new SeqLogger<SeqBackgroundService>(httpClient, config);
|
||||
|
||||
_service = new SeqBackgroundService(telemetryClient, _messageChannel, logger);
|
||||
_cts = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Messages_ShouldBeProcessedFromQueue()
|
||||
{
|
||||
await _service.StartAsync(_cts.Token);
|
||||
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
var eventTelemetry = new EventTelemetry
|
||||
{
|
||||
Name = "Test Event",
|
||||
Timestamp = DateTimeOffset.UtcNow
|
||||
};
|
||||
eventTelemetry.Properties.Add("TestId", Guid.NewGuid().ToString());
|
||||
eventTelemetry.Metrics.Add("TestMetric", 42.0);
|
||||
|
||||
await _messageChannel.Writer.WriteAsync(eventTelemetry);
|
||||
}
|
||||
|
||||
// wait for processing
|
||||
await Task.Delay(5000);
|
||||
|
||||
_cts.Cancel(); //not sure about this, we need to analyse more before this is "the way"
|
||||
await _service.StopAsync(CancellationToken.None);
|
||||
|
||||
|
||||
var hasMoreMessages = await _messageChannel.Reader.WaitToReadAsync();
|
||||
Assert.IsFalse(hasMoreMessages, "Queue should be empty after 5 seconds");
|
||||
}
|
||||
|
||||
private class TestMessageHandler : HttpMessageHandler
|
||||
{
|
||||
protected override Task<HttpResponseMessage> SendAsync(
|
||||
HttpRequestMessage request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
|
||||
}
|
||||
}
|
||||
}
|
||||
145
PlanTempus.Tests/Logging/SeqLoggerTests.cs
Normal file
145
PlanTempus.Tests/Logging/SeqLoggerTests.cs
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
using Autofac;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using PlanTempus.Core.SeqLogging;
|
||||
|
||||
namespace PlanTempus.X.TDD.Logging;
|
||||
|
||||
[TestClass]
|
||||
public class SeqLoggerTests : TestFixture
|
||||
{
|
||||
private readonly string _testId;
|
||||
private readonly SeqHttpClient _httpClient;
|
||||
private readonly SeqLogger<SeqLoggerTests> _logger;
|
||||
|
||||
public SeqLoggerTests()
|
||||
{
|
||||
_testId = Guid.NewGuid().ToString();
|
||||
var config = new SeqConfiguration("http://localhost:5341", null, "MSTEST");
|
||||
_httpClient = new SeqHttpClient(config);
|
||||
_logger = new SeqLogger<SeqLoggerTests>(_httpClient, config);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task LogTraceTelemetry_SendsCorrectDataWithErrorLevel()
|
||||
{
|
||||
// Arrange
|
||||
var traceTelemetry = new TraceTelemetry
|
||||
{
|
||||
Message = "Test trace error message",
|
||||
SeverityLevel = SeverityLevel.Error,
|
||||
Timestamp = DateTimeOffset.UtcNow
|
||||
};
|
||||
traceTelemetry.Properties.Add("TestId", _testId);
|
||||
|
||||
// Act
|
||||
await _logger.LogAsync(traceTelemetry);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task LogTraceTelemetry_SendsCorrectDataWithWarningLevel()
|
||||
{
|
||||
// Arrange
|
||||
var traceTelemetry = new TraceTelemetry
|
||||
{
|
||||
Message = "Test trace warning message",
|
||||
SeverityLevel = SeverityLevel.Warning,
|
||||
Timestamp = DateTimeOffset.UtcNow
|
||||
};
|
||||
traceTelemetry.Properties.Add("TestId", _testId);
|
||||
|
||||
// Act
|
||||
await _logger.LogAsync(traceTelemetry);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task LogEventTelemetry_SendsCorrectData()
|
||||
{
|
||||
// Arrange
|
||||
var eventTelemetry = new EventTelemetry
|
||||
{
|
||||
Name = "Test Event",
|
||||
Timestamp = DateTimeOffset.UtcNow
|
||||
};
|
||||
eventTelemetry.Properties.Add("TestId", _testId);
|
||||
eventTelemetry.Metrics.Add("TestMetric", 42.0);
|
||||
|
||||
// Act
|
||||
await _logger.LogAsync(eventTelemetry);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task LogExceptionTelemetry_SendsCorrectData()
|
||||
{
|
||||
try
|
||||
{
|
||||
var t = 0;
|
||||
var result = 10 / t;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Arrange
|
||||
var exceptionTelemetry = new ExceptionTelemetry(e)
|
||||
{
|
||||
Timestamp = DateTimeOffset.UtcNow
|
||||
};
|
||||
exceptionTelemetry.Properties.Add("TestId", _testId);
|
||||
|
||||
// Act
|
||||
await _logger.LogAsync(exceptionTelemetry);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task LogDependencyTelemetry_SendsCorrectData()
|
||||
{
|
||||
// Arrange
|
||||
var dependencyTelemetry = new DependencyTelemetry
|
||||
{
|
||||
Name = "SQL Query",
|
||||
Type = "SQL",
|
||||
Target = "TestDB",
|
||||
Success = true,
|
||||
Duration = TimeSpan.FromMilliseconds(100),
|
||||
Timestamp = DateTimeOffset.UtcNow
|
||||
};
|
||||
dependencyTelemetry.Properties.Add("TestId", _testId);
|
||||
|
||||
// Act
|
||||
await _logger.LogAsync(dependencyTelemetry);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is for scope test in SeqLogger. It is not testing anything related to the TelemetryChannel which logs to Seq.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[TestMethod]
|
||||
public async Task LogRequestTelemetryInOperationHolderWithParentChild_SendsCorrectData()
|
||||
{
|
||||
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
|
||||
using (var parent = telemetryClient.StartOperation<RequestTelemetry>("Parent First"))
|
||||
{
|
||||
parent.Telemetry.Duration = TimeSpan.FromMilliseconds(250);
|
||||
parent.Telemetry.Url = new Uri("http://parent.test.com/api/test");
|
||||
|
||||
using (var child = telemetryClient.StartOperation<RequestTelemetry>("Child 1"))
|
||||
{
|
||||
child.Telemetry.Success = true;
|
||||
child.Telemetry.ResponseCode = "200";
|
||||
child.Telemetry.Duration = TimeSpan.FromMilliseconds(50);
|
||||
child.Telemetry.Url = new Uri("http://child.test.com/api/test");
|
||||
child.Telemetry.Timestamp = DateTimeOffset.UtcNow;
|
||||
|
||||
child.Telemetry.Properties.Add("httpMethod", HttpMethod.Get.ToString());
|
||||
child.Telemetry.Properties.Add("TestId", _testId);
|
||||
|
||||
await _logger.LogAsync(child);
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
await _logger.LogAsync(parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
60
PlanTempus.Tests/Logging/SeqTelemetryChannelTest.cs
Normal file
60
PlanTempus.Tests/Logging/SeqTelemetryChannelTest.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using Autofac;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using PlanTempus.Core.SeqLogging;
|
||||
using PlanTempus.Core.Telemetry;
|
||||
|
||||
namespace PlanTempus.X.TDD.Logging;
|
||||
|
||||
[TestClass]
|
||||
public class SeqTelemetryChannelTest : TestFixture
|
||||
{
|
||||
private CancellationTokenSource _cts;
|
||||
private IMessageChannel<ITelemetry> _messageChannel;
|
||||
private SeqBackgroundService _service;
|
||||
private TelemetryClient _telemetryClient;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetupThis()
|
||||
{
|
||||
//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.
|
||||
|
||||
_messageChannel = Container.Resolve<IMessageChannel<ITelemetry>>();
|
||||
_service = Container.Resolve<SeqBackgroundService>();
|
||||
_telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
|
||||
_cts = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Messages_ShouldBeProcessedFromQueue()
|
||||
{
|
||||
await _service.StartAsync(_cts.Token);
|
||||
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
var eventTelemetry = new EventTelemetry
|
||||
{
|
||||
Name = "Test Event 3",
|
||||
Timestamp = DateTimeOffset.UtcNow
|
||||
};
|
||||
|
||||
eventTelemetry.Properties.Add("TestId", Guid.NewGuid().ToString());
|
||||
eventTelemetry.Metrics.Add("TestMetric", 42.0);
|
||||
|
||||
//we don't write to the _messageChannel.Writer.WriteAsync(eventTelemetry);, but the TelemetryClient which is configured to use SeqTelemetryChannel
|
||||
_telemetryClient.TrackEvent(eventTelemetry);
|
||||
}
|
||||
|
||||
// wait for processing
|
||||
await Task.Delay(5000);
|
||||
|
||||
await _service.StopAsync(CancellationToken.None);
|
||||
|
||||
var hasMoreMessages = await _messageChannel.Reader.WaitToReadAsync();
|
||||
Assert.IsFalse(hasMoreMessages, "Queue should be empty after 5 seconds");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue