|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reflection; |
| 4 | +using NUnit.Framework; |
| 5 | +using MCPForUnity.Editor.Services; |
| 6 | + |
| 7 | +namespace MCPForUnityTests.Editor.Services |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Tests for TestJobManager's per-job InitTimeoutMs feature. |
| 11 | + /// Uses reflection to manipulate internal state since StartJob triggers a real test run. |
| 12 | + /// </summary> |
| 13 | + public class TestJobManagerInitTimeoutTests |
| 14 | + { |
| 15 | + private FieldInfo _jobsField; |
| 16 | + private FieldInfo _currentJobIdField; |
| 17 | + private MethodInfo _getJobMethod; |
| 18 | + private MethodInfo _persistMethod; |
| 19 | + private MethodInfo _restoreMethod; |
| 20 | + private Type _testJobType; |
| 21 | + |
| 22 | + private string _originalJobId; |
| 23 | + |
| 24 | + [SetUp] |
| 25 | + public void SetUp() |
| 26 | + { |
| 27 | + var asm = typeof(MCPServiceLocator).Assembly; |
| 28 | + var managerType = asm.GetType("MCPForUnity.Editor.Services.TestJobManager"); |
| 29 | + Assert.NotNull(managerType, "Could not find TestJobManager"); |
| 30 | + |
| 31 | + _testJobType = asm.GetType("MCPForUnity.Editor.Services.TestJob"); |
| 32 | + Assert.NotNull(_testJobType, "Could not find TestJob"); |
| 33 | + |
| 34 | + _jobsField = managerType.GetField("Jobs", BindingFlags.NonPublic | BindingFlags.Static); |
| 35 | + Assert.NotNull(_jobsField, "Could not find Jobs field"); |
| 36 | + |
| 37 | + _currentJobIdField = managerType.GetField("_currentJobId", BindingFlags.NonPublic | BindingFlags.Static); |
| 38 | + Assert.NotNull(_currentJobIdField, "Could not find _currentJobId field"); |
| 39 | + |
| 40 | + _getJobMethod = managerType.GetMethod("GetJob", BindingFlags.NonPublic | BindingFlags.Static); |
| 41 | + Assert.NotNull(_getJobMethod, "Could not find GetJob method"); |
| 42 | + |
| 43 | + _persistMethod = managerType.GetMethod("PersistToSessionState", BindingFlags.NonPublic | BindingFlags.Static); |
| 44 | + Assert.NotNull(_persistMethod, "Could not find PersistToSessionState method"); |
| 45 | + |
| 46 | + _restoreMethod = managerType.GetMethod("TryRestoreFromSessionState", BindingFlags.NonPublic | BindingFlags.Static); |
| 47 | + Assert.NotNull(_restoreMethod, "Could not find TryRestoreFromSessionState method"); |
| 48 | + |
| 49 | + // Snapshot original state |
| 50 | + _originalJobId = _currentJobIdField.GetValue(null) as string; |
| 51 | + // We'll restore _currentJobId in TearDown; Jobs dictionary is shared static state |
| 52 | + } |
| 53 | + |
| 54 | + [TearDown] |
| 55 | + public void TearDown() |
| 56 | + { |
| 57 | + // Restore original state |
| 58 | + _currentJobIdField.SetValue(null, _originalJobId); |
| 59 | + // Clean up any test jobs we inserted |
| 60 | + var jobs = _jobsField.GetValue(null) as System.Collections.IDictionary; |
| 61 | + jobs?.Remove("test-init-timeout-job"); |
| 62 | + jobs?.Remove("test-init-timeout-default"); |
| 63 | + jobs?.Remove("test-init-timeout-persist"); |
| 64 | + // Flush cleaned state to SessionState so synthetic jobs don't survive domain reloads. |
| 65 | + // The persist test writes to SessionState; without this, the stub job would be |
| 66 | + // restored on the next [InitializeOnLoadMethod] and pollute later test runs. |
| 67 | + _persistMethod.Invoke(null, new object[] { true }); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void GetJob_WithCustomInitTimeout_UsesPerJobTimeout() |
| 72 | + { |
| 73 | + // Arrange: insert a job with a custom init timeout and a start time far enough in the |
| 74 | + // past to exceed the default 15s but within the custom 120s. |
| 75 | + var jobs = _jobsField.GetValue(null) as System.Collections.IDictionary; |
| 76 | + long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 77 | + |
| 78 | + var job = Activator.CreateInstance(_testJobType); |
| 79 | + _testJobType.GetProperty("JobId").SetValue(job, "test-init-timeout-job"); |
| 80 | + _testJobType.GetProperty("Status").SetValue(job, TestJobStatus.Running); |
| 81 | + _testJobType.GetProperty("Mode").SetValue(job, "PlayMode"); |
| 82 | + _testJobType.GetProperty("StartedUnixMs").SetValue(job, now - 30_000); // 30s ago |
| 83 | + _testJobType.GetProperty("LastUpdateUnixMs").SetValue(job, now - 30_000); |
| 84 | + _testJobType.GetProperty("TotalTests").SetValue(job, null); // Not initialized yet |
| 85 | + _testJobType.GetProperty("InitTimeoutMs").SetValue(job, 120_000L); // 120s custom timeout |
| 86 | + _testJobType.GetProperty("FailuresSoFar").SetValue(job, new List<TestJobFailure>()); |
| 87 | + |
| 88 | + jobs["test-init-timeout-job"] = job; |
| 89 | + _currentJobIdField.SetValue(null, "test-init-timeout-job"); |
| 90 | + |
| 91 | + // Act: GetJob should NOT auto-fail because 30s < 120s custom timeout |
| 92 | + var result = _getJobMethod.Invoke(null, new object[] { "test-init-timeout-job" }); |
| 93 | + |
| 94 | + // Assert: job should still be running |
| 95 | + var status = (TestJobStatus)_testJobType.GetProperty("Status").GetValue(result); |
| 96 | + Assert.AreEqual(TestJobStatus.Running, status, |
| 97 | + "Job with 120s custom timeout should not auto-fail after 30s"); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public void GetJob_WithDefaultTimeout_AutoFailsAfter15Seconds() |
| 102 | + { |
| 103 | + // Arrange: insert a job with InitTimeoutMs=0 (use default) and start time 20s ago |
| 104 | + var jobs = _jobsField.GetValue(null) as System.Collections.IDictionary; |
| 105 | + long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 106 | + |
| 107 | + var job = Activator.CreateInstance(_testJobType); |
| 108 | + _testJobType.GetProperty("JobId").SetValue(job, "test-init-timeout-default"); |
| 109 | + _testJobType.GetProperty("Status").SetValue(job, TestJobStatus.Running); |
| 110 | + _testJobType.GetProperty("Mode").SetValue(job, "EditMode"); |
| 111 | + _testJobType.GetProperty("StartedUnixMs").SetValue(job, now - 20_000); // 20s ago |
| 112 | + _testJobType.GetProperty("LastUpdateUnixMs").SetValue(job, now - 20_000); |
| 113 | + _testJobType.GetProperty("TotalTests").SetValue(job, null); |
| 114 | + _testJobType.GetProperty("InitTimeoutMs").SetValue(job, 0L); // Use default |
| 115 | + _testJobType.GetProperty("FailuresSoFar").SetValue(job, new List<TestJobFailure>()); |
| 116 | + |
| 117 | + jobs["test-init-timeout-default"] = job; |
| 118 | + _currentJobIdField.SetValue(null, "test-init-timeout-default"); |
| 119 | + |
| 120 | + // Act: GetJob should auto-fail because 20s > 15s default |
| 121 | + var result = _getJobMethod.Invoke(null, new object[] { "test-init-timeout-default" }); |
| 122 | + |
| 123 | + // Assert: job should be failed |
| 124 | + var status = (TestJobStatus)_testJobType.GetProperty("Status").GetValue(result); |
| 125 | + Assert.AreEqual(TestJobStatus.Failed, status, |
| 126 | + "Job with default timeout should auto-fail after 20s"); |
| 127 | + } |
| 128 | + |
| 129 | + [Test] |
| 130 | + public void InitTimeoutMs_SurvivesPersistAndRestore() |
| 131 | + { |
| 132 | + // Arrange: insert a job with custom InitTimeoutMs |
| 133 | + var jobs = _jobsField.GetValue(null) as System.Collections.IDictionary; |
| 134 | + long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 135 | + |
| 136 | + var job = Activator.CreateInstance(_testJobType); |
| 137 | + _testJobType.GetProperty("JobId").SetValue(job, "test-init-timeout-persist"); |
| 138 | + _testJobType.GetProperty("Status").SetValue(job, TestJobStatus.Running); |
| 139 | + _testJobType.GetProperty("Mode").SetValue(job, "PlayMode"); |
| 140 | + _testJobType.GetProperty("StartedUnixMs").SetValue(job, now); |
| 141 | + _testJobType.GetProperty("LastUpdateUnixMs").SetValue(job, now); |
| 142 | + _testJobType.GetProperty("TotalTests").SetValue(job, null); |
| 143 | + _testJobType.GetProperty("InitTimeoutMs").SetValue(job, 90_000L); |
| 144 | + _testJobType.GetProperty("FailuresSoFar").SetValue(job, new List<TestJobFailure>()); |
| 145 | + |
| 146 | + jobs["test-init-timeout-persist"] = job; |
| 147 | + _currentJobIdField.SetValue(null, "test-init-timeout-persist"); |
| 148 | + |
| 149 | + // Act: persist then restore (simulates domain reload) |
| 150 | + _persistMethod.Invoke(null, new object[] { true }); |
| 151 | + // Clear in-memory state |
| 152 | + jobs.Remove("test-init-timeout-persist"); |
| 153 | + _currentJobIdField.SetValue(null, null); |
| 154 | + // Restore from SessionState |
| 155 | + _restoreMethod.Invoke(null, null); |
| 156 | + |
| 157 | + // Assert: restored job should have the same InitTimeoutMs |
| 158 | + var restoredJobs = _jobsField.GetValue(null) as System.Collections.IDictionary; |
| 159 | + Assert.IsTrue(restoredJobs.Contains("test-init-timeout-persist"), |
| 160 | + "Job should be restored from SessionState"); |
| 161 | + |
| 162 | + var restoredJob = restoredJobs["test-init-timeout-persist"]; |
| 163 | + var restoredTimeout = (long)_testJobType.GetProperty("InitTimeoutMs").GetValue(restoredJob); |
| 164 | + Assert.AreEqual(90_000L, restoredTimeout, |
| 165 | + "InitTimeoutMs should survive persist/restore cycle"); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments