Skip to content

Add support for unit-testing async code in the editor #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "AsyncAwaitUtil",
"references": [
"GUID:478a2357cc57436488a56e564b08d223"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [
{
"name": "com.unity.editorcoroutines",
"expression": "0.0.2-preview",
"define": "HAVE_EDITOR_COROUTINES"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using UnityEngine;
#if UNITY_EDITOR && HAVE_EDITOR_COROUTINES
using Unity.EditorCoroutines.Editor;
#endif

namespace UnityAsyncAwaitUtil
{
public class AsyncCoroutineRunner : MonoBehaviour
public class AsyncCoroutineRunner : MonoBehaviour, AsyncCoroutineRunner.ICoroutineRunner
{
static AsyncCoroutineRunner _instance;
public interface ICoroutineRunner
{
object StartCoroutine(IEnumerator routine);
}

#if UNITY_EDITOR
class EditorAsyncCoroutineRunner : ICoroutineRunner
{
object ICoroutineRunner.StartCoroutine(IEnumerator routine)
{
#if HAVE_EDITOR_COROUTINES
return EditorCoroutineUtility.StartCoroutine(routine, this);
#elif UNITY_2019_1_OR_NEWER
throw new NotImplementedException("Install package com.unity.editorcoroutines");
#else
// asmdef "Version Defines" support doesn't exist yet
throw new NotImplementedException("Install package com.unity.editorcoroutines and define HAVE_EDITOR_COROUTINES");
#endif
}
}
#endif

static ICoroutineRunner _instance;

public static AsyncCoroutineRunner Instance
public static ICoroutineRunner Instance
{
get
{
#if UNITY_EDITOR
if (_instance == null && !Application.isPlaying)
{
_instance = new EditorAsyncCoroutineRunner();
}
#endif
if (_instance == null)
{
_instance = new GameObject("AsyncCoroutineRunner")
Expand All @@ -31,5 +61,10 @@ void Awake()

DontDestroyOnLoad(gameObject);
}

object ICoroutineRunner.StartCoroutine(IEnumerator routine)
{
return StartCoroutine(routine);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace UnityAsyncAwaitUtil
{
public static class SyncContextUtil
{
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod]
#endif
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Install()
{
Expand Down
8 changes: 8 additions & 0 deletions UnityProject/Assets/Plugins/AsyncAwaitUtil/Tests/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,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));
});
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.