From 825226c31015913354e379883bf1b5fbc5b0a9de Mon Sep 17 00:00:00 2001 From: Dzmitry Bazyleu Date: Tue, 12 Nov 2024 15:33:06 +0100 Subject: [PATCH] Fix sub states disposing --- .../Runtime/Core/State/CompositeStateBase.cs | 6 ++++ .../UniStateTests/PlayMode/SubStateTests.meta | 3 ++ .../SubStateTests/Infrastructure.meta | 3 ++ .../Infrastructure/StateFinal.cs | 8 +++++ .../Infrastructure/StateFinal.cs.meta | 3 ++ .../Infrastructure/StateInitial.cs | 8 +++++ .../Infrastructure/StateInitial.cs.meta | 3 ++ .../Infrastructure/StateMachineSubStates.cs | 23 ++++++++++++ .../StateMachineSubStates.cs.meta | 3 ++ .../Infrastructure/SubStateFinalFirst.cs | 33 +++++++++++++++++ .../Infrastructure/SubStateFinalFirst.cs.meta | 3 ++ .../Infrastructure/SubStateFinalSecond.cs | 30 ++++++++++++++++ .../SubStateFinalSecond.cs.meta | 3 ++ .../Infrastructure/SubStateInitialFirst.cs | 32 +++++++++++++++++ .../SubStateInitialFirst.cs.meta | 3 ++ .../Infrastructure/SubStateInitialSecond.cs | 29 +++++++++++++++ .../SubStateInitialSecond.cs.meta | 3 ++ .../SubStateTests/SubStateVContainerTests.cs | 35 +++++++++++++++++++ .../SubStateVContainerTests.cs.meta | 3 ++ .../SubStateTests/SubStateZenjectTests.cs | 34 ++++++++++++++++++ .../SubStateZenjectTests.cs.meta | 3 ++ 21 files changed, 271 insertions(+) create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateFinal.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateFinal.cs.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateInitial.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateInitial.cs.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateMachineSubStates.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateMachineSubStates.cs.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalFirst.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalFirst.cs.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalSecond.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalSecond.cs.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialFirst.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialFirst.cs.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialSecond.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialSecond.cs.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/SubStateVContainerTests.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/SubStateVContainerTests.cs.meta create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/SubStateZenjectTests.cs create mode 100644 Assets/UniStateTests/PlayMode/SubStateTests/SubStateZenjectTests.cs.meta diff --git a/Assets/UniState/Runtime/Core/State/CompositeStateBase.cs b/Assets/UniState/Runtime/Core/State/CompositeStateBase.cs index 6f8cdf7..f1b8421 100644 --- a/Assets/UniState/Runtime/Core/State/CompositeStateBase.cs +++ b/Assets/UniState/Runtime/Core/State/CompositeStateBase.cs @@ -42,5 +42,11 @@ public override void SetTransitionFacade(IStateTransitionFacade transitionFacade base.SetTransitionFacade(transitionFacade); _subStatesContainer.SetTransitionFacade(transitionFacade); } + + public override void Dispose() + { + base.Dispose(); + _subStatesContainer.Dispose(); + } } } \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests.meta b/Assets/UniStateTests/PlayMode/SubStateTests.meta new file mode 100644 index 0000000..1019c70 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c7e27f2d21db437197cff12e00705552 +timeCreated: 1731419435 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure.meta b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure.meta new file mode 100644 index 0000000..982eb20 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 47263778b70c4e6da1ee0419561d5c60 +timeCreated: 1731419657 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateFinal.cs b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateFinal.cs new file mode 100644 index 0000000..8987da3 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateFinal.cs @@ -0,0 +1,8 @@ +using UniState; + +namespace UniStateTests.PlayMode.SubStateTests.Infrastructure +{ + internal class StateFinal : DefaultCompositeState + { + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateFinal.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateFinal.cs.meta new file mode 100644 index 0000000..4311593 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateFinal.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c33c79efb5b54312831ce8f21ff6e288 +timeCreated: 1731419871 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateInitial.cs b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateInitial.cs new file mode 100644 index 0000000..9ba8cb2 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateInitial.cs @@ -0,0 +1,8 @@ +using UniState; + +namespace UniStateTests.PlayMode.SubStateTests.Infrastructure +{ + internal class StateInitial : DefaultCompositeState + { + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateInitial.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateInitial.cs.meta new file mode 100644 index 0000000..18c621b --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateInitial.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8e14b643a69041b38a051f186994f9ba +timeCreated: 1731419751 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateMachineSubStates.cs b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateMachineSubStates.cs new file mode 100644 index 0000000..8010c90 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateMachineSubStates.cs @@ -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)"; + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateMachineSubStates.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateMachineSubStates.cs.meta new file mode 100644 index 0000000..ecf2c57 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/StateMachineSubStates.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6cff555c1f93498d8c266b3889c4821f +timeCreated: 1731420489 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalFirst.cs b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalFirst.cs new file mode 100644 index 0000000..230f0ea --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalFirst.cs @@ -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 + { + private readonly ExecutionLogger _logger; + + public SubStateFinalFirst(ExecutionLogger logger) + { + _logger = logger; + } + + public override async UniTask 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(); + } + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalFirst.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalFirst.cs.meta new file mode 100644 index 0000000..777df28 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalFirst.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 661cde3d026c48b8a9693c10ea79eed3 +timeCreated: 1731419886 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalSecond.cs b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalSecond.cs new file mode 100644 index 0000000..938a676 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalSecond.cs @@ -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 + { + private readonly ExecutionLogger _logger; + + public SubStateFinalSecond(ExecutionLogger logger) + { + _logger = logger; + } + + public override async UniTask 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"); + } + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalSecond.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalSecond.cs.meta new file mode 100644 index 0000000..0c3393c --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateFinalSecond.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 01ce718c903c413eb6ae709992482791 +timeCreated: 1731419906 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialFirst.cs b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialFirst.cs new file mode 100644 index 0000000..bd88806 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialFirst.cs @@ -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 + { + private readonly ExecutionLogger _logger; + + public SubStateInitialFirst(ExecutionLogger logger) + { + _logger = logger; + } + + public override async UniTask 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(); + } + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialFirst.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialFirst.cs.meta new file mode 100644 index 0000000..f6ee915 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialFirst.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9802a3f36cf244cf8ff9d9e1d63f9a0a +timeCreated: 1731419827 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialSecond.cs b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialSecond.cs new file mode 100644 index 0000000..39247b2 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialSecond.cs @@ -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 + { + private readonly ExecutionLogger _logger; + + public SubStateInitialSecond(ExecutionLogger logger) + { + _logger = logger; + } + + public override async UniTask Execute(CancellationToken token) + { + Disposables.Add(() => { _logger.LogStep("SubStateInitialSecond", "Disposables"); }); + + _logger.LogStep("SubStateInitialSecond", "Execute"); + + await UniTask.Yield(token); + await UniTask.Yield(token); + + return Transition.GoTo(); + } + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialSecond.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialSecond.cs.meta new file mode 100644 index 0000000..a935160 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/Infrastructure/SubStateInitialSecond.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d72102dd4f994e95b9047bb9525b097b +timeCreated: 1731419838 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/SubStateVContainerTests.cs b/Assets/UniStateTests/PlayMode/SubStateTests/SubStateVContainerTests.cs new file mode 100644 index 0000000..d948be1 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/SubStateVContainerTests.cs @@ -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(); }); + + protected override void SetupBindings(IContainerBuilder builder) + { + base.SetupBindings(builder); + + builder.RegisterStateMachine(); + + builder.RegisterState(); + builder.RegisterState(); + builder.RegisterState(); + builder.RegisterState(); + builder.RegisterState(); + builder.RegisterState(); + } + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/SubStateVContainerTests.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/SubStateVContainerTests.cs.meta new file mode 100644 index 0000000..a7ac23d --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/SubStateVContainerTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4aedd72b10b048b4879f70b474246077 +timeCreated: 1731421866 \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/SubStateZenjectTests.cs b/Assets/UniStateTests/PlayMode/SubStateTests/SubStateZenjectTests.cs new file mode 100644 index 0000000..4087344 --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/SubStateZenjectTests.cs @@ -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(); }); + + protected override void SetupBindings(DiContainer container) + { + base.SetupBindings(container); + + container.BindStateMachine(); + + container.BindState(); + container.BindState(); + container.BindState(); + container.BindState(); + container.BindState(); + container.BindState(); + } + } +} \ No newline at end of file diff --git a/Assets/UniStateTests/PlayMode/SubStateTests/SubStateZenjectTests.cs.meta b/Assets/UniStateTests/PlayMode/SubStateTests/SubStateZenjectTests.cs.meta new file mode 100644 index 0000000..9d2c07e --- /dev/null +++ b/Assets/UniStateTests/PlayMode/SubStateTests/SubStateZenjectTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9029c42c783849b781ce44f3532e5bc2 +timeCreated: 1731420745 \ No newline at end of file