forked from ppy/osu-queue-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeatmapStatusWatcherTests.cs
More file actions
93 lines (67 loc) · 3.85 KB
/
Copy pathBeatmapStatusWatcherTests.cs
File metadata and controls
93 lines (67 loc) · 3.85 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
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using Xunit;
namespace osu.Server.QueueProcessor.Tests
{
public class BeatmapStatusWatcherTests
{
/// <summary>
/// Checking that processing an empty queue works as expected.
/// </summary>
[Fact]
public async Task TestBasic()
{
var cts = new CancellationTokenSource(10000);
TaskCompletionSource<BeatmapUpdates> tcs = new TaskCompletionSource<BeatmapUpdates>();
using var db = await DatabaseAccess.GetConnectionAsync(cts.Token);
// just a safety measure for now to ensure we don't hit production. since i was running on production until now.
// will throw if not on test database.
if (db.QueryFirstOrDefault<int?>("SELECT `count` FROM `osu_counts` WHERE `name` = 'is_production'") != null)
throw new InvalidOperationException("You are trying to do something very silly.");
await db.ExecuteAsync("TRUNCATE TABLE `bss_process_queue`");
using var poller = await BeatmapStatusWatcher.StartPollingAsync(updates => { tcs.SetResult(updates); }, pollMilliseconds: 100);
await db.ExecuteAsync("INSERT INTO `bss_process_queue` (beatmapset_id) VALUES (1)");
var updates = await tcs.Task.WaitAsync(cts.Token);
Assert.Equal(new[] { 1 }, updates.BeatmapSetIDs);
Assert.Equal(1, updates.LastProcessedQueueID);
tcs = new TaskCompletionSource<BeatmapUpdates>();
await db.ExecuteAsync("INSERT INTO `bss_process_queue` (beatmapset_id) VALUES (2), (3)");
updates = await tcs.Task.WaitAsync(cts.Token);
Assert.Equal(new[] { 2, 3 }, updates.BeatmapSetIDs);
Assert.Equal(3, updates.LastProcessedQueueID);
}
/// <summary>
/// Checking that processing an empty queue works as expected.
/// </summary>
[Fact]
public async Task TestLimit()
{
var cts = new CancellationTokenSource(10000);
TaskCompletionSource<BeatmapUpdates> tcs = new TaskCompletionSource<BeatmapUpdates>();
using var db = await DatabaseAccess.GetConnectionAsync(cts.Token);
// just a safety measure for now to ensure we don't hit production. since i was running on production until now.
// will throw if not on test database.
if (db.QueryFirstOrDefault<int?>("SELECT `count` FROM `osu_counts` WHERE `name` = 'is_production'") != null)
throw new InvalidOperationException("You are trying to do something very silly.");
await db.ExecuteAsync("TRUNCATE TABLE `bss_process_queue`");
using var poller = await BeatmapStatusWatcher.StartPollingAsync(updates => { tcs.SetResult(updates); }, limit: 1, pollMilliseconds: 100);
await db.ExecuteAsync("INSERT INTO `bss_process_queue` (beatmapset_id) VALUES (1)");
var updates = await tcs.Task.WaitAsync(cts.Token);
tcs = new TaskCompletionSource<BeatmapUpdates>();
Assert.Equal(new[] { 1 }, updates.BeatmapSetIDs);
Assert.Equal(1, updates.LastProcessedQueueID);
await db.ExecuteAsync("INSERT INTO `bss_process_queue` (beatmapset_id) VALUES (2), (3)");
updates = await tcs.Task.WaitAsync(cts.Token);
tcs = new TaskCompletionSource<BeatmapUpdates>();
Assert.Equal(new[] { 2 }, updates.BeatmapSetIDs);
Assert.Equal(2, updates.LastProcessedQueueID);
updates = await tcs.Task.WaitAsync(cts.Token);
Assert.Equal(new[] { 3 }, updates.BeatmapSetIDs);
Assert.Equal(3, updates.LastProcessedQueueID);
}
}
}