A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.Linq;
5using System.Threading;
6using System.Threading.Tasks;
7using NUnit.Framework;
8using osu.Framework.Allocation;
9using osu.Framework.Graphics;
10using osu.Framework.Graphics.Containers;
11using osu.Framework.Graphics.Shapes;
12using osuTK;
13using osuTK.Graphics;
14
15namespace osu.Framework.Tests.Visual.Drawables
16{
17 public class TestSceneConcurrentLoad : FrameworkTestScene
18 {
19 private const int panel_count = 6;
20
21 private FillFlowContainer flow;
22
23 [SetUp]
24 public void SetUp()
25 {
26 Child = flow = new FillFlowContainer
27 {
28 RelativeSizeAxes = Axes.X,
29 AutoSizeAxes = Axes.Y,
30 };
31 }
32
33 [Test]
34 [Ignore("pointless with LoadComponentAsync concurrency limiting")]
35 public void LoadManyThreaded()
36 {
37 AddStep("load many thread", () =>
38 {
39 for (int i = 0; i < panel_count; i++)
40 LoadComponentAsync(new DelayedTestBox(), flow.Add);
41 });
42
43 AddAssert("check none loaded", () => !flow.Children.OfType<DelayedTestBox>().Any());
44
45 AddUntilStep("check not all loaded", () => flow.Children.OfType<DelayedTestBox>().Any() && flow.Children.OfType<DelayedTestBox>().Count() < panel_count);
46
47 AddUntilStep("wait all loaded", () => flow.Children.Count == panel_count);
48 }
49
50 [Test]
51 [Ignore("pointless with LoadComponentAsync concurrency limiting")]
52 public void LoadManyAsync()
53 {
54 AddStep("load many async", () =>
55 {
56 for (int i = 0; i < panel_count; i++)
57 LoadComponentAsync(new DelayedTestBoxAsync(), flow.Add);
58 });
59
60 AddAssert("check none loaded", () => !flow.Children.OfType<DelayedTestBoxAsync>().Any());
61
62 AddUntilStep("wait some loaded", () => flow.Children.OfType<DelayedTestBoxAsync>().Any());
63
64 // due to thread yielding all should be loaded straight after any are loaded.
65 AddAssert("check all loaded", () => flow.Children.OfType<DelayedTestBoxAsync>().Count() == panel_count);
66 }
67
68 public class DelayedTestBox : Box
69 {
70 public DelayedTestBox()
71 {
72 Size = new Vector2(50);
73 Colour = Color4.Blue;
74 }
75
76 [BackgroundDependencyLoader]
77 private void load()
78 {
79 Thread.Sleep((int)(1000 / Clock.Rate));
80 }
81 }
82
83 public class DelayedTestBoxAsync : Box
84 {
85 public DelayedTestBoxAsync()
86 {
87 Size = new Vector2(50);
88 Colour = Color4.Green;
89 }
90
91 [BackgroundDependencyLoader]
92 private async Task load()
93 {
94 await Task.Delay((int)(1000 / Clock.Rate)).ConfigureAwait(false);
95 }
96 }
97 }
98}