-
Notifications
You must be signed in to change notification settings - Fork 398
/
TestSceneDrawablePool.cs
439 lines (331 loc) · 14.9 KB
/
TestSceneDrawablePool.cs
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// 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.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Pooling;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing;
using osu.Framework.Timing;
using osu.Framework.Utils;
using osuTK;
using osuTK.Graphics;
namespace osu.Framework.Tests.Visual.Drawables
{
public partial class TestSceneDrawablePool : TestScene
{
private DrawablePool<TestDrawable> pool = null!;
private SpriteText? count;
private readonly HashSet<TestDrawable> consumed = new HashSet<TestDrawable>();
[Test]
public void TestPoolInitialDrawableLoadedAheadOfTime()
{
const int pool_size = 3;
resetWithNewPool(() => new TestPool(TimePerAction, pool_size));
for (int i = 0; i < 3; i++)
AddAssert("check drawable is in ready state", () => pool.Get().LoadState == LoadState.Ready);
}
[Test]
public void TestPoolUsageWithinLimits()
{
const int pool_size = 10;
resetWithNewPool(() => new TestPool(TimePerAction, pool_size));
AddRepeatStep("get new pooled drawable", () => consumeDrawable(), 50);
AddUntilStep("all returned to pool", () => pool.CountAvailable == pool_size);
AddAssert("consumed drawables report returned to pool", () => consumed.All(d => d.IsInPool));
AddAssert("consumed drawables not disposed", () => consumed.All(d => !d.IsDisposed));
AddAssert("consumed less than pool size", () => consumed.Count < pool_size);
}
[Test]
public void TestPoolUsageExceedsLimits()
{
const int pool_size = 10;
resetWithNewPool(() => new TestPool(TimePerAction * 20, pool_size));
AddRepeatStep("get new pooled drawable", () => consumeDrawable(), 50);
AddUntilStep("all returned to pool", () => pool.CountAvailable == consumed.Count);
AddAssert("pool grew in size", () => pool.CountAvailable > pool_size);
AddAssert("consumed drawables report returned to pool", () => consumed.All(d => d.IsInPool));
AddAssert("consumed drawables not disposed", () => consumed.All(d => !d.IsDisposed));
}
[TestCase(10)]
[TestCase(20)]
public void TestPoolInitialSize(int initialPoolSize)
{
resetWithNewPool(() => new TestPool(TimePerAction * 20, initialPoolSize));
AddUntilStep("available count is correct", () => pool.CountAvailable == initialPoolSize);
}
[Test]
public void TestReturnWithoutAdding()
{
resetWithNewPool(() => new TestPool(TimePerAction, 1));
TestDrawable drawable = null!;
AddStep("consume without adding", () => drawable = pool.Get());
AddStep("manually return", () => drawable.Return());
AddUntilStep("free was run", () => drawable.FreedCount == 1);
AddUntilStep("was returned", () => pool.CountAvailable == 1);
AddAssert("manually return twice throws", () =>
{
try
{
drawable.Return();
return false;
}
catch (InvalidOperationException)
{
return true;
}
});
}
[Test]
public void TestPoolReturnWhenAboveCapacity()
{
resetWithNewPool(() => new TestPool(TimePerAction * 20, 1, 1));
TestDrawable first = null!, second = null!;
AddStep("consume item", () => first = consumeDrawable());
AddAssert("pool is empty", () => pool.CountAvailable == 0);
AddStep("consume and return another item", () =>
{
second = pool.Get();
second.Return();
});
AddAssert("first item still in use", () => first.IsInUse);
AddUntilStep("second is returned", () => !second.IsInUse && pool.CountAvailable == 1);
AddStep("expire first", () => first.Expire());
AddUntilStep("wait until first dead", () => !first.IsAlive);
AddUntilStep("drawable is disposed", () => first.IsDisposed);
AddAssert("excess constructed count is one", () => pool.CountExcessConstructed == 1);
}
[Test]
public void TestPrepareAndFreeMethods()
{
resetWithNewPool(() => new TestPool(TimePerAction, 1));
TestDrawable drawable = null!;
TestDrawable drawable2 = null!;
AddStep("consume item", () => drawable = consumeDrawable());
AddAssert("prepare was run", () => drawable.PreparedCount == 1);
AddUntilStep("free was run", () => drawable.FreedCount == 1);
AddStep("consume item", () => drawable2 = consumeDrawable());
AddAssert("is same item", () => ReferenceEquals(drawable, drawable2));
AddAssert("prepare was run", () => drawable2.PreparedCount == 2);
AddUntilStep("free was run", () => drawable2.FreedCount == 2);
AddAssert("no excess constructed count", () => pool.CountExcessConstructed == 0);
}
[Test]
public void TestPrepareOnlyOnceOnMultipleUsages()
{
resetWithNewPool(() => new TestPool(TimePerAction, 1));
TestDrawable drawable = null!;
TestDrawable drawable2 = null!;
AddStep("consume item", () => drawable = consumeDrawable(false));
AddAssert("prepare was not run", () => drawable.PreparedCount == 0);
AddUntilStep("free was not run", () => drawable.FreedCount == 0);
AddStep("manually return drawable", () => pool.Return(drawable));
AddUntilStep("free was run", () => drawable.FreedCount == 1);
AddStep("consume item", () => drawable2 = consumeDrawable());
AddAssert("is same item", () => ReferenceEquals(drawable, drawable2));
AddAssert("prepare was only run once", () => drawable2.PreparedCount == 1);
AddUntilStep("free was run", () => drawable2.FreedCount == 2);
AddAssert("no excess constructed count", () => pool.CountExcessConstructed == 0);
}
[Test]
public void TestUsePoolableDrawableWithoutPool()
{
TestDrawable drawable = null!;
AddStep("consume item", () => Add(drawable = new TestDrawable()));
AddAssert("prepare was run", () => drawable.PreparedCount == 1);
AddUntilStep("free was run", () => drawable.FreedCount == 1);
AddUntilStep("drawable was disposed", () => drawable.IsDisposed);
}
[Test]
public void TestAllDrawablesComeReady()
{
const int pool_size = 10;
List<Drawable> retrieved = new List<Drawable>();
resetWithNewPool(() => new TestPool(TimePerAction * 20, 10, pool_size));
AddStep("get many pooled drawables", () =>
{
retrieved.Clear();
for (int i = 0; i < pool_size * 2; i++)
retrieved.Add(pool.Get());
});
AddAssert("all drawables in ready state", () => retrieved.All(d => d.LoadState == LoadState.Ready));
}
[Test]
public void TestPoolUsageExceedsInitialNoMaximum()
{
const int requested_drawables = 20;
const int initial_size = 10;
resetWithNewPool(() => new TestPool(TimePerAction * 20, initial_size));
AddStep("get many pooled drawables", () =>
{
for (int i = 0; i < requested_drawables; i++)
consumeDrawable();
});
AddAssert("pool saturated", () => pool.CountAvailable, () => Is.EqualTo(0));
AddUntilStep("pool size returned to correct maximum", () => pool.CountAvailable == requested_drawables);
AddUntilStep("excess constructed zero", () => pool.CountExcessConstructed, () => Is.Zero);
AddUntilStep("count in pool is correct", () => consumed.Count(d => d.IsInPool) == requested_drawables);
AddAssert("all drawables in pool", () => consumed.All(d => d.IsInPool));
}
[TestCase(10)]
[TestCase(20)]
public void TestPoolUsageExceedsMaximum(int maxPoolSize)
{
resetWithNewPool(() => new TestPool(TimePerAction * 20, 10, maxPoolSize));
AddStep("get many pooled drawables", () =>
{
for (int i = 0; i < maxPoolSize * 2; i++)
consumeDrawable();
});
AddAssert("pool saturated", () => pool.CountAvailable, () => Is.EqualTo(0));
AddUntilStep("pool size returned to correct maximum", () => pool.CountAvailable == maxPoolSize);
AddUntilStep("excess constructed count correct", () => pool.CountExcessConstructed == maxPoolSize);
AddUntilStep("count in pool is correct", () => consumed.Count(d => d.IsInPool) == maxPoolSize);
AddAssert("excess drawables were used", () => consumed.Any(d => !d.IsInPool));
AddUntilStep("non-returned drawables disposed", () => consumed.Where(d => !d.IsInPool).All(d => d.IsDisposed));
}
[Test]
public void TestGetFromNotLoadedPool()
{
// could theoretically be made to work - and did previously work -
// but it would work in a counterintuitive manner
// (as it will not preload the initial count of drawables in advance on load thread),
// so to avoid a footgun make this throw.
Assert.Throws<InvalidOperationException>(() => new TestPool(100, 1).Get());
}
/// <summary>
/// Tests that when a child of a pooled drawable receives a parent invalidation, the parent pooled drawable is not returned.
/// A parent invalidation can happen on the child if it's added to the hierarchy of the parent.
/// </summary>
[Test]
public void TestParentInvalidationFromChildDoesNotReturnPooledParent()
{
resetWithNewPool(() => new TestPool(TimePerAction, 1));
TestDrawable drawable = null!;
AddStep("consume item", () => drawable = consumeDrawable(false));
AddStep("add child", () => drawable.AddChild(Empty()));
AddAssert("not freed", () => drawable.FreedCount == 0);
}
[Test]
public void TestDrawablePreparedWhenClockRewound()
{
resetWithNewPool(() => new TestPool(TimePerAction, 1));
TestDrawable drawable = null!;
AddStep("consume item and rewind clock", () =>
{
var clock = new ManualClock { CurrentTime = Time.Current };
Add(new Container
{
RelativeSizeAxes = Axes.Both,
Clock = new FramedClock(clock),
Child = drawable = consumeDrawable(false)
});
clock.CurrentTime = 0;
});
AddAssert("child prepared", () => drawable.PreparedCount == 1);
}
protected override void Update()
{
base.Update();
if (count != null)
{
count.Text =
$"available: {pool.CountAvailable} poolSize: {pool.CurrentPoolSize} inUse: {pool.CountInUse} consumed: {consumed.Count} excessConstructed: {pool.CountExcessConstructed} disposed: {consumed.Count(d => d.IsDisposed)}";
}
}
private static int displayCount;
private TestDrawable consumeDrawable(bool addToHierarchy = true)
{
var drawable = pool.Get(d =>
{
d.Position = new Vector2(RNG.NextSingle(), RNG.NextSingle());
d.DisplayString = (++displayCount).ToString();
});
consumed.Add(drawable);
if (addToHierarchy)
Add(drawable);
return drawable;
}
private void resetWithNewPool(Func<DrawablePool<TestDrawable>> createPool)
{
AddStep("reset stats", () => consumed.Clear());
AddStep("create pool", () =>
{
pool = createPool();
Children = new Drawable[]
{
pool,
count = new SpriteText(),
};
});
}
private partial class TestPool : DrawablePool<TestDrawable>
{
private readonly double fadeTime;
public TestPool(double fadeTime, int initialSize, int? maximumSize = null)
: base(initialSize, maximumSize)
{
this.fadeTime = fadeTime;
}
protected override TestDrawable CreateNewDrawable()
{
return new TestDrawable(fadeTime);
}
}
private partial class TestDrawable : PoolableDrawable
{
private readonly double fadeTime;
private readonly SpriteText text;
public string DisplayString
{
set => text.Text = value;
}
public TestDrawable()
: this(1000)
{
}
public TestDrawable(double fadeTime)
{
this.fadeTime = fadeTime;
RelativePositionAxes = Axes.Both;
Size = new Vector2(50);
Origin = Anchor.Centre;
InternalChildren = new Drawable[]
{
new Box
{
Colour = Color4.Green,
RelativeSizeAxes = Axes.Both,
},
text = new SpriteText
{
Text = "-",
Font = FontUsage.Default.With(size: 40),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
};
}
public void AddChild(Drawable drawable) => AddInternal(drawable);
public new bool IsDisposed => base.IsDisposed;
public int PreparedCount { get; private set; }
public int FreedCount { get; private set; }
protected override void PrepareForUse()
{
this.FadeOutFromOne(fadeTime);
this.RotateTo(0).RotateTo(80, fadeTime);
Expire();
PreparedCount++;
}
protected override void FreeAfterUse()
{
base.FreeAfterUse();
FreedCount++;
}
}
}
}