Skip to content

Commit 1149f5d

Browse files
committed
Add test coverage
1 parent aa3d55a commit 1149f5d

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Dapper;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace osu.Server.QueueProcessor.Tests
12+
{
13+
public class BeatmapStatusWatcherTests
14+
{
15+
private readonly ITestOutputHelper output;
16+
17+
public BeatmapStatusWatcherTests(ITestOutputHelper output)
18+
{
19+
this.output = output;
20+
}
21+
22+
/// <summary>
23+
/// Checking that processing an empty queue works as expected.
24+
/// </summary>
25+
[Fact]
26+
public async Task TestBasic()
27+
{
28+
var cts = new CancellationTokenSource(10000);
29+
30+
TaskCompletionSource<BeatmapUpdates> tcs = new TaskCompletionSource<BeatmapUpdates>();
31+
using var db = await DatabaseAccess.GetConnectionAsync(cts.Token);
32+
33+
// just a safety measure for now to ensure we don't hit production. since i was running on production until now.
34+
// will throw if not on test database.
35+
if (db.QueryFirstOrDefault<int?>("SELECT `count` FROM `osu_counts` WHERE `name` = 'is_production'") != null)
36+
throw new InvalidOperationException("You are trying to do something very silly.");
37+
38+
await db.ExecuteAsync("TRUNCATE TABLE `bss_process_queue`");
39+
40+
using var poller = await BeatmapStatusWatcher.StartPollingAsync(updates => { tcs.SetResult(updates); }, pollMilliseconds: 100);
41+
42+
await db.ExecuteAsync("INSERT INTO `bss_process_queue` (beatmapset_id) VALUES (1)");
43+
44+
var updates = await tcs.Task.WaitAsync(cts.Token);
45+
46+
Assert.Equal(new[] { 1 }, updates.BeatmapSetIDs);
47+
Assert.Equal(1, updates.LastProcessedQueueID);
48+
49+
tcs = new TaskCompletionSource<BeatmapUpdates>();
50+
51+
await db.ExecuteAsync("INSERT INTO `bss_process_queue` (beatmapset_id) VALUES (2), (3)");
52+
53+
updates = await tcs.Task.WaitAsync(cts.Token);
54+
55+
Assert.Equal(new[] { 2, 3 }, updates.BeatmapSetIDs);
56+
Assert.Equal(3, updates.LastProcessedQueueID);
57+
}
58+
59+
/// <summary>
60+
/// Checking that processing an empty queue works as expected.
61+
/// </summary>
62+
[Fact]
63+
public async Task TestLimit()
64+
{
65+
var cts = new CancellationTokenSource(10000);
66+
67+
TaskCompletionSource<BeatmapUpdates> tcs = new TaskCompletionSource<BeatmapUpdates>();
68+
using var db = await DatabaseAccess.GetConnectionAsync(cts.Token);
69+
70+
// just a safety measure for now to ensure we don't hit production. since i was running on production until now.
71+
// will throw if not on test database.
72+
if (db.QueryFirstOrDefault<int?>("SELECT `count` FROM `osu_counts` WHERE `name` = 'is_production'") != null)
73+
throw new InvalidOperationException("You are trying to do something very silly.");
74+
75+
await db.ExecuteAsync("TRUNCATE TABLE `bss_process_queue`");
76+
77+
using var poller = await BeatmapStatusWatcher.StartPollingAsync(updates => { tcs.SetResult(updates); }, limit: 1, pollMilliseconds: 100);
78+
79+
await db.ExecuteAsync("INSERT INTO `bss_process_queue` (beatmapset_id) VALUES (1)");
80+
81+
var updates = await tcs.Task.WaitAsync(cts.Token);
82+
tcs = new TaskCompletionSource<BeatmapUpdates>();
83+
84+
Assert.Equal(new[] { 1 }, updates.BeatmapSetIDs);
85+
Assert.Equal(1, updates.LastProcessedQueueID);
86+
87+
await db.ExecuteAsync("INSERT INTO `bss_process_queue` (beatmapset_id) VALUES (2), (3)");
88+
89+
updates = await tcs.Task.WaitAsync(cts.Token);
90+
tcs = new TaskCompletionSource<BeatmapUpdates>();
91+
92+
Assert.Equal(new[] { 2 }, updates.BeatmapSetIDs);
93+
Assert.Equal(2, updates.LastProcessedQueueID);
94+
95+
updates = await tcs.Task.WaitAsync(cts.Token);
96+
97+
Assert.Equal(new[] { 3 }, updates.BeatmapSetIDs);
98+
Assert.Equal(3, updates.LastProcessedQueueID);
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)