Skip to content

Commit 71c9f95

Browse files
committed
Add test coverage
1 parent aa3d55a commit 71c9f95

File tree

1 file changed

+93
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)