<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using ErgastApi.Client.Caching;
using ErgastApi.Responses;
using FluentAssertions;
using Xunit;

namespace ErgastApi.Tests.Client.Caching
{
    public class ErgastMemoryCacheCleanupWorkerTests
    {
        private IDictionary<string, CacheEntry<ErgastResponse>> Cache { get; }

        public ErgastMemoryCacheCleanupWorkerTests()
        {
            Cache = new ConcurrentDictionary<string, CacheEntry<ErgastResponse>>();
        }

        [Theory]
        [AutoMockedData]
        public async Task RemovesExpiredItemsContinously(string key1, CacheEntry<ErgastResponse> entry1, string key2, CacheEntry<ErgastResponse> entry2)
        {
            // Arrange
            entry1.Expiration = DateTime.Now.AddDays(-1);
            entry2.Expiration = DateTime.Now.AddDays(1);
            Cache.Add(key1, entry1);
            Cache.Add(key2, entry2);

            // Act
            var worker = CreateCleanupWorker(TimeSpan.FromMilliseconds(10));
            await Task.Delay(50);

            // Assert
            Cache.Should().HaveCount(1);

            entry2.Expiration = DateTime.Now.AddDays(-1);
            await Task.Delay(50);

            Cache.Should().BeEmpty();
            worker.CleanupTask.IsCompleted.Should().BeFalse();
        }

        [Fact]
        public void Dispose_StopsCleanupTask()
        {
            // Arrange
            var worker = CreateCleanupWorker(TimeSpan.FromDays(1));

            // Act
            worker.Dispose();

            // Assert
            worker.CleanupTask.IsCompleted.Should().BeTrue();
        }

        [Fact]
        public async Task Stop_StopsCleanupTask()
        {
            // Arrange
            var worker = CreateCleanupWorker(TimeSpan.FromDays(1));

            // Act
            worker.Stop();
            await Task.WhenAny(worker.CleanupTask, Task.Delay(100));

            // Assert
            worker.CleanupTask.IsCompleted.Should().BeTrue();
        }

        [Fact]
        public void Stop_CanBeCalledMultipleTimesWithoutThrowingException()
        {
            // Arrange
            var worker = CreateCleanupWorker(TimeSpan.FromDays(1));

            // Act
            Action act = () =>
            {
                worker.Stop();
                worker.Stop();
                worker.Stop();
            };

            // Assert
            act.ShouldNotThrow();
        }

        [Fact]
        public async Task CanBeStartedAndStoppedMultipleTimes()
        {
            // Arrange
            var worker = CreateCleanupWorker(TimeSpan.FromDays(1));

            // Act
            worker.Start();
            worker.Stop();
            worker.Start();
            worker.Stop();
            worker.Start();

            // Assert
            worker.CleanupTask.IsCompleted.Should().BeFalse();
        }<commit_msg>Make non-async unit test return void<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using ErgastApi.Client.Caching;
using ErgastApi.Responses;
using FluentAssertions;
using Xunit;

namespace ErgastApi.Tests.Client.Caching
{
    public class ErgastMemoryCacheCleanupWorkerTests
    {
        private IDictionary<string, CacheEntry<ErgastResponse>> Cache { get; }

        public ErgastMemoryCacheCleanupWorkerTests()
        {
            Cache = new ConcurrentDictionary<string, CacheEntry<ErgastResponse>>();
        }

        [Theory]
        [AutoMockedData]
        public async Task RemovesExpiredItemsContinously(string key1, CacheEntry<ErgastResponse> entry1, string key2, CacheEntry<ErgastResponse> entry2)
        {
            // Arrange
            entry1.Expiration = DateTime.Now.AddDays(-1);
            entry2.Expiration = DateTime.Now.AddDays(1);
            Cache.Add(key1, entry1);
            Cache.Add(key2, entry2);

            // Act
            var worker = CreateCleanupWorker(TimeSpan.FromMilliseconds(10));
            await Task.Delay(50);

            // Assert
            Cache.Should().HaveCount(1);

            entry2.Expiration = DateTime.Now.AddDays(-1);
            await Task.Delay(50);

            Cache.Should().BeEmpty();
            worker.CleanupTask.IsCompleted.Should().BeFalse();
        }

        [Fact]
        public void Dispose_StopsCleanupTask()
        {
            // Arrange
            var worker = CreateCleanupWorker(TimeSpan.FromDays(1));

            // Act
            worker.Dispose();

            // Assert
            worker.CleanupTask.IsCompleted.Should().BeTrue();
        }

        [Fact]
        public async Task Stop_StopsCleanupTask()
        {
            // Arrange
            var worker = CreateCleanupWorker(TimeSpan.FromDays(1));

            // Act
            worker.Stop();
            await Task.WhenAny(worker.CleanupTask, Task.Delay(100));

            // Assert
            worker.CleanupTask.IsCompleted.Should().BeTrue();
        }

        [Fact]
        public void Stop_CanBeCalledMultipleTimesWithoutThrowingException()
        {
            // Arrange
            var worker = CreateCleanupWorker(TimeSpan.FromDays(1));

            // Act
            Action act = () =>
            {
                worker.Stop();
                worker.Stop();
                worker.Stop();
            };

            // Assert
            act.ShouldNotThrow();
        }

        [Fact]
        public void CanBeStartedAndStoppedMultipleTimes()
        {
            // Arrange
            var worker = CreateCleanupWorker(TimeSpan.FromDays(1));

            // Act
            worker.Start();
            worker.Stop();
            worker.Start();
            worker.Stop();
            worker.Start();

            // Assert
            worker.CleanupTask.IsCompleted.Should().BeFalse();
        }
{'fraction_non_alphanumeric': 0.10255110660584559, 'fraction_numerical': 0.009461057611082953, 'mean_word_length': 1.6933575978161965, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3837456', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1541, 'n_words': 262}