-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfoMapHostedServiceTests.cs
More file actions
271 lines (229 loc) · 11.5 KB
/
Copy pathInfoMapHostedServiceTests.cs
File metadata and controls
271 lines (229 loc) · 11.5 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NSubstitute;
using RustMapsApi.Results;
using RustMapsApi.V4;
using RustMapsApi.V4.Models;
using RustPlusBot.Abstractions.Connections;
using RustPlusBot.Abstractions.Events;
using RustPlusBot.Domain.Connections;
using RustPlusBot.Features.Map.Hosting;
using RustPlusBot.Features.Map.RustMaps;
using RustPlusBot.Persistence.Connections;
namespace RustPlusBot.Features.Map.Tests.Hosting;
public sealed class InfoMapHostedServiceTests
{
private const ulong GuildA = 1UL;
private const ulong GuildB = 2UL;
private static readonly Guid ServerA = Guid.NewGuid();
private static readonly Guid ServerB = Guid.NewGuid();
private static readonly RustMapsMapKey Key = new(4000, 12345);
private static IServiceScopeFactory ScopeFactory(IConnectionStore store) =>
new ServiceCollection()
.AddScoped(_ => store)
.BuildServiceProvider()
.GetRequiredService<IServiceScopeFactory>();
private static IOptions<MapOptions> ShortPollOptions() => Options.Create(new MapOptions
{
RustMaps = new RustMapsOptions
{
GenerationPollInterval = TimeSpan.FromMilliseconds(10)
}
});
/// <summary>
/// A fake bus that records every published <see cref="InfoMapReadyEvent"/> and signals each arrival.
/// Deterministic, unlike subscribing to the real <see cref="InMemoryEventBus"/> from a background task:
/// that bus drops events published before the subscriber is active, which races the service's first tick.
/// </summary>
private static CapturingEventBus CapturingBus() => new();
[Fact]
public async Task Ready_key_publishes_InfoMapReadyEvent_once_per_requester()
{
var coordinator = new RustMapsMapCoordinator();
coordinator.Register(Key, GuildA, ServerA);
coordinator.Register(Key, GuildB, ServerB);
var client = Substitute.For<IRustMapsClient>();
client.GetMapBySeedAndSizeAsync(Key.Size, Key.Seed, false, Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Success(
new MapInfo
{
ImageUrl = "https://img/plain.png",
ImageIconUrl = "https://img/icons.png",
Url = "https://rustmaps/x"
}, 200));
var driver = new RustMapsGenerationDriver(client, coordinator, NullLogger<RustMapsGenerationDriver>.Instance);
var query = Substitute.For<IRustServerQuery>();
// Capture publishes off a substituted bus: the real InMemoryEventBus drops events published
// before a subscriber is active, so collecting via a background subscription races the
// service's first tick and flakes on slow CI runners.
using var bus = CapturingBus();
var received = bus.Received;
var service = new InfoMapHostedService(
bus, coordinator, driver, query, ShortPollOptions(),
ScopeFactory(Substitute.For<IConnectionStore>()), NullLogger<InfoMapHostedService>.Instance);
await service.StartAsync(CancellationToken.None);
try
{
await bus.WaitForAsync(2);
}
finally
{
await service.StopAsync(CancellationToken.None);
}
Assert.Equal(2, received.Count);
Assert.Contains(received, e => e.GuildId == GuildA && e.ServerId == ServerA);
Assert.Contains(received, e => e.GuildId == GuildB && e.ServerId == ServerB);
Assert.Equal(RustMapsGenerationState.Ready, coordinator.Snapshot(Key).State);
Assert.Equal("https://img/icons.png", coordinator.GetReady(Key.Size, Key.Seed)!.ImageUrl);
}
[Fact]
public async Task Tick_registers_and_generates_connected_servers_without_any_connect_event()
{
// The ConnectionStatusChangedEvent is live-only and can be missed on startup (the connection may
// publish it before this service subscribes). Registration must therefore come from the connection
// store each tick, so a connected server still generates even if the event was never seen.
var coordinator = new RustMapsMapCoordinator();
var serverId = Guid.NewGuid();
var client = Substitute.For<IRustMapsClient>();
client.GetMapBySeedAndSizeAsync(Key.Size, Key.Seed, false, Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Success(
new MapInfo
{
ImageUrl = "https://img/plain.png",
ImageIconUrl = "https://img/icons.png",
Url = "https://rustmaps/x"
}, 200));
var driver = new RustMapsGenerationDriver(client, coordinator, NullLogger<RustMapsGenerationDriver>.Instance);
var query = Substitute.For<IRustServerQuery>();
query.GetWorldAsync(GuildA, serverId, Arg.Any<CancellationToken>())
.Returns(new WorldSnapshot((uint)Key.Size, (uint)Key.Seed));
var store = Substitute.For<IConnectionStore>();
IReadOnlyList<(ulong GuildId, Guid ServerId)> connectable = [(GuildA, serverId)];
store.ListConnectableServersAsync(Arg.Any<CancellationToken>()).Returns(connectable);
using var bus = CapturingBus();
var received = bus.Received;
var service = new InfoMapHostedService(
bus, coordinator, driver, query, ShortPollOptions(),
ScopeFactory(store), NullLogger<InfoMapHostedService>.Instance);
await service.StartAsync(CancellationToken.None);
try
{
await bus.WaitForAsync(1);
}
finally
{
await service.StopAsync(CancellationToken.None);
}
// No ConnectionStatusChangedEvent was ever published — registration came from the store tick.
Assert.Contains(received, e => e.GuildId == GuildA && e.ServerId == serverId);
Assert.Equal(RustMapsGenerationState.Ready, coordinator.Snapshot(Key).State);
}
[Fact]
public async Task Connect_registers_the_servers_world_key_with_the_coordinator()
{
// No tick should be needed for registration — the connection-status path resolves the world and
// registers immediately, independent of the (here, effectively-never-firing) generation poll.
var coordinator = new RustMapsMapCoordinator();
var driver = new RustMapsGenerationDriver(
Substitute.For<IRustMapsClient>(), coordinator, NullLogger<RustMapsGenerationDriver>.Instance);
var serverId = Guid.NewGuid();
var query = Substitute.For<IRustServerQuery>();
query.GetWorldAsync(GuildA, serverId, Arg.Any<CancellationToken>())
.Returns(new WorldSnapshot((uint)Key.Size, (uint)Key.Seed));
var connectionStore = Substitute.For<IConnectionStore>();
connectionStore.GetStateAsync(GuildA, serverId, Arg.Any<CancellationToken>())
.Returns(new ConnectionState
{
GuildId = GuildA, RustServerId = serverId, Status = ConnectionStatus.Connected
});
var bus = new InMemoryEventBus();
var pollOptions = Options.Create(new MapOptions
{
RustMaps = new RustMapsOptions
{
GenerationPollInterval = TimeSpan.FromSeconds(30) // must not need to fire for this test to pass
}
});
// Signals the registration instead of polling for it: a poll loop needs the thread pool to take its
// next turn, so on a saturated CI runner the whole deadline can elapse inside one Task.Delay.
var registered = new SignallingCoordinator(coordinator);
var service = new InfoMapHostedService(
bus, registered, driver, query, pollOptions,
ScopeFactory(connectionStore), NullLogger<InfoMapHostedService>.Instance);
await service.StartAsync(CancellationToken.None);
try
{
// Published exactly once: StartAsync subscribes before it returns, so no event can be dropped.
await bus.PublishAsync(new ConnectionStatusChangedEvent(GuildA, serverId, true, false));
await registered.Registered.WaitAsync(TimeSpan.FromSeconds(30));
}
finally
{
await service.StopAsync(CancellationToken.None);
}
Assert.Contains(Key, coordinator.PendingKeys());
Assert.Contains((GuildA, serverId), coordinator.Requesters(Key));
}
/// <summary>Forwards to a real coordinator and completes <see cref="Registered"/> on the first Register.</summary>
/// <param name="inner">The coordinator that holds the real state.</param>
private sealed class SignallingCoordinator(IRustMapsMapCoordinator inner) : IRustMapsMapCoordinator
{
private readonly TaskCompletionSource _registered =
new(TaskCreationOptions.RunContinuationsAsynchronously);
public Task Registered => _registered.Task;
public void Register(RustMapsMapKey key, ulong guildId, Guid serverId)
{
inner.Register(key, guildId, serverId);
_registered.TrySetResult();
}
public RustMapsMapSnapshot Snapshot(RustMapsMapKey key) => inner.Snapshot(key);
public bool TrySetGenerating(RustMapsMapKey key, string? mapId) => inner.TrySetGenerating(key, mapId);
public void SetReady(RustMapsMapKey key, RustMapsReadyMap ready) => inner.SetReady(key, ready);
public void SetFailed(RustMapsMapKey key) => inner.SetFailed(key);
public void SetLimitReached(RustMapsMapKey key) => inner.SetLimitReached(key);
public IReadOnlyList<RustMapsMapKey> PendingKeys() => inner.PendingKeys();
public IReadOnlyList<(ulong Guild, Guid Server)> Requesters(RustMapsMapKey key) => inner.Requesters(key);
}
private sealed class CapturingEventBus : IEventBus, IDisposable
{
private readonly SemaphoreSlim _published = new(0);
public ConcurrentQueue<InfoMapReadyEvent> Received { get; } = new();
public void Dispose() => _published.Dispose();
public ValueTask PublishAsync<TEvent>(TEvent @event, CancellationToken cancellationToken = default)
where TEvent : notnull
{
if (@event is InfoMapReadyEvent ready)
{
Received.Enqueue(ready);
_published.Release();
}
return ValueTask.CompletedTask;
}
public IAsyncEnumerable<TEvent> SubscribeAsync<TEvent>(CancellationToken cancellationToken = default)
where TEvent : notnull =>
AsyncEnumerable.Empty<TEvent>(); // No live subscriptions needed: assertions read Received.
/// <summary>
/// Waits for <paramref name="count"/> events. Signal-based rather than a poll loop: polling needs the
/// thread pool to take its next turn, so on a saturated runner a whole wall-clock deadline can elapse
/// inside a single Task.Delay. A timeout here is not asserted on — the caller's assertions report it.
/// </summary>
/// <param name="count">How many events to wait for.</param>
public async Task WaitForAsync(int count)
{
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30));
try
{
for (var i = 0; i < count; i++)
{
await _published.WaitAsync(timeout.Token);
}
}
catch (OperationCanceledException)
{
// Timed out; the caller asserts on what actually arrived.
}
}
}
}