Skip to content

Commit

Permalink
Fix sub states disposing
Browse files Browse the repository at this point in the history
  • Loading branch information
bazyleu committed Nov 12, 2024
1 parent c979a94 commit 825226c
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Assets/UniState/Runtime/Core/State/CompositeStateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@ public override void SetTransitionFacade(IStateTransitionFacade transitionFacade
base.SetTransitionFacade(transitionFacade);
_subStatesContainer.SetTransitionFacade(transitionFacade);
}

public override void Dispose()
{
base.Dispose();
_subStatesContainer.Dispose();
}
}
}
3 changes: 3 additions & 0 deletions Assets/UniStateTests/PlayMode/SubStateTests.meta

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UniState;

namespace UniStateTests.PlayMode.SubStateTests.Infrastructure
{
internal class StateFinal : DefaultCompositeState
{
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UniState;

namespace UniStateTests.PlayMode.SubStateTests.Infrastructure
{
internal class StateInitial : DefaultCompositeState
{
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using UniState;
using UniStateTests.Common;

namespace UniStateTests.PlayMode.SubStateTests.Infrastructure
{
public class StateMachineSubStates : VerifiableStateMachine
{
public StateMachineSubStates(ExecutionLogger logger) : base(logger)
{
}

protected override StateTransitionInfo BuildRecoveryTransition(IStateTransitionFactory transitionFactory)
=> transitionFactory.CreateExitTransition();

protected override void HandleError(StateMachineErrorData errorData)
{
}

protected override string ExpectedLog =>
"SubStateInitialSecond (Execute) -> SubStateInitialFirst (Execute, Disposables) -> SubStateInitialSecond (Disposables) -> " +
"SubStateFinalSecond (Execute) -> SubStateFinalFirst (Execute, Disposables) -> SubStateFinalSecond (Disposables)";
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using UniState;
using UniStateTests.Common;

namespace UniStateTests.PlayMode.SubStateTests.Infrastructure
{
internal class SubStateFinalFirst : SubStateBase<StateFinal>
{
private readonly ExecutionLogger _logger;

public SubStateFinalFirst(ExecutionLogger logger)
{
_logger = logger;
}

public override async UniTask<StateTransitionInfo> Execute(CancellationToken token)
{
Disposables.Add(() => { _logger.LogStep("SubStateFinalFirst", "Disposables"); });

await UniTask.Yield(token);

_logger.LogStep("SubStateFinalFirst", "Execute");

await UniTask.Yield(token);
await UniTask.Yield(token);
await UniTask.Yield(token);
await UniTask.Yield(token);

return Transition.GoBack();
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using UniState;
using UniStateTests.Common;

namespace UniStateTests.PlayMode.SubStateTests.Infrastructure
{
internal class SubStateFinalSecond : SubStateBase<StateFinal>
{
private readonly ExecutionLogger _logger;

public SubStateFinalSecond(ExecutionLogger logger)
{
_logger = logger;
}

public override async UniTask<StateTransitionInfo> Execute(CancellationToken token)
{
Disposables.Add(() => { _logger.LogStep("SubStateFinalSecond", "Disposables"); });

_logger.LogStep("SubStateFinalSecond", "Execute");

await UniTask.Yield(token);
await UniTask.Yield(token);

throw new Exception("SubStateFinalSecond exception");
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using UniState;
using UniStateTests.Common;

namespace UniStateTests.PlayMode.SubStateTests.Infrastructure
{
internal class SubStateInitialFirst : SubStateBase<StateInitial>
{
private readonly ExecutionLogger _logger;

public SubStateInitialFirst(ExecutionLogger logger)
{
_logger = logger;
}

public override async UniTask<StateTransitionInfo> Execute(CancellationToken token)
{
Disposables.Add(() => { _logger.LogStep("SubStateInitialFirst", "Disposables"); });

await UniTask.Yield(token);

_logger.LogStep("SubStateInitialFirst", "Execute");

await UniTask.Yield(token);
await UniTask.Yield(token);
await UniTask.Yield(token);

return Transition.GoToExit();
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using UniState;
using UniStateTests.Common;

namespace UniStateTests.PlayMode.SubStateTests.Infrastructure
{
internal class SubStateInitialSecond: SubStateBase<StateInitial>
{
private readonly ExecutionLogger _logger;

public SubStateInitialSecond(ExecutionLogger logger)
{
_logger = logger;
}

public override async UniTask<StateTransitionInfo> Execute(CancellationToken token)
{
Disposables.Add(() => { _logger.LogStep("SubStateInitialSecond", "Disposables"); });

_logger.LogStep("SubStateInitialSecond", "Execute");

await UniTask.Yield(token);
await UniTask.Yield(token);

return Transition.GoTo<StateFinal>();
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using UniState;
using UniStateTests.Common;
using UniStateTests.PlayMode.HistoryTests.Infrastructure;
using UniStateTests.PlayMode.SubStateTests.Infrastructure;
using UnityEngine.TestTools;
using VContainer;
using Zenject;

namespace UniStateTests.PlayMode.SubStateTests
{
[TestFixture]
public class SubStateVContainerTests : VContainerTestsBase
{
[UnityTest]
public IEnumerator RunChaneOfStateSubStates_ExeptionRisedInSubState_AllSubStateDisposed() =>
UniTask.ToCoroutine(async () => { await RunAndVerify<StateMachineSubStates, StateInitial>(); });

protected override void SetupBindings(IContainerBuilder builder)
{
base.SetupBindings(builder);

builder.RegisterStateMachine<StateMachineSubStates>();

builder.RegisterState<StateInitial>();
builder.RegisterState<StateFinal>();
builder.RegisterState<SubStateInitialFirst>();
builder.RegisterState<SubStateInitialSecond>();
builder.RegisterState<SubStateFinalFirst>();
builder.RegisterState<SubStateFinalSecond>();
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using UniState;
using UniStateTests.Common;
using UniStateTests.PlayMode.HistoryTests.Infrastructure;
using UniStateTests.PlayMode.SubStateTests.Infrastructure;
using UnityEngine.TestTools;
using Zenject;

namespace UniStateTests.PlayMode.SubStateTests
{
[TestFixture]
public class SubStateZenjectTests : ZenjectTestsBase
{
[UnityTest]
public IEnumerator RunChaneOfStateSubStates_ExeptionRisedInSubState_AllSubStateDisposed() =>
UniTask.ToCoroutine(async () => { await RunAndVerify<StateMachineSubStates, StateInitial>(); });

protected override void SetupBindings(DiContainer container)
{
base.SetupBindings(container);

container.BindStateMachine<StateMachineSubStates>();

container.BindState<StateInitial>();
container.BindState<StateFinal>();
container.BindState<SubStateInitialFirst>();
container.BindState<SubStateInitialSecond>();
container.BindState<SubStateFinalFirst>();
container.BindState<SubStateFinalSecond>();
}
}
}

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

0 comments on commit 825226c

Please sign in to comment.