No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActionDelegator.cs 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  3. using UnityEngine.TestTools.Logging;
  4. namespace UnityEngine.TestTools.NUnitExtensions
  5. {
  6. /// <summary>
  7. /// This class delegates actions from the NUnit thread that should be executed on the main thread.
  8. /// NUnit thread calls Delegate which blocks the execution on the thread until the action is executed.
  9. /// The main thread will poll for awaiting actions (HasAction) and invoke them (Execute).
  10. /// Once the action is executed, the main thread releases the lock and executino on the NUnit thread is continued.
  11. /// </summary>
  12. internal class ActionDelegator : BaseDelegator
  13. {
  14. private Func<object> m_Action;
  15. public object Delegate(Action action)
  16. {
  17. return Delegate(() => { action(); return null; });
  18. }
  19. public object Delegate(Func<object> action)
  20. {
  21. if (m_Aborted)
  22. {
  23. return null;
  24. }
  25. AssertState();
  26. m_Context = UnityTestExecutionContext.CurrentContext;
  27. m_Signal.Reset();
  28. m_Action = action;
  29. WaitForSignal();
  30. return HandleResult();
  31. }
  32. private void AssertState()
  33. {
  34. if (m_Action != null)
  35. {
  36. throw new Exception("Action not executed yet");
  37. }
  38. }
  39. public bool HasAction()
  40. {
  41. return m_Action != null;
  42. }
  43. public void Execute(LogScope logScope)
  44. {
  45. try
  46. {
  47. SetCurrentTestContext();
  48. m_Result = m_Action();
  49. logScope.EvaluateLogScope(true);
  50. }
  51. catch (Exception e)
  52. {
  53. m_Exception = e;
  54. }
  55. finally
  56. {
  57. m_Action = null;
  58. m_Signal.Set();
  59. }
  60. }
  61. }
  62. }