-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileSystemQueueProcessorTests.cs
More file actions
157 lines (135 loc) · 4.83 KB
/
FileSystemQueueProcessorTests.cs
File metadata and controls
157 lines (135 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using CloudNimble.Breakdance.Assemblies;
using CloudNimble.Breakdance.Extensions.MSTest2;
using CloudNimble.SimpleMessageBus.Core;
using CloudNimble.SimpleMessageBus.Publish;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using SimpleMessageBus.Tests.Shared;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SimpleMessageBus.Tests.Dispatch
{
/// <summary>
///
/// </summary>
[TestClass]
[DoNotParallelize]
public class FileSystemQueueProcessorTests : BreakdanceMSTestBase
{
public static int MessageCount = 0;
private const string filePath = @"D:\Scratch\SimpleMessageBus\";
[TestInitialize]
public async Task TestInitAsync()
{
TestHostBuilder
.UseEnvironment("Development")
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<IMessageHandler, TestMessageHandler>();
})
.UseFileSystemMessagePublisher(options =>
{
options.RootFolder = filePath;
})
.UseFileSystemQueueProcessor(options =>
{
options.RootFolder = filePath;
options.VirusScanDelayInSeconds = 5;
})
.UseOrderedMessageDispatcher()
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
b.AddConsole();
})
.UseConsoleLifetime();
TestSetup();
_ = TestHost.RunAsync(TestContext.CancellationToken);
}
[TestCleanup]
public async Task DeleteQueueFiles()
{
var options = TestHost.Services.GetRequiredService<IOptions<FileSystemOptions>>().Value;
foreach (var file in Directory.GetFiles(options.QueueFolderPath))
{
File.Delete(file);
}
TestTearDown();
}
/// <summary>
///
/// </summary>
[TestMethod]
public async Task RegularMessagePublisherWorks()
{
var publisher = TestHost.Services.GetRequiredService<IMessagePublisher>();
await publisher.PublishAsync(new TestMessage());
Thread.Sleep(3000);
MessageCount.Should().Be(0);
Thread.Sleep(3000);
MessageCount.Should().Be(1);
MessageCount = 0;
}
/// <summary>
///
/// </summary>
[TestMethod]
public async Task SimulatedNetworkMessagePublisherWorks()
{
var time = DateTime.Now.ToString("HHmmss");
var envelope = new MessageEnvelope(new TestMessage())
{
Id = Guid.NewGuid(),
DatePublished = DateTimeOffset.UtcNow
};
var options = TestHost.Services.GetRequiredService<IOptions<FileSystemOptions>>().Value;
File.WriteAllText(Path.Combine(options.QueueFolderPath, $"{time}.tmpmsg"), JsonConvert.SerializeObject(envelope));
Thread.Sleep(200);
File.Move(Path.Combine(options.QueueFolderPath, $"{time}.tmpmsg"), Path.Combine(options.QueueFolderPath, $"{time}.json"));
Thread.Sleep(3000);
MessageCount.Should().Be(0);
Thread.Sleep(3000);
MessageCount.Should().Be(1);
MessageCount = 0;
await Task.FromResult(0);
}
private class TestMessageHandler : IMessageHandler
{
/// <summary>
///
/// </summary>
/// <returns></returns>
public IEnumerable<Type> GetHandledMessageTypes()
{
Console.WriteLine("TestMessageHandler Loaded.");
yield return typeof(TestMessage);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
/// <returns></returns>
public Task OnErrorAsync(IMessage message, Exception exception) => throw new NotImplementedException();
/// <summary>
///
/// </summary>
/// <param name="messageEnvelope"></param>
/// <returns></returns>
public async Task OnNextAsync(MessageEnvelope messageEnvelope)
{
MessageCount = 1;
Console.WriteLine("MessageCount Incremented.");
await Task.FromResult(true);
}
}
}
}