More work on SeqBackgroundService, next step is tests for it.
This commit is contained in:
parent
a139b1ad08
commit
67207cf90b
27 changed files with 237 additions and 190 deletions
|
|
@ -1,70 +0,0 @@
|
|||
using Autofac;
|
||||
using System.Data;
|
||||
using Insight.Database;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Core.Telemetry;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class MessageChannelIntegrationTests : TestFixture
|
||||
{
|
||||
private IMessageChannel<ITelemetry> _messageChannel;
|
||||
private SeqBackgroundService _service;
|
||||
private CancellationTokenSource _cts;
|
||||
|
||||
[TestInitialize]
|
||||
public void SetupThis()
|
||||
{
|
||||
_messageChannel = new MessageChannel();
|
||||
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
var httpClient = new HttpClient(new TestMessageHandler());
|
||||
_service = new SeqBackgroundService(telemetryClient, _messageChannel, httpClient);
|
||||
_cts = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Messages_ShouldBeProcessedFromQueue()
|
||||
{
|
||||
var processedMessages = new List<HttpRequestMessage>();
|
||||
|
||||
var serviceTask = _service.StartAsync(_cts.Token);
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
var eventTelemetry = new EventTelemetry("SomeEvent");
|
||||
await _messageChannel.Writer.WriteAsync(eventTelemetry);
|
||||
}
|
||||
|
||||
// wait for processing
|
||||
await Task.Delay(5000);
|
||||
|
||||
_cts.Cancel();
|
||||
await _service.StopAsync(CancellationToken.None);
|
||||
|
||||
|
||||
bool 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(System.Net.HttpStatusCode.OK));
|
||||
}
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue