<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Caching;
using Foundatio.Extensions;
using Foundatio.Jobs;
using Foundatio.Metrics;
using Foundatio.Queues;
using Foundatio.ServiceProviders;
using Foundatio.Tests.Utility;
using Xunit;
using Xunit.Abstractions;

namespace Foundatio.Tests.Jobs {
    public class JobTests : CaptureTests {
        private readonly TextWriter _writer;

        public JobTests(CaptureFixture fixture, ITestOutputHelper output) : base(fixture, output) {
            _writer = new TestOutputWriter(output);
        }

        [Fact]
        public void CanCancelJob()
        {
            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            var result = JobRunner.RunAsync(new JobRunOptions
            {
                JobTypeName = typeof(HelloWorldJob).AssemblyQualifiedName,
                InstanceCount = 1,
                Interval = null,
                RunContinuous = true
            }, cts.Token).Result;

            Assert.Equal(0, result);
        }

        [Fact]
        public async Task CanRunJobs() {
            var job = new HelloWorldJob();
            Assert.Equal(0, job.RunCount);
            await job.RunAsync().AnyContext();
            Assert.Equal(1, job.RunCount);

            await job.RunContinuousAsync(iterationLimit: 2).AnyContext();
            Assert.Equal(3, job.RunCount);

            var sw = new Stopwatch();
            sw.Start();
            await job.RunContinuousAsync(cancellationToken: new CancellationTokenSource(TimeSpan.FromMilliseconds(100)).Token).AnyContext();
            sw.Stop();
            Assert.InRange(sw.Elapsed, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150));

            var jobInstance = JobRunner.CreateJobInstance(typeof(HelloWorldJob).AssemblyQualifiedName);
            Assert.NotNull(jobInstance);
            Assert.Equal(0, ((HelloWorldJob)jobInstance).RunCount);
            Assert.Equal(JobResult.Success, await jobInstance.RunAsync().AnyContext());
            Assert.Equal(1, ((HelloWorldJob)jobInstance).RunCount);
        }

        [Fact]
        public async void CanRunMultipleInstances() {
            HelloWorldJob.GlobalRunCount = 0;
            
            var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            await JobRunner.RunContinuousAsync(typeof(HelloWorldJob), null, 5, 1, tokenSource.Token).AnyContext();
            Assert.Equal(5, HelloWorldJob.GlobalRunCount);

            HelloWorldJob.GlobalRunCount = 0;

            tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            await JobRunner.RunContinuousAsync(typeof(HelloWorldJob), null, 100, 5, tokenSource.Token).AnyContext();
            Assert.Equal(500, HelloWorldJob.GlobalRunCount);
        }

        [Fact]
        public async void CanCancelContinuousJobs() {
            var job = new HelloWorldJob();
            var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
            await job.RunContinuousAsync(TimeSpan.FromSeconds(1), 5, tokenSource.Token).AnyContext();
            Assert.Equal(1, job.RunCount);

            tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));

                await Assert.ThrowsAsync<TaskCanceledException>(async () => await JobRunner.RunContinuousAsync(typeof(HelloWorldJob), instanceCount: 5, iterationLimit: 10000, cancellationToken: tokenSource.Token, interval: TimeSpan.FromMilliseconds(1)).AnyContext());
        }

        [Fact]
        public async Task CanRunJobsWithLocks() {
            var job = new WithLockingJob();
            Assert.Equal(0, job.RunCount);
            await job.RunAsync().AnyContext();
            Assert.Equal(1, job.RunCount);

            await job.RunContinuousAsync(iterationLimit: 2).AnyContext();
            Assert.Equal(3, job.RunCount);

            Task.Run(async () => await job.RunAsync().AnyContext()).AnyContext();
            Task.Run(async () => await job.RunAsync().AnyContext()).AnyContext();
            Thread.Sleep(200);
            Assert.Equal(4, job.RunCount);
        }

        [Fact]
        public async void CanRunThrottledJobs() {
            var client = new InMemoryCacheClient();
            var jobs = new List<ThrottledJob>(new[] {
                new ThrottledJob(client),
                new ThrottledJob(client),
                new ThrottledJob(client)
            });

            var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            await Task.WhenAll(jobs.Select(async job => await job.RunContinuousAsync(TimeSpan.FromMilliseconds(1), cancellationToken: tokenSource.Token).AnyContext())).AnyContext();<commit_msg>Fixed breaking unit test<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Caching;
using Foundatio.Extensions;
using Foundatio.Jobs;
using Foundatio.Metrics;
using Foundatio.Queues;
using Foundatio.ServiceProviders;
using Foundatio.Tests.Utility;
using Xunit;
using Xunit.Abstractions;

namespace Foundatio.Tests.Jobs {
    public class JobTests : CaptureTests {
        private readonly TextWriter _writer;

        public JobTests(CaptureFixture fixture, ITestOutputHelper output) : base(fixture, output) {
            _writer = new TestOutputWriter(output);
        }

        [Fact]
        public void CanCancelJob()
        {
            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            var result = JobRunner.RunAsync(new JobRunOptions
            {
                JobTypeName = typeof(HelloWorldJob).AssemblyQualifiedName,
                InstanceCount = 1,
                Interval = null,
                RunContinuous = true
            }, cts.Token).Result;

            Assert.Equal(0, result);
        }

        [Fact]
        public async Task CanRunJobs() {
            var job = new HelloWorldJob();
            Assert.Equal(0, job.RunCount);
            await job.RunAsync().AnyContext();
            Assert.Equal(1, job.RunCount);

            await job.RunContinuousAsync(iterationLimit: 2).AnyContext();
            Assert.Equal(3, job.RunCount);

            var sw = new Stopwatch();
            sw.Start();
            await job.RunContinuousAsync(cancellationToken: new CancellationTokenSource(TimeSpan.FromMilliseconds(100)).Token).AnyContext();
            sw.Stop();
            Assert.InRange(sw.Elapsed, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150));

            var jobInstance = JobRunner.CreateJobInstance(typeof(HelloWorldJob).AssemblyQualifiedName);
            Assert.NotNull(jobInstance);
            Assert.Equal(0, ((HelloWorldJob)jobInstance).RunCount);
            Assert.Equal(JobResult.Success, await jobInstance.RunAsync().AnyContext());
            Assert.Equal(1, ((HelloWorldJob)jobInstance).RunCount);
        }

        [Fact]
        public async void CanRunMultipleInstances() {
            HelloWorldJob.GlobalRunCount = 0;
            
            var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            await JobRunner.RunContinuousAsync(typeof(HelloWorldJob), null, 5, 1, tokenSource.Token).AnyContext();
            Assert.Equal(5, HelloWorldJob.GlobalRunCount);

            HelloWorldJob.GlobalRunCount = 0;

            tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            await JobRunner.RunContinuousAsync(typeof(HelloWorldJob), null, 100, 5, tokenSource.Token).AnyContext();
            Assert.Equal(500, HelloWorldJob.GlobalRunCount);
        }

        [Fact]
        public async void CanCancelContinuousJobs() {
            var job = new HelloWorldJob();
            var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
            await job.RunContinuousAsync(TimeSpan.FromSeconds(1), 5, tokenSource.Token).AnyContext();
            Assert.Equal(1, job.RunCount);

            tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
            await JobRunner.RunContinuousAsync(typeof(HelloWorldJob), instanceCount: 5, iterationLimit: 10000, cancellationToken: tokenSource.Token, interval: TimeSpan.FromMilliseconds(1)).AnyContext();
        }

        [Fact]
        public async Task CanRunJobsWithLocks() {
            var job = new WithLockingJob();
            Assert.Equal(0, job.RunCount);
            await job.RunAsync().AnyContext();
            Assert.Equal(1, job.RunCount);

            await job.RunContinuousAsync(iterationLimit: 2).AnyContext();
            Assert.Equal(3, job.RunCount);

            Task.Run(async () => await job.RunAsync().AnyContext()).AnyContext();
            Task.Run(async () => await job.RunAsync().AnyContext()).AnyContext();
            Thread.Sleep(200);
            Assert.Equal(4, job.RunCount);
        }

        [Fact]
        public async void CanRunThrottledJobs() {
            var client = new InMemoryCacheClient();
            var jobs = new List<ThrottledJob>(new[] {
                new ThrottledJob(client),
                new ThrottledJob(client),
                new ThrottledJob(client)
            });

            var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            await Task.WhenAll(jobs.Select(async job => await job.RunContinuousAsync(TimeSpan.FromMilliseconds(1), cancellationToken: tokenSource.Token).AnyContext())).AnyContext();
{'fraction_non_alphanumeric': 0.10357698289269052, 'fraction_numerical': 0.012026956972524623, 'mean_word_length': 2.6181545386346587, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 13, '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': '1859508', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2545, 'n_words': 488}