-
-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathBug_using_host_stop.cs
More file actions
109 lines (91 loc) · 3.35 KB
/
Bug_using_host_stop.cs
File metadata and controls
109 lines (91 loc) · 3.35 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
using Alba;
using JasperFx.CommandLine;
using JasperFx.Core.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Shouldly;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Wolverine.Runtime;
using Wolverine.Tracking;
namespace Wolverine.Http.Tests.Bugs;
public class Bug_using_host_stop
{
public enum HostType
{
WebApplicationBuilder,
AlbaHostWithWebApplicationBuilder,
AlbaHostWithFactory
}
private class HostTypeData : TheoryData<HostType>
{
public HostTypeData() => AddRange(Enum.GetValues<HostType>());
}
[Theory]
[ClassData(typeof(HostTypeData))]
public async Task wolverine_runtime_stops_when_host_is_stopped(HostType type)
{
using var host = await CreateHostAsync(type);
var wolverineRuntime = host.GetRuntime();
var checkPoints = new bool[2];
checkPoints[0] = IsRunning(wolverineRuntime);
await host.StopAsync();
checkPoints[1] = IsRunning(wolverineRuntime);
checkPoints.ShouldBe([true, false]);
}
[Theory]
[ClassData(typeof(HostTypeData))]
public async Task wolverine_runtime_stops_when_host_is_disposed(HostType type)
{
using var host = await CreateHostAsync(type);
var wolverineRuntime = host.GetRuntime();
var checkPoints = new bool[2];
checkPoints[0] = IsRunning(wolverineRuntime);
await host.As<IAsyncDisposable>().DisposeAsync();
checkPoints[1] = IsRunning(wolverineRuntime);
checkPoints.ShouldBe([true, false]);
}
static bool IsRunning(WolverineRuntime runtime)
{
var field = typeof(WolverineRuntime).GetField("_hasStopped",
BindingFlags.NonPublic | BindingFlags.Instance);
return (bool?)field?.GetValue(runtime) == false;
}
private static async Task<IHost> CreateHostAsync(HostType hostType) =>
hostType switch
{
HostType.WebApplicationBuilder =>
await CreateHostWithWebApplicationBuilder(),
HostType.AlbaHostWithWebApplicationBuilder =>
await AlbaHost.For(CreateWebApplicationBuilder(), _ => { }),
_ =>
await CreateAlbaHostWithWithFactory()
};
private static async Task<IHost> CreateAlbaHostWithWithFactory()
{
JasperFxEnvironment.AutoStartHost = true; // to start the underlying host
return await AlbaHost.For<WolverineWebApi.Program>(x =>
x.ConfigureServices(ConfigureWolverine));
}
private static async Task<IHost> CreateHostWithWebApplicationBuilder()
{
var builder = CreateWebApplicationBuilder();
var host = builder.Build();
await host.StartAsync();
return host;
}
private static WebApplicationBuilder CreateWebApplicationBuilder()
{
var builder = WebApplication.CreateBuilder([]);
ConfigureWolverine(builder.Services);
builder.Services.AddWolverine(_ => { });
return builder;
}
private static void ConfigureWolverine(IServiceCollection services)
{
services
.RunWolverineInSoloMode()
.DisableAllWolverineMessagePersistence()
.DisableAllExternalWolverineTransports();
}
}