-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathTestMisc.cs
More file actions
57 lines (52 loc) · 1.6 KB
/
TestMisc.cs
File metadata and controls
57 lines (52 loc) · 1.6 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace UnityAsyncAwaitUtil
{
class TestMisc
{
// Helper for writing async editor tests.
static IEnumerator AsUnityTest(Func<Task> body)
{
var task = Task.Run(body);
while (!task.IsCompleted)
{
yield return null;
}
if (task.IsFaulted)
{
ExceptionDispatchInfo.Capture(task.Exception.InnerExceptions[0]).Throw();
}
}
[UnityTest]
public IEnumerator TestAsyncCoroutineRunnerInEditor()
{
List<bool> result = new List<bool>();
IEnumerator AppendResult()
{
yield return null;
result.Add(true);
}
AsyncCoroutineRunner.Instance.StartCoroutine(AppendResult());
for (int i = 0; i < 10; ++i)
{
if (result.Count > 0) { yield break; }
yield return null;
}
Assert.Fail("Coroutine did not complete in the allotted time");
}
[UnityTest]
public IEnumerator TestAwaitSecondsRealtime() => AsUnityTest(async () =>
{
const float delay = .25f;
var start = DateTime.Now;
await new WaitForSecondsRealtime(delay);
Assert.Greater(DateTime.Now - start, TimeSpan.FromSeconds(delay - .01));
});
}
}