Skip to content

Commit

Permalink
Fix includes null and itself agent case
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed May 6, 2024
1 parent bda7489 commit b2f21e2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Runtime/Agents/ParallelCompositeAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Agents/SerialCompositeAgent.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down
52 changes: 52 additions & 0 deletions Tests/Runtime/Agents/ParallelCompositeAgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<ParallelCompositeAgent>();
sut.Logger = new ConsoleLogger(Debug.unityLogger.logHandler);
sut.Random = new RandomFactory(0).CreateRandom();
sut.name = nameof(Run_lifespanPassed_stopAgent);
sut.agents = new List<AbstractAgent>() { 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<ParallelCompositeAgent>();
sut.Logger = new ConsoleLogger(Debug.unityLogger.logHandler);
sut.Random = new RandomFactory(0).CreateRandom();
sut.name = nameof(Run_lifespanPassed_stopAgent);
sut.agents = new List<AbstractAgent>() { 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()");
}
}
}
52 changes: 52 additions & 0 deletions Tests/Runtime/Agents/SerialCompositeAgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<SerialCompositeAgent>();
sut.Logger = new ConsoleLogger(Debug.unityLogger.logHandler);
sut.Random = new RandomFactory(0).CreateRandom();
sut.name = nameof(Run_lifespanPassed_stopAgent);
sut.agents = new List<AbstractAgent>() { 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<SerialCompositeAgent>();
sut.Logger = new ConsoleLogger(Debug.unityLogger.logHandler);
sut.Random = new RandomFactory(0).CreateRandom();
sut.name = nameof(Run_lifespanPassed_stopAgent);
sut.agents = new List<AbstractAgent>() { 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()");
}
}
}

0 comments on commit b2f21e2

Please sign in to comment.