-
Notifications
You must be signed in to change notification settings - Fork 46
/
AcceptanceTest.cs
117 lines (100 loc) · 4.88 KB
/
AcceptanceTest.cs
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
namespace ServiceControl.AcceptanceTests
{
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using AcceptanceTesting;
using Infrastructure.DomainEvents;
using Microsoft.Extensions.Hosting;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTesting.Support;
using NUnit.Framework;
using ServiceBus.Management.Infrastructure.Settings;
using ServiceControl.AcceptanceTesting.InfrastructureConfig;
using TestSupport;
[TestFixture]
//[Parallelizable(ParallelScope.All)]
abstract class AcceptanceTest : NServiceBusAcceptanceTest, IAcceptanceTestInfrastructureProvider
{
protected AcceptanceTest()
{
ServicePointManager.DefaultConnectionLimit = int.MaxValue;
ServicePointManager.MaxServicePoints = int.MaxValue;
ServicePointManager.UseNagleAlgorithm = false; // Improvement for small tcp packets traffic, get buffered up to 1/2-second. If your storage communication is for small (less than ~1400 byte) payloads, this setting should help (especially when dealing with things like Azure Queues, which tend to have very small messages).
ServicePointManager.Expect100Continue = false; // This ensures tcp ports are free up quicker by the OS, prevents starvation of ports
ServicePointManager.SetTcpKeepAlive(true, 5000, 1000); // This is good for Azure because it reuses connections
}
public IDomainEvents DomainEvents => serviceControlRunnerBehavior.DomainEvents;
public HttpClient HttpClient => serviceControlRunnerBehavior.HttpClient;
public JsonSerializerOptions SerializerOptions => serviceControlRunnerBehavior.SerializerOptions;
public Settings Settings => serviceControlRunnerBehavior.Settings;
public Func<HttpMessageHandler> HttpMessageHandlerFactory => serviceControlRunnerBehavior.HttpMessageHandlerFactory;
[SetUp]
public void Setup()
{
SetSettings = _ => { };
CustomConfiguration = _ => { };
CustomizeHostBuilder = _ => { };
var logfilesPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "logs");
Directory.CreateDirectory(logfilesPath);
var logFile = Path.Combine(logfilesPath, $"{TestContext.CurrentContext.Test.ID}-{TestContext.CurrentContext.Test.Name}.txt");
if (File.Exists(logFile))
{
File.Delete(logFile);
}
textWriterTraceListener = new TextWriterTraceListener(logFile);
Trace.Listeners.Add(textWriterTraceListener);
TransportIntegration = new ConfigureEndpointLearningTransport();
StorageConfiguration = new AcceptanceTestStorageConfiguration();
serviceControlRunnerBehavior = new ServiceControlComponentBehavior(
TransportIntegration,
StorageConfiguration,
s =>
{
SetSettings(s);
},
s => CustomConfiguration(s),
hb => CustomizeHostBuilder(hb)
);
}
[TearDown]
public async Task Teardown()
{
TransportIntegration = null;
Trace.Flush();
Trace.Close();
Trace.Listeners.Remove(textWriterTraceListener);
await StorageConfiguration.Cleanup();
}
#pragma warning disable IDE0060 // Remove unused parameter
protected void ExecuteWhen(Func<bool> execute, Func<IDomainEvents, Task> action, string instanceName = Settings.DEFAULT_SERVICE_NAME)
#pragma warning restore IDE0060 // Remove unused parameter
{
var timeout = TimeSpan.FromSeconds(1);
_ = Task.Run(async () =>
{
while (!SpinWait.SpinUntil(execute, timeout))
{
}
await action(DomainEvents);
});
}
protected IScenarioWithEndpointBehavior<T> Define<T>() where T : ScenarioContext, new() => Define<T>(c => { });
protected IScenarioWithEndpointBehavior<T> Define<T>(Action<T> contextInitializer) where T : ScenarioContext, new() =>
Scenario.Define(contextInitializer)
.WithComponent(serviceControlRunnerBehavior);
protected Action<EndpointConfiguration> CustomConfiguration = _ => { };
protected Action<Settings> SetSettings = _ => { };
protected Action<IHostApplicationBuilder> CustomizeHostBuilder = _ => { };
protected ITransportIntegration TransportIntegration;
protected AcceptanceTestStorageConfiguration StorageConfiguration;
ServiceControlComponentBehavior serviceControlRunnerBehavior;
TextWriterTraceListener textWriterTraceListener;
}
}