/
AcceptanceTest.cs
90 lines (76 loc) · 4.16 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
namespace ServiceControl.Audit.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using AcceptanceTesting;
using Microsoft.Extensions.Hosting;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTesting.Support;
using NUnit.Framework;
using ServiceControl.AcceptanceTesting.InfrastructureConfig;
using ServiceControl.Audit.Infrastructure.Settings;
using TestSupport;
[TestFixture]
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 HttpClient HttpClient => serviceControlRunnerBehavior.HttpClient;
public JsonSerializerOptions SerializerOptions => serviceControlRunnerBehavior.SerializerOptions;
protected IServiceProvider ServiceProvider => serviceControlRunnerBehavior.ServiceProvider;
[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), d => SetStorageConfiguration(d), hb => CustomizeHostBuilder(hb));
TestContext.WriteLine($"Using persistence {StorageConfiguration.PersistenceType}");
}
[TearDown]
public Task Teardown()
{
TransportIntegration = null;
Trace.Flush();
Trace.Close();
Trace.Listeners.Remove(textWriterTraceListener);
return StorageConfiguration.Cleanup();
}
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<IDictionary<string, string>> SetStorageConfiguration = _ => { };
protected Action<IHostApplicationBuilder> CustomizeHostBuilder = _ => { };
protected ITransportIntegration TransportIntegration;
protected AcceptanceTestStorageConfiguration StorageConfiguration;
ServiceControlComponentBehavior serviceControlRunnerBehavior;
TextWriterTraceListener textWriterTraceListener;
}
}