Skip to content

Commit

Permalink
Fix tests using spy agent
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed Apr 22, 2024
1 parent bc8bcd2 commit 5cc6137
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
48 changes: 21 additions & 27 deletions Tests/Runtime/AgentDispatcherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Cysharp.Threading.Tasks.Triggers;
using DeNA.Anjin.Agents;
using DeNA.Anjin.Settings;
using DeNA.Anjin.TestDoubles;
using DeNA.Anjin.Utilities;
using NUnit.Framework;
using UnityEngine;
Expand Down Expand Up @@ -50,11 +51,11 @@ private static AutopilotSettings CreateAutopilotSettings()
return testSettings;
}

private static DoNothingAgent CreateDoNothingAgent(string name = nameof(DoNothingAgent))
private static SpyAliveCountAgent CreateSpyAliveCountAgent(string name = nameof(DoNothingAgent))
{
var doNothingAgent = ScriptableObject.CreateInstance<DoNothingAgent>();
doNothingAgent.name = name;
return doNothingAgent;
var agent = ScriptableObject.CreateInstance<SpyAliveCountAgent>();
agent.name = name;
return agent;
}

private void SetUpDispatcher(AutopilotSettings settings)
Expand All @@ -81,41 +82,37 @@ private static async UniTask<Scene> LoadTestSceneAsync(string path, LoadSceneMod
return scene;
}

private static IEnumerable<string> FindAliveAgentNames()
{
return Object.FindObjectsOfType<AsyncDestroyTrigger>().Select(x => x.name);
// Note: AsyncDestroyTrigger is a component attached to the GameObject when binding agent by GetCancellationTokenOnDestroy.
}

[Test]
public async Task DispatchByScene_DispatchAgentBySceneAgentMaps()
{
const string AgentName = "Mapped Agent";
var settings = CreateAutopilotSettings();
settings.sceneAgentMaps.Add(new SceneAgentMap
{
scenePath = TestScenePath, agent = CreateDoNothingAgent(AgentName)
scenePath = TestScenePath, agent = CreateSpyAliveCountAgent(AgentName)
});
SetUpDispatcher(settings);

await LoadTestSceneAsync(TestScenePath);

var actual = FindAliveAgentNames();
Assert.That(actual, Is.EquivalentTo(new[] { AgentName }));
var gameObject = GameObject.Find(AgentName);
Assert.That(gameObject, Is.Not.Null);
Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1));
}

[Test]
public async Task DispatchByScene_DispatchFallbackAgent()
{
const string AgentName = "Fallback Agent";
var settings = CreateAutopilotSettings();
settings.fallbackAgent = CreateDoNothingAgent(AgentName);
settings.fallbackAgent = CreateSpyAliveCountAgent(AgentName);
SetUpDispatcher(settings);

await LoadTestSceneAsync(TestScenePath);

var actual = FindAliveAgentNames();
Assert.That(actual, Is.EquivalentTo(new[] { AgentName }));
var gameObject = GameObject.Find(AgentName);
Assert.That(gameObject, Is.Not.Null);
Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1));
}

[Test]
Expand All @@ -126,23 +123,23 @@ public async Task DispatchByScene_NoSceneAgentMapsAndFallbackAgent_AgentIsNotDis

await LoadTestSceneAsync(TestScenePath);

var actual = FindAliveAgentNames();
Assert.That(actual, Is.Empty);
LogAssert.Expect(LogType.Warning, "Agent not found by scene: Buttons");
Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(0));
}

[Test]
public async Task DispatchByScene_DispatchObserverAgent()
{
const string AgentName = "Observer Agent";
var settings = CreateAutopilotSettings();
settings.observerAgent = CreateDoNothingAgent(AgentName);
settings.observerAgent = CreateSpyAliveCountAgent(AgentName);
SetUpDispatcher(settings);

await LoadTestSceneAsync(TestScenePath);

var actual = FindAliveAgentNames();
Assert.That(actual, Is.EquivalentTo(new[] { AgentName }));
var gameObject = GameObject.Find(AgentName);
Assert.That(gameObject, Is.Not.Null);
Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1));
}

[Test]
Expand All @@ -152,22 +149,19 @@ public async Task DispatchByScene_ReActivateScene_NotCreateDuplicateAgents()
var settings = CreateAutopilotSettings();
settings.sceneAgentMaps.Add(new SceneAgentMap
{
scenePath = TestScenePath, agent = CreateDoNothingAgent(AgentName)
scenePath = TestScenePath, agent = CreateSpyAliveCountAgent(AgentName)
});
SetUpDispatcher(settings);

var scene = await LoadTestSceneAsync(TestScenePath);

var agents = FindAliveAgentNames();
Assume.That(agents, Is.EquivalentTo(new[] { AgentName }));
Assume.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1));

var additiveScene = await LoadTestSceneAsync(TestScenePath2, LoadSceneMode.Additive);
SceneManager.SetActiveScene(additiveScene);

SceneManager.SetActiveScene(scene); // Re-activate

var actual = FindAliveAgentNames();
Assert.That(actual, Is.EquivalentTo(new[] { AgentName }));
Assert.That(SpyAliveCountAgent.AliveInstances, Is.EqualTo(1)); // Not create duplicate agents
}
}
}
33 changes: 33 additions & 0 deletions Tests/Runtime/TestDoubles/SpyAliveCountAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2023-2024 DeNA Co., Ltd.
// This software is released under the MIT License.

using System.Threading;
using Cysharp.Threading.Tasks;
using DeNA.Anjin.Agents;
using UnityEngine;

namespace DeNA.Anjin.TestDoubles
{
/// <summary>
/// Spy agent that count the number of alive instances.
/// </summary>
[AddComponentMenu("")]
// ReSharper disable once RequiredBaseTypesIsNotInherited
public class SpyAliveCountAgent : AbstractAgent
{
public static int AliveInstances { get; private set; }

public override async UniTask Run(CancellationToken token)
{
AliveInstances++;
try
{
await UniTask.WaitWhile(() => true, cancellationToken: token); // Wait indefinitely
}
finally
{
AliveInstances--;
}
}
}
}
3 changes: 3 additions & 0 deletions Tests/Runtime/TestDoubles/SpyAliveCountAgent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5cc6137

Please sign in to comment.