-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathIntegrationTestBase.cs
More file actions
73 lines (61 loc) · 1.86 KB
/
IntegrationTestBase.cs
File metadata and controls
73 lines (61 loc) · 1.86 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
using System.Net.Sockets;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace MaIN.Core.IntegrationTests;
public class IntegrationTestBase : IDisposable
{
protected readonly IHost _host;
protected readonly IServiceProvider _services;
public IntegrationTestBase()
{
_host = CreateHost();
_host.Start();
_services = _host.Services;
}
private IHost CreateHost()
{
var hostBuilder = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseUrls("http://localhost:0") // Random available port
.ConfigureServices((context, services) =>
{
services.AddMaIN(context.Configuration);
var provider = services.BuildServiceProvider();
provider.UseMaIN();
});
});
return hostBuilder.Build();
}
protected T GetService<T>() where T : notnull
{
return _services.GetRequiredService<T>();
}
protected static bool PingHost(string host, int port, int timeout)
{
try
{
using (var client = new TcpClient())
{
var result = client.BeginConnect(host, port, null, null);
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout));
if (!success)
{
return false;
}
client.EndConnect(result);
return true;
}
}
catch
{
return false;
}
}
public void Dispose()
{
_host?.Dispose();
}
}