-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathTestLoadExceptions.cs
More file actions
320 lines (271 loc) · 10 KB
/
Copy pathTestLoadExceptions.cs
File metadata and controls
320 lines (271 loc) · 10 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// 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.
#nullable disable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Extensions.ExceptionExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Testing;
using System.Numerics;
using osuTK.Graphics;
namespace osu.Framework.Tests.Exceptions
{
[TestFixture]
public partial class TestLoadExceptions
{
[Test]
public void TestLoadIntoInvalidTarget()
{
var loadable = new DelayedTestBoxAsync();
var loadTarget = new LoadTarget(loadable);
Assert.Throws<InvalidOperationException>(() => loadTarget.PerformAsyncLoad());
}
[Test]
public void TestSingleSyncAdd()
{
var loadable = new DelayedTestBoxAsync();
Assert.DoesNotThrow(() =>
{
runGameWithLogic(g =>
{
g.Add(loadable);
Assert.IsTrue(loadable.LoadState == LoadState.Ready);
Assert.AreEqual(loadable.Parent, g);
g.Exit();
});
});
}
[Test]
public void TestUnobservedException()
{
Exception loggedException = null;
Logger.NewEntry += newLogEntry;
try
{
var exception = Assert.Throws<AggregateException>(() =>
{
Task.Factory.StartNew(() =>
{
runGameWithLogic(g =>
{
g.Scheduler.Add(() => Task.Run(() => throw new InvalidOperationException()));
g.Scheduler.AddDelayed(collect, 1, true);
if (loggedException != null)
throw loggedException;
});
}, TaskCreationOptions.LongRunning).Wait(TimeSpan.FromSeconds(10));
Assert.Fail("Game execution was not aborted");
});
Assert.True(exception?.AsSingular() is InvalidOperationException);
}
finally
{
Logger.NewEntry -= newLogEntry;
}
void newLogEntry(LogEntry entry) => loggedException = entry.Exception;
}
private static void collect()
{
GC.Collect();
}
[Test]
public void TestSingleAsyncAdd()
{
var loadable = new DelayedTestBoxAsync();
var loadTarget = new LoadTarget(loadable);
Assert.DoesNotThrow(() =>
{
runGameWithLogic(g =>
{
g.Add(loadTarget);
loadTarget.PerformAsyncLoad();
}, _ => loadable.Parent == loadTarget);
});
}
[Test]
public void TestDoubleAsyncLoad()
{
var loadable = new DelayedTestBoxAsync();
var loadTarget = new LoadTarget(loadable);
Assert.DoesNotThrow(() =>
{
runGameWithLogic(g =>
{
g.Add(loadTarget);
loadTarget.PerformAsyncLoad();
loadTarget.PerformAsyncLoad(false);
}, _ => loadable.Parent == loadTarget);
});
}
[Test]
public void TestDoubleAsyncAddFails()
{
Assert.Throws<InvalidOperationException>(() =>
{
runGameWithLogic(g =>
{
var loadable = new DelayedTestBoxAsync();
var loadTarget = new LoadTarget(loadable);
g.Add(loadTarget);
loadTarget.PerformAsyncLoad();
loadTarget.PerformAsyncLoad();
});
});
}
[Test]
public void TestTargetDisposedDuringAsyncLoad()
{
Assert.Throws<ObjectDisposedException>(() =>
{
runGameWithLogic(g =>
{
var loadable = new DelayedTestBoxAsync();
var loadTarget = new LoadTarget(loadable);
g.Add(loadTarget);
loadTarget.PerformAsyncLoad();
while (loadable.LoadState < LoadState.Loading)
Thread.Sleep(1);
g.Dispose();
});
});
}
[Test]
public void TestLoadableDisposedDuringAsyncLoad()
{
Assert.Throws<ObjectDisposedException>(() =>
{
runGameWithLogic(g =>
{
var loadable = new DelayedTestBoxAsync();
var loadTarget = new LoadTarget(loadable);
g.Add(loadTarget);
loadTarget.PerformAsyncLoad();
while (loadable.LoadState < LoadState.Loading)
Thread.Sleep(1);
loadable.Dispose();
});
});
}
/// <summary>
/// The async load completion callback is scheduled on the <see cref="Game"/>. The callback is generally used to add the child to the container,
/// however it is possible for the container to be disposed when this occurs due to being scheduled on the <see cref="Game"/>. If this occurs,
/// the cancellation is invoked and the completion task should not be run.
///
/// This is a very timing-dependent test which performs the following sequence:
/// LoadAsync -> schedule Callback -> dispose parent -> invoke scheduled callback
/// </summary>
[Test]
public void TestDisposeAfterLoad()
{
Assert.DoesNotThrow(() =>
{
var loadTarget = new LoadTarget(new DelayedTestBoxAsync());
bool allowDispose = false;
bool disposeTriggered = false;
bool updatedAfterDispose = false;
runGameWithLogic(g =>
{
g.Add(loadTarget);
loadTarget.PerformAsyncLoad().ContinueWith(_ => allowDispose = true);
}, g =>
{
// The following code is done here for a very specific reason, but can occur naturally in normal use
// This delegate is essentially the first item in the game's scheduler, so it will always run PRIOR to the async callback
if (disposeTriggered)
updatedAfterDispose = true;
if (allowDispose)
{
// Async load has complete, the callback has been scheduled but NOT run yet
// Dispose the parent container - this is done by clearing the game
g.Clear(true);
disposeTriggered = true;
}
// After disposing the parent, one update loop is required
return updatedAfterDispose;
});
});
}
[Test]
public void TestSyncLoadException()
{
Assert.Throws<AsyncTestException>(() => runGameWithLogic(g => g.Add(new DelayedTestBoxAsync(true))));
}
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
private void runGameWithLogic(Action<Game> logic, Func<Game, bool> exitCondition = null)
{
Storage storage = null;
try
{
using (var host = new TestRunHeadlessGameHost($"{GetType().Name}-{Guid.NewGuid()}", new HostOptions()))
{
using (var game = new TestGame())
{
game.Schedule(() =>
{
storage = host.Storage;
host.UpdateThread.Scheduler.AddDelayed(() =>
{
if (exitCondition?.Invoke(game) == true)
host.Exit();
}, 0, true);
logic(game);
});
host.Run(game);
}
}
}
finally
{
try
{
storage?.DeleteDirectory(string.Empty);
}
catch
{
// May fail due to the file handles still being open on Windows, but this isn't a big problem for us
}
}
}
private partial class LoadTarget : Container
{
private readonly Drawable loadable;
public LoadTarget(Drawable loadable)
{
this.loadable = loadable;
}
public Task PerformAsyncLoad(bool withAdd = true) => LoadComponentAsync(loadable, _ =>
{
if (withAdd) Add(loadable);
});
}
public partial class DelayedTestBoxAsync : Box
{
private readonly bool throws;
public DelayedTestBoxAsync(bool throws = false)
{
this.throws = throws;
Size = new Vector2(50);
Colour = Color4.Green;
}
[BackgroundDependencyLoader]
private void load()
{
Task.Delay((int)(1000 / Clock.Rate)).WaitSafely();
if (throws)
throw new AsyncTestException();
}
}
private class AsyncTestException : Exception
{
}
}
}