summaryrefslogtreecommitdiff
path: root/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEngine.TestRunner/NUnitExtensions/BaseDelegator.cs
blob: 596c0004ff7579c81e1af73515a0c1aa251812d6 (plain) (blame)
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
using System;
using System.Threading;
using NUnit.Framework.Internal;

namespace UnityEngine.TestTools.NUnitExtensions
{
    internal abstract class BaseDelegator
    {
        protected ManualResetEvent m_Signal = new ManualResetEvent(false);

        protected object m_Result;
        protected Exception m_Exception;
        protected ITestExecutionContext m_Context;

        protected bool m_Aborted;

        protected object HandleResult()
        {
            SetCurrentTestContext();
            if (m_Exception != null)
            {
                var temp = m_Exception;
                m_Exception = null;
                throw temp;
            }
            var tempResult = m_Result;
            m_Result = null;
            return tempResult;
        }

        protected void WaitForSignal()
        {
            while (!m_Signal.WaitOne(100))
            {
                if (m_Aborted)
                {
                    m_Aborted = false;
                    Reflect.MethodCallWrapper = null;
                    throw new Exception();
                }
            }
        }

        public void Abort()
        {
            m_Aborted = true;
        }

        protected void SetCurrentTestContext()
        {
            var prop = typeof(TestExecutionContext).GetProperty("CurrentContext");
            if (prop != null)
            {
                prop.SetValue(null, m_Context, null);
            }
        }
    }
}