From b2f21e2a8cc96d9e6996cbf472b969164443e088 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Mon, 6 May 2024 12:54:57 +0900 Subject: [PATCH] Fix includes null and itself agent case --- Runtime/Agents/ParallelCompositeAgent.cs | 5 +- Runtime/Agents/SerialCompositeAgent.cs | 3 +- .../Agents/ParallelCompositeAgentTest.cs | 52 +++++++++++++++++++ .../Agents/SerialCompositeAgentTest.cs | 52 +++++++++++++++++++ 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/Runtime/Agents/ParallelCompositeAgent.cs b/Runtime/Agents/ParallelCompositeAgent.cs index de95d2d..9e99f76 100644 --- a/Runtime/Agents/ParallelCompositeAgent.cs +++ b/Runtime/Agents/ParallelCompositeAgent.cs @@ -20,12 +20,13 @@ public override async UniTask Run(CancellationToken token) Logger.Log($"Enter {this.name}.Run()"); var tasks = new UniTask[agents.Count]; + var taskIndex = 0; - foreach (var (agent, i) in agents.Select((agent, i) => (agent, i))) + foreach (var agent in agents.Where(agent => agent != null && agent != this)) { agent.Logger = Logger; agent.Random = RandomFactory.CreateRandom(); - tasks[i] = agent.Run(token); + tasks[taskIndex++] = agent.Run(token); } try diff --git a/Runtime/Agents/SerialCompositeAgent.cs b/Runtime/Agents/SerialCompositeAgent.cs index 5849c23..15ba168 100644 --- a/Runtime/Agents/SerialCompositeAgent.cs +++ b/Runtime/Agents/SerialCompositeAgent.cs @@ -1,6 +1,7 @@ // Copyright (c) 2023 DeNA Co., Ltd. // This software is released under the MIT License. +using System.Linq; using System.Threading; using Cysharp.Threading.Tasks; using UnityEngine; @@ -20,7 +21,7 @@ public override async UniTask Run(CancellationToken token) try { - foreach (var agent in agents) + foreach (var agent in agents.Where(agent => agent != null && agent != this)) { agent.Logger = Logger; agent.Random = RandomFactory.CreateRandom(); diff --git a/Tests/Runtime/Agents/ParallelCompositeAgentTest.cs b/Tests/Runtime/Agents/ParallelCompositeAgentTest.cs index 8a0d652..49632fe 100644 --- a/Tests/Runtime/Agents/ParallelCompositeAgentTest.cs +++ b/Tests/Runtime/Agents/ParallelCompositeAgentTest.cs @@ -12,6 +12,8 @@ using UnityEngine.TestTools; using Object = UnityEngine.Object; +// ReSharper disable MethodSupportsCancellation + namespace DeNA.Anjin.Agents { [UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)] @@ -121,5 +123,55 @@ public async Task Run_setLoggerAndRandomInstanceToChildAgent() LogAssert.Expect(LogType.Log, $"Enter {agent.name}.Run()"); LogAssert.Expect(LogType.Log, $"Exit {agent.name}.Run()"); } + + [Test] + public async Task Run_IncludesNull_IgnoreNull() + { + var firstChildAgent = CreateChildAgent(500); + var lastChildAgent = CreateChildAgent(500); + + var sut = ScriptableObject.CreateInstance(); + sut.Logger = new ConsoleLogger(Debug.unityLogger.logHandler); + sut.Random = new RandomFactory(0).CreateRandom(); + sut.name = nameof(Run_lifespanPassed_stopAgent); + sut.agents = new List() { firstChildAgent, null, lastChildAgent }; + + using (var cancellationTokenSource = new CancellationTokenSource()) + { + var token = cancellationTokenSource.Token; + await sut.Run(token); + } + + Assert.That(firstChildAgent.CompleteCount, Is.EqualTo(1)); + Assert.That(lastChildAgent.CompleteCount, Is.EqualTo(1)); + + LogAssert.Expect(LogType.Log, $"Enter {sut.name}.Run()"); + LogAssert.Expect(LogType.Log, $"Exit {sut.name}.Run()"); + } + + [Test] + public async Task Run_NestingAgent_IgnoreNested() + { + var firstChildAgent = CreateChildAgent(500); + var lastChildAgent = CreateChildAgent(500); + + var sut = ScriptableObject.CreateInstance(); + sut.Logger = new ConsoleLogger(Debug.unityLogger.logHandler); + sut.Random = new RandomFactory(0).CreateRandom(); + sut.name = nameof(Run_lifespanPassed_stopAgent); + sut.agents = new List() { firstChildAgent, sut, lastChildAgent }; + + using (var cancellationTokenSource = new CancellationTokenSource()) + { + var token = cancellationTokenSource.Token; + await sut.Run(token); + } + + Assert.That(firstChildAgent.CompleteCount, Is.EqualTo(1)); + Assert.That(lastChildAgent.CompleteCount, Is.EqualTo(1)); + + LogAssert.Expect(LogType.Log, $"Enter {sut.name}.Run()"); + LogAssert.Expect(LogType.Log, $"Exit {sut.name}.Run()"); + } } } diff --git a/Tests/Runtime/Agents/SerialCompositeAgentTest.cs b/Tests/Runtime/Agents/SerialCompositeAgentTest.cs index 2f4fdda..f1163f6 100644 --- a/Tests/Runtime/Agents/SerialCompositeAgentTest.cs +++ b/Tests/Runtime/Agents/SerialCompositeAgentTest.cs @@ -12,6 +12,8 @@ using UnityEngine.TestTools; using Object = UnityEngine.Object; +// ReSharper disable MethodSupportsCancellation + namespace DeNA.Anjin.Agents { [UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)] @@ -121,5 +123,55 @@ public async Task Run_setLoggerAndRandomInstanceToChildAgent() LogAssert.Expect(LogType.Log, $"Enter {agent.name}.Run()"); LogAssert.Expect(LogType.Log, $"Exit {agent.name}.Run()"); } + + [Test] + public async Task Run_IncludesNull_IgnoreNull() + { + var firstChildAgent = CreateChildAgent(500); + var lastChildAgent = CreateChildAgent(500); + + var sut = ScriptableObject.CreateInstance(); + sut.Logger = new ConsoleLogger(Debug.unityLogger.logHandler); + sut.Random = new RandomFactory(0).CreateRandom(); + sut.name = nameof(Run_lifespanPassed_stopAgent); + sut.agents = new List() { firstChildAgent, null, lastChildAgent }; + + using (var cancellationTokenSource = new CancellationTokenSource()) + { + var token = cancellationTokenSource.Token; + await sut.Run(token); + } + + Assert.That(firstChildAgent.CompleteCount, Is.EqualTo(1)); + Assert.That(lastChildAgent.CompleteCount, Is.EqualTo(1)); + + LogAssert.Expect(LogType.Log, $"Enter {sut.name}.Run()"); + LogAssert.Expect(LogType.Log, $"Exit {sut.name}.Run()"); + } + + [Test] + public async Task Run_NestingAgent_IgnoreNested() + { + var firstChildAgent = CreateChildAgent(500); + var lastChildAgent = CreateChildAgent(500); + + var sut = ScriptableObject.CreateInstance(); + sut.Logger = new ConsoleLogger(Debug.unityLogger.logHandler); + sut.Random = new RandomFactory(0).CreateRandom(); + sut.name = nameof(Run_lifespanPassed_stopAgent); + sut.agents = new List() { firstChildAgent, sut, lastChildAgent }; + + using (var cancellationTokenSource = new CancellationTokenSource()) + { + var token = cancellationTokenSource.Token; + await sut.Run(token); + } + + Assert.That(firstChildAgent.CompleteCount, Is.EqualTo(1)); + Assert.That(lastChildAgent.CompleteCount, Is.EqualTo(1)); + + LogAssert.Expect(LogType.Log, $"Enter {sut.name}.Run()"); + LogAssert.Expect(LogType.Log, $"Exit {sut.name}.Run()"); + } } }