From c55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sun, 19 Apr 2020 17:19:32 -0400 Subject: Inital commit --- .../NUnitExtensions/ActionDelegator.cs | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Library/PackageCache/com.unity.test-framework@1.1.11/UnityEngine.TestRunner/NUnitExtensions/ActionDelegator.cs (limited to 'Library/PackageCache/com.unity.test-framework@1.1.11/UnityEngine.TestRunner/NUnitExtensions/ActionDelegator.cs') diff --git a/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEngine.TestRunner/NUnitExtensions/ActionDelegator.cs b/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEngine.TestRunner/NUnitExtensions/ActionDelegator.cs new file mode 100644 index 0000000..d13a7e0 --- /dev/null +++ b/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEngine.TestRunner/NUnitExtensions/ActionDelegator.cs @@ -0,0 +1,79 @@ +using System; +using System.Linq; +using UnityEngine.TestRunner.NUnitExtensions.Runner; +using UnityEngine.TestTools.Logging; +using UnityEngine.TestTools.TestRunner; + +namespace UnityEngine.TestTools.NUnitExtensions +{ + /// + /// This class delegates actions from the NUnit thread that should be executed on the main thread. + /// NUnit thread calls Delegate which blocks the execution on the thread until the action is executed. + /// The main thread will poll for awaiting actions (HasAction) and invoke them (Execute). + /// Once the action is executed, the main thread releases the lock and executino on the NUnit thread is continued. + /// + internal class ActionDelegator : BaseDelegator + { + private Func m_Action; + public object Delegate(Action action) + { + return Delegate(() => { action(); return null; }); + } + + public object Delegate(Func action) + { + if (m_Aborted) + { + return null; + } + + AssertState(); + m_Context = UnityTestExecutionContext.CurrentContext; + + m_Signal.Reset(); + m_Action = action; + + WaitForSignal(); + + return HandleResult(); + } + + private void AssertState() + { + if (m_Action != null) + { + throw new Exception("Action not executed yet"); + } + } + + public bool HasAction() + { + return m_Action != null; + } + + public void Execute(LogScope logScope) + { + try + { + SetCurrentTestContext(); + m_Result = m_Action(); + if (logScope.AnyFailingLogs()) + { + var failingLog = logScope.FailingLogs.First(); + throw new UnhandledLogMessageException(failingLog); + } + if (logScope.ExpectedLogs.Any()) + throw new UnexpectedLogMessageException(LogScope.Current.ExpectedLogs.Peek()); + } + catch (Exception e) + { + m_Exception = e; + } + finally + { + m_Action = null; + m_Signal.Set(); + } + } + } +} -- cgit v1.2.3