<commit_before>using System;
using System.Threading.Tasks;
using Akka.Actor;
using GridDomain.Common;
using GridDomain.Node.Actors.EventSourced.Messages;
using GridDomain.Node.Actors.PersistentHub;
using Xunit;
using Xunit.Abstractions;

namespace GridDomain.Tests.Unit.Aggregate_actor_lifetime
{
    public abstract class PersistentHubChildrenLifetimeTest : NodeTestKit
    {
        public PersistentHubChildrenLifetimeTest(ITestOutputHelper output, PersistentHubFixture fixture)
            : base(output, fixture)
        {
            Infrastructure = fixture.Infrastructure;
            var actorOfAsTestActorRef = ActorOfAsTestActorRef<PersistentHubActor>(Infrastructure.CreateHubProps(Node.System),
                                                                                  "TestHub_" + Guid.NewGuid());
            Hub = actorOfAsTestActorRef.UnderlyingActor;
            HubRef = actorOfAsTestActorRef.Ref;
        }

        private IPersistentActorTestsInfrastructure Infrastructure { get; }

        private async Task When_hub_creates_a_child()
        {
            HubRef.Tell(Infrastructure.ChildCreateMessage);
            await Task.Delay(200);
        }

        private async Task And_it_is_not_active_until_lifetime_period_is_expired()
        {
            await Task.Delay(5000);
        }

        private async Task And_command_for_child_is_sent()
        {
            HubRef.Tell(Infrastructure.ChildActivateMessage);
            await Task.Delay(200);
        }

        private readonly PersistentHubActor Hub;
        private readonly IActorRef HubRef;
        private ChildInfo Child => Hub.Children[Infrastructure.ChildId];

        private async Task<HealthStatus> PingChild(string payload)
        {
            return await Child.Ref.Ask(new CheckHealth(payload), TimeSpan.FromSeconds(1)) as HealthStatus;
        }

        [Fact]
        public async Task PersistenceHubOnChildCreation()
        {
            //When_Hub_creates_a_child()
            await When_hub_creates_a_child();
            //Time_to_life_is_set_and_can_differs_by_check_time()
            Assert.True(Child.ExpiresAt - BusinessDateTime.UtcNow >= Hub.ChildMaxInactiveTime - Hub.ChildClearPeriod);
            //Hub_should_contains_child()
            Assert.True(Hub.Children.ContainsKey(Infrastructure.ChildId));
            //Time_to_life_is_limited()
            Assert.True(Child.ExpiresAt - BusinessDateTime.UtcNow <= Hub.ChildMaxInactiveTime);
        }

        [Fact]
        public async Task When_child_became_inactive_too_long()
        {
            await When_hub_creates_a_child();
            var actorToWatch = Child.Ref;
            Watch(actorToWatch);
            await And_it_is_not_active_until_lifetime_period_is_expired();
            //It_should_be_removed_from_hub()
            Assert.False(Hub.Children.ContainsKey(Infrastructure.ChildId));
            //It_should_be_terminated()
            FishForMessage<Terminated>(m => m.ActorRef.Path == actorToWatch.Path);
        }

        [Fact]
        public async Task When_child_has_activity_while_living_Then_it_should_exist()
        {
            await When_hub_creates_a_child();
            var initialTimeToLife = Child.ExpiresAt;

            //TODO: replace sleep with dateTime manipulations via DateTimeFacade
            await Task.Delay(200);

            await And_command_for_child_is_sent();<commit_msg>wait for terminated message first<commit_after>using System;
using System.Threading.Tasks;
using Akka.Actor;
using GridDomain.Common;
using GridDomain.Node.Actors.EventSourced.Messages;
using GridDomain.Node.Actors.PersistentHub;
using Xunit;
using Xunit.Abstractions;

namespace GridDomain.Tests.Unit.Aggregate_actor_lifetime
{
    public abstract class PersistentHubChildrenLifetimeTest : NodeTestKit
    {
        public PersistentHubChildrenLifetimeTest(ITestOutputHelper output, PersistentHubFixture fixture)
            : base(output, fixture)
        {
            Infrastructure = fixture.Infrastructure;
            var actorOfAsTestActorRef = ActorOfAsTestActorRef<PersistentHubActor>(Infrastructure.CreateHubProps(Node.System),
                                                                                  "TestHub_" + Guid.NewGuid());
            Hub = actorOfAsTestActorRef.UnderlyingActor;
            HubRef = actorOfAsTestActorRef.Ref;
        }

        private IPersistentActorTestsInfrastructure Infrastructure { get; }

        private async Task When_hub_creates_a_child()
        {
            HubRef.Tell(Infrastructure.ChildCreateMessage);
            await Task.Delay(200);
        }

        private async Task And_it_is_not_active_until_lifetime_period_is_expired()
        {
            await Task.Delay(5000);
        }

        private async Task And_command_for_child_is_sent()
        {
            HubRef.Tell(Infrastructure.ChildActivateMessage);
            await Task.Delay(200);
        }

        private readonly PersistentHubActor Hub;
        private readonly IActorRef HubRef;
        private ChildInfo Child => Hub.Children[Infrastructure.ChildId];

        private async Task<HealthStatus> PingChild(string payload)
        {
            return await Child.Ref.Ask(new CheckHealth(payload), TimeSpan.FromSeconds(1)) as HealthStatus;
        }

        [Fact]
        public async Task PersistenceHubOnChildCreation()
        {
            //When_Hub_creates_a_child()
            await When_hub_creates_a_child();
            //Time_to_life_is_set_and_can_differs_by_check_time()
            Assert.True(Child.ExpiresAt - BusinessDateTime.UtcNow >= Hub.ChildMaxInactiveTime - Hub.ChildClearPeriod);
            //Hub_should_contains_child()
            Assert.True(Hub.Children.ContainsKey(Infrastructure.ChildId));
            //Time_to_life_is_limited()
            Assert.True(Child.ExpiresAt - BusinessDateTime.UtcNow <= Hub.ChildMaxInactiveTime);
        }

        [Fact]
        public async Task When_child_became_inactive_too_long()
        {
            await When_hub_creates_a_child();
            var actorToWatch = Child.Ref;
            Watch(actorToWatch);
            await And_it_is_not_active_until_lifetime_period_is_expired();
            //It_should_be_terminated()
            FishForMessage<Terminated>(m => m.ActorRef.Path == actorToWatch.Path);
            //It_should_be_removed_from_hub()
            Assert.False(Hub.Children.ContainsKey(Infrastructure.ChildId));

        }

        [Fact]
        public async Task When_child_has_activity_while_living_Then_it_should_exist()
        {
            await When_hub_creates_a_child();
            var initialTimeToLife = Child.ExpiresAt;

            //TODO: replace sleep with dateTime manipulations via DateTimeFacade
            await Task.Delay(200);

            await And_command_for_child_is_sent();
{'fraction_non_alphanumeric': 0.07338108463674901, 'fraction_numerical': 0.00409296886420114, 'mean_word_length': 2.469574036511156, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4639109', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1852, 'n_words': 301}