Skip to content

Commit

Permalink
Merge pull request #134 from nowsprinting/chore/unitaskvoid_terminate…
Browse files Browse the repository at this point in the history
…async

Mod TerminateAsync return type UniTaskVoid and remove parameter CancellationToken
  • Loading branch information
asurato authored Jan 10, 2025
2 parents d0ac012 + c5b736e commit 83c8e91
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
16 changes: 7 additions & 9 deletions Runtime/Autopilot.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) 2023-2024 DeNA Co., Ltd.
// Copyright (c) 2023-2025 DeNA Co., Ltd.
// This software is released under the MIT License.

using System;
using System.Collections;
using System.Threading;
using Cysharp.Threading.Tasks;
using DeNA.Anjin.Settings;
using DeNA.Anjin.Utilities;
Expand All @@ -28,10 +27,9 @@ public interface ITerminatable
/// <param name="message">Log message string or terminate message</param>
/// <param name="stackTrace">Stack trace when terminate by the log message</param>
/// <param name="reporting">Call Reporter if true</param>
/// <param name="token">Cancellation token</param>
/// <returns>A task awaits termination get completed</returns>
UniTask TerminateAsync(ExitCode exitCode, string message = null, string stackTrace = null,
bool reporting = true, CancellationToken token = default);
UniTaskVoid TerminateAsync(ExitCode exitCode, string message = null, string stackTrace = null,
bool reporting = true);
}

/// <summary>
Expand Down Expand Up @@ -144,7 +142,7 @@ private void DispatchByLoadedScenes()
private IEnumerator Lifespan(int timeoutSec, ExitCode exitCode, string message)
{
yield return new WaitForSecondsRealtime(timeoutSec);
yield return UniTask.ToCoroutine(() => TerminateAsync(exitCode, message));
TerminateAsync(exitCode, message).Forget();
}

private void OnDestroy()
Expand All @@ -157,8 +155,8 @@ private void OnDestroy()
}

/// <inheritdoc/>
public async UniTask TerminateAsync(ExitCode exitCode, string message = null, string stackTrace = null,
bool reporting = true, CancellationToken token = default)
public async UniTaskVoid TerminateAsync(ExitCode exitCode, string message = null, string stackTrace = null,
bool reporting = true)
{
if (_isTerminating)
{
Expand All @@ -176,7 +174,7 @@ public async UniTask TerminateAsync(ExitCode exitCode, string message = null, st

if (reporting && _state.IsRunning && _settings.Reporter != null)
{
await _settings.Reporter.PostReportAsync(message, stackTrace, exitCode, token);
await _settings.Reporter.PostReportAsync(message, stackTrace, exitCode);
}

Destroy(this.gameObject);
Expand Down
12 changes: 9 additions & 3 deletions Tests/Runtime/AutopilotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public async Task TerminateAsync_DestroyedAutopilotAndAgentObjects()
var agents = AgentInspector.Instances;
Assume.That(agents, Is.Not.Empty, "Agents are running");

await autopilot.TerminateAsync(ExitCode.Normally, reporting: false);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
autopilot.TerminateAsync(ExitCode.Normally, reporting: false);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
await UniTask.NextFrame(); // wait for destroy

autopilot = Object.FindObjectOfType<Autopilot>(); // re-find after terminated
Expand Down Expand Up @@ -138,7 +140,9 @@ public async Task TerminateAsync_NoReporting_NotCallReporter()
await UniTask.Delay(500); // wait for launch

var autopilot = Autopilot.Instance;
await autopilot.TerminateAsync(ExitCode.Normally, null, null, false);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
autopilot.TerminateAsync(ExitCode.Normally, null, null, false);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

Assert.That(spyReporter.IsCalled, Is.False);
}
Expand All @@ -153,7 +157,9 @@ public async Task TerminateAsync_Reporting_CallReporter()
await UniTask.Delay(500); // wait for launch

var autopilot = Autopilot.Instance;
await autopilot.TerminateAsync(ExitCode.Normally, "message", "stack trace", true);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
autopilot.TerminateAsync(ExitCode.Normally, "message", "stack trace", true);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

Assert.That(spyReporter.IsCalled, Is.True);
Assert.That(spyReporter.Arguments["exitCode"], Is.EqualTo("Normally"));
Expand Down
7 changes: 3 additions & 4 deletions Tests/Runtime/TestDoubles/SpyTerminatable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2023-2024 DeNA Co., Ltd.
// Copyright (c) 2023-2025 DeNA Co., Ltd.
// This software is released under the MIT License.

using System.Threading;
using Cysharp.Threading.Tasks;

namespace DeNA.Anjin.TestDoubles
Expand All @@ -14,8 +13,8 @@ public class SpyTerminatable : ITerminatable
public string CapturedStackTrace { get; private set; }
public bool CapturedReporting { get; private set; }

public async UniTask TerminateAsync(ExitCode exitCode, string message = null, string stackTrace = null,
bool reporting = true, CancellationToken token = default)
public async UniTaskVoid TerminateAsync(ExitCode exitCode, string message = null, string stackTrace = null,
bool reporting = true)
{
IsCalled = true;
CapturedExitCode = exitCode;
Expand Down

0 comments on commit 83c8e91

Please sign in to comment.