Beispiel #1
0
        public async Task CanRunJobs()
        {
            var job = new HelloWorldJob();

            Assert.Equal(0, job.RunCount);
            await job.RunAsync();

            Assert.Equal(1, job.RunCount);

            await job.RunContinuousAsync(iterationLimit : 2);

            Assert.Equal(3, job.RunCount);

            var sw = Stopwatch.StartNew();
            await job.RunContinuousAsync(cancellationToken : TimeSpan.FromMilliseconds(100).ToCancellationToken());

            sw.Stop();
            Assert.InRange(sw.Elapsed, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(800));

            var jobInstance = new HelloWorldJob();

            Assert.NotNull(jobInstance);
            Assert.Equal(0, jobInstance.RunCount);
            Assert.Equal(JobResult.Success, await jobInstance.RunAsync());
            Assert.Equal(1, jobInstance.RunCount);
        }
Beispiel #2
0
        public async Task CanCancelJob() {
            var token = TimeSpan.FromSeconds(1).ToCancellationToken();
            var job = new HelloWorldJob();
            var result = await new JobRunner(job, Log).RunAsync(token);

            Assert.True(result);
        }
Beispiel #3
0
        public async Task CanCancelContinuousJobs() {
            var job = new HelloWorldJob();
            await job.RunContinuousAsync(TimeSpan.FromSeconds(1), 5, TimeSpan.FromMilliseconds(100).ToCancellationToken());
            Assert.Equal(1, job.RunCount);

            await JobRunner.RunContinuousAsync(typeof(HelloWorldJob), instanceCount: 5, iterationLimit: 10000, cancellationToken: TimeSpan.FromMilliseconds(500).ToCancellationToken(), interval: TimeSpan.FromMilliseconds(1));
        }
Beispiel #4
0
        public async Task CanCancelJob() {
            var job = new HelloWorldJob();
            var token = TimeSpan.FromSeconds(1).ToCancellationToken();
            var resultTask = new JobRunner(job, Log).RunAsync(token);
            await SystemClock.SleepAsync(TimeSpan.FromSeconds(2));

            Assert.True(await resultTask);
        }
Beispiel #5
0
        public async Task CanCancelJob()
        {
            var token  = TimeSpan.FromSeconds(1).ToCancellationToken();
            var job    = new HelloWorldJob();
            var result = await new JobRunner(job, Log).RunAsync(token);

            Assert.True(result);
        }
Beispiel #6
0
        public async Task CanCancelJob()
        {
            var job = new HelloWorldJob(Log);
            var timeoutCancellationTokenSource = new CancellationTokenSource(1000);
            var resultTask = new JobRunner(job, Log).RunAsync(timeoutCancellationTokenSource.Token);
            await SystemClock.SleepAsync(TimeSpan.FromSeconds(2));

            Assert.True(await resultTask);
        }
Beispiel #7
0
        public async Task CanCancelContinuousJobs()
        {
            var job = new HelloWorldJob();
            await job.RunContinuousAsync(TimeSpan.FromSeconds(1), 5, TimeSpan.FromMilliseconds(100).ToCancellationToken());

            Assert.Equal(1, job.RunCount);

            await JobRunner.RunContinuousAsync(typeof(HelloWorldJob), instanceCount : 5, iterationLimit : 10000, cancellationToken : TimeSpan.FromMilliseconds(500).ToCancellationToken(), interval : TimeSpan.FromMilliseconds(1));
        }
Beispiel #8
0
        public async void CanCancelContinuousJobs()
        {
            var job = new HelloWorldJob();
            var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
            job.RunContinuous(TimeSpan.FromSeconds(1), 5, tokenSource.Token);
            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));
        }
Beispiel #9
0
        public async Task CanRunMultipleInstances() {
            HelloWorldJob.GlobalRunCount = 0;
            
            var job = new HelloWorldJob();
            await new JobRunner(job, Log, instanceCount: 5, iterationLimit: 1).RunAsync(TimeSpan.FromSeconds(1).ToCancellationToken());
            Assert.Equal(5, HelloWorldJob.GlobalRunCount);

            HelloWorldJob.GlobalRunCount = 0;

            await new JobRunner(job, Log, instanceCount: 5, iterationLimit: 100).RunAsync(TimeSpan.FromSeconds(5).ToCancellationToken());
            Assert.Equal(500, HelloWorldJob.GlobalRunCount);
        }
Beispiel #10
0
        public async Task CanRunMultipleInstances() {
            HelloWorldJob.GlobalRunCount = 0;
            
            var job = new HelloWorldJob();
            await new JobRunner(job, Log, instanceCount: 5, iterationLimit: 1).RunAsync(TimeSpan.FromSeconds(1).ToCancellationToken());
            Assert.Equal(5, HelloWorldJob.GlobalRunCount);

            HelloWorldJob.GlobalRunCount = 0;

            await new JobRunner(job, Log, instanceCount: 5, iterationLimit: 100).RunAsync(TimeSpan.FromSeconds(5).ToCancellationToken());
            Assert.Equal(500, HelloWorldJob.GlobalRunCount);
        }
Beispiel #11
0
        public async Task CanCancelContinuousJobs()
        {
            using (TestSystemClock.Install()) {
                var job = new HelloWorldJob();
                job.RunContinuous(TimeSpan.FromSeconds(1), 5, TimeSpan.FromMilliseconds(100).ToCancellationToken());
                Assert.Equal(1, job.RunCount);

                var runnerTask = new JobRunner(job, Log, instanceCount: 5, iterationLimit: 10000, interval: TimeSpan.FromMilliseconds(1)).RunAsync(TimeSpan.FromMilliseconds(500).ToCancellationToken());
                await SystemClock.SleepAsync(TimeSpan.FromSeconds(1));

                await runnerTask;
            }
        }
Beispiel #12
0
        public async Task CanRunJobsWithInterval()
        {
            using (TestSystemClock.Install()) {
                var time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                TestSystemClock.SetFrozenTime(time);
                TestSystemClock.UseFakeSleep();

                var job      = new HelloWorldJob(Log);
                var interval = TimeSpan.FromHours(.75);

                await job.RunContinuousAsync(iterationLimit : 2, interval : interval);

                Assert.Equal(2, job.RunCount);
                Assert.Equal(interval, (SystemClock.UtcNow - time));
            }
        }
Beispiel #13
0
        public async Task CanRunMultipleInstances()
        {
            var job = new HelloWorldJob(Log);

            HelloWorldJob.GlobalRunCount = 0;
            using (var timeoutCancellationTokenSource = new CancellationTokenSource(1000)) {
                await new JobRunner(job, Log, instanceCount: 5, iterationLimit: 1).RunAsync(timeoutCancellationTokenSource.Token);
            }

            Assert.Equal(5, HelloWorldJob.GlobalRunCount);

            HelloWorldJob.GlobalRunCount = 0;
            using (var timeoutCancellationTokenSource = new CancellationTokenSource(50000)) {
                await new JobRunner(job, Log, instanceCount: 5, iterationLimit: 100).RunAsync(timeoutCancellationTokenSource.Token);
            }

            Assert.Equal(500, HelloWorldJob.GlobalRunCount);
        }
Beispiel #14
0
        public async Task CanCancelContinuousJobs()
        {
            using (TestSystemClock.Install()) {
                var job = new HelloWorldJob(Log);
                using (var timeoutCancellationTokenSource = new CancellationTokenSource(100)) {
                    await job.RunContinuousAsync(TimeSpan.FromSeconds(1), 5, timeoutCancellationTokenSource.Token);
                }

                Assert.Equal(1, job.RunCount);

                using (var timeoutCancellationTokenSource = new CancellationTokenSource(500)) {
                    var runnerTask = new JobRunner(job, Log, instanceCount: 5, iterationLimit: 10000, interval: TimeSpan.FromMilliseconds(1)).RunAsync(timeoutCancellationTokenSource.Token);
                    await SystemClock.SleepAsync(TimeSpan.FromSeconds(1));

                    await runnerTask;
                }
            }
        }
Beispiel #15
0
        public async Task CanRunJobs() {
            var job = new HelloWorldJob();
            Assert.Equal(0, job.RunCount);
            await job.RunAsync();
            Assert.Equal(1, job.RunCount);

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

            var sw = Stopwatch.StartNew();
            await job.RunContinuousAsync(cancellationToken: TimeSpan.FromMilliseconds(100).ToCancellationToken());
            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());
            Assert.Equal(1, ((HelloWorldJob)jobInstance).RunCount);
        }
Beispiel #16
0
        public void CanRunJobs()
        {
            var job = new HelloWorldJob();

            Assert.Equal(0, job.RunCount);
            job.Run();
            Assert.Equal(1, job.RunCount);

            job.RunContinuous(iterationLimit: 2);
            Assert.Equal(3, job.RunCount);

            job.RunContinuous(token: new CancellationTokenSource(TimeSpan.FromMilliseconds(10)).Token);
            Assert.True(job.RunCount > 10);

            var jobInstance = JobRunner.CreateJobInstance(typeof(HelloWorldJob).AssemblyQualifiedName);

            Assert.NotNull(jobInstance);
            Assert.Equal(0, ((HelloWorldJob)jobInstance).RunCount);
            Assert.Equal(JobResult.Success, jobInstance.Run());
            Assert.Equal(1, ((HelloWorldJob)jobInstance).RunCount);
        }
Beispiel #17
0
        public void CanRunJobs() {
            var job = new HelloWorldJob();
            Assert.Equal(0, job.RunCount);
            job.Run();
            Assert.Equal(1, job.RunCount);

            job.RunContinuous(iterationLimit: 2);
            Assert.Equal(3, job.RunCount);

            var sw = new Stopwatch();
            sw.Start();
            job.RunContinuous(token: new CancellationTokenSource(TimeSpan.FromMilliseconds(100)).Token);
            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, jobInstance.Run());
            Assert.Equal(1, ((HelloWorldJob)jobInstance).RunCount);
        }