RemoteTestResultSender.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using NUnit.Framework.Interfaces;
  7. using UnityEngine.Networking.PlayerConnection;
  8. using UnityEngine.TestRunner.TestLaunchers;
  9. namespace UnityEngine.TestTools.TestRunner.Callbacks
  10. {
  11. [AddComponentMenu("")]
  12. internal class RemoteTestResultSender : MonoBehaviour, ITestRunnerListener
  13. {
  14. private class QueueData
  15. {
  16. public Guid id { get; set; }
  17. public byte[] data { get; set; }
  18. }
  19. private const int k_aliveMessageFrequency = 120;
  20. private float m_NextliveMessage = k_aliveMessageFrequency;
  21. private readonly Queue<QueueData> m_SendQueue = new Queue<QueueData>();
  22. private readonly object m_LockQueue = new object();
  23. private readonly IRemoteTestResultDataFactory m_TestResultDataFactory = new RemoteTestResultDataFactory();
  24. public void Start()
  25. {
  26. StartCoroutine(SendDataRoutine());
  27. }
  28. private byte[] SerializeObject(object objectToSerialize)
  29. {
  30. return Encoding.UTF8.GetBytes(JsonUtility.ToJson(objectToSerialize));
  31. }
  32. public void RunStarted(ITest testsToRun)
  33. {
  34. var data = SerializeObject(m_TestResultDataFactory.CreateFromTest(testsToRun));
  35. lock (m_LockQueue)
  36. {
  37. m_SendQueue.Enqueue(new QueueData
  38. {
  39. id = PlayerConnectionMessageIds.runStartedMessageId,
  40. data = data
  41. });
  42. }
  43. }
  44. public void RunFinished(ITestResult testResults)
  45. {
  46. var data = SerializeObject(m_TestResultDataFactory.CreateFromTestResult(testResults));
  47. lock (m_LockQueue)
  48. {
  49. m_SendQueue.Enqueue(new QueueData { id = PlayerConnectionMessageIds.runFinishedMessageId, data = data, });
  50. }
  51. }
  52. public void TestStarted(ITest test)
  53. {
  54. var data = SerializeObject(m_TestResultDataFactory.CreateFromTest(test));
  55. lock (m_LockQueue)
  56. {
  57. m_SendQueue.Enqueue(new QueueData
  58. {
  59. id = PlayerConnectionMessageIds.testStartedMessageId,
  60. data = data
  61. });
  62. }
  63. }
  64. public void TestFinished(ITestResult result)
  65. {
  66. var testRunnerResultForApi = m_TestResultDataFactory.CreateFromTestResult(result);
  67. var resultData = SerializeObject(testRunnerResultForApi);
  68. lock (m_LockQueue)
  69. {
  70. m_SendQueue.Enqueue(new QueueData
  71. {
  72. id = PlayerConnectionMessageIds.testFinishedMessageId,
  73. data = resultData,
  74. });
  75. }
  76. }
  77. public IEnumerator SendDataRoutine()
  78. {
  79. while (!PlayerConnection.instance.isConnected)
  80. {
  81. yield return new WaitForSeconds(1);
  82. }
  83. while (true)
  84. {
  85. lock (m_LockQueue)
  86. {
  87. if (PlayerConnection.instance.isConnected && m_SendQueue.Count > 0)
  88. {
  89. ResetNextPlayerAliveMessageTime();
  90. var queueData = m_SendQueue.Dequeue();
  91. PlayerConnection.instance.Send(queueData.id, queueData.data);
  92. yield return null;
  93. }
  94. //This is needed so we dont stall the player totally
  95. if (!m_SendQueue.Any())
  96. {
  97. SendAliveMessageIfNeeded();
  98. yield return new WaitForSeconds(0.02f);
  99. }
  100. }
  101. }
  102. }
  103. private void SendAliveMessageIfNeeded()
  104. {
  105. if (Time.timeSinceLevelLoad < m_NextliveMessage)
  106. {
  107. return;
  108. }
  109. Debug.Log("Sending player alive message back to editor.");
  110. ResetNextPlayerAliveMessageTime();
  111. PlayerConnection.instance.Send(PlayerConnectionMessageIds.playerAliveHeartbeat, new byte[0]);
  112. }
  113. private void ResetNextPlayerAliveMessageTime()
  114. {
  115. m_NextliveMessage = Time.timeSinceLevelLoad + k_aliveMessageFrequency;
  116. }
  117. }
  118. }