예제 #1
0
        public void ShouldExecuteJobWhenFreeSlotIsFound()
        {
            //arrange
            var testJob1 = new TestJob {Id = 1, ExecutionTime = 1000};
            var testJob2 = new TestJob {Id = 2, ExecutionTime = 1000};
            var testJob3 = new TestJob {Id = 3, ExecutionTime = 1000};
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            //act
            _jobQueue.Add(testJob1);
            _jobQueue.Add(testJob2);
            _jobQueue.Add(testJob3);

            //assert
            WaitHandle.WaitAll(new[] {testJob1.Event, testJob2.Event});
            Assert.That(testJob1.Finished, Is.True);
            Assert.That(testJob2.Finished, Is.True);
            Assert.That(stopwatch.ElapsedMilliseconds, Is.LessThan(2000));

            testJob3.Event.WaitOne();
            stopwatch.Stop();
            Assert.That(testJob3.Finished, Is.True);
            Assert.That(stopwatch.ElapsedMilliseconds, Is.LessThan(3000));
        }
예제 #2
0
        public void ShouldHandleException()
        {
            //arrange
            var testJob1 = new ExceptionJob {Id = 1, ExecutionTime = 1000};
            var testJob2 = new TestJob {Id = 2, ExecutionTime = 1000};
            var testJob3 = new TestJob {Id = 3, ExecutionTime = 1000};

            //act
            _jobQueue.Add(testJob1);
            _jobQueue.Add(testJob2);
            _jobQueue.Add(testJob3);
            WaitHandle.WaitAll(new[] {testJob2.Event, testJob3.Event});

            //assert
            Assert.That(testJob1.Finished, Is.False);
            Assert.That(testJob2.Finished, Is.True);
            Assert.That(testJob3.Finished, Is.True);
        }
예제 #3
0
        public void ShouldNotExecuteMoreThanGivenAmountOfJobsAtOnce()
        {
            //arrange
            var testJob1 = new TestJob {Id = 1, ExecutionTime = 1000};
            var testJob2 = new TestJob {Id = 2, ExecutionTime = 1000};
            var testJob3 = new TestJob {Id = 3, ExecutionTime = 1000};

            //act
            _jobQueue.Add(testJob1);
            _jobQueue.Add(testJob2);
            _jobQueue.Add(testJob3);
            WaitHandle.WaitAll(new[] {testJob1.Event, testJob2.Event});

            //assert
            Assert.That(testJob1.Finished, Is.True);
            Assert.That(testJob2.Finished, Is.True);
            Assert.That(testJob3.Finished, Is.False);
        }
예제 #4
0
        public void ShouldRunTwoJobsInParallel()
        {
            //arrange
            var testJob1 = new TestJob {Id = 1, ExecutionTime = 1000};
            var testJob2 = new TestJob {Id = 2, ExecutionTime = 1000};
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            //act
            _jobQueue.Add(testJob1);
            _jobQueue.Add(testJob2);
            WaitHandle.WaitAll(new[] {testJob1.Event, testJob2.Event});

            //assert
            stopwatch.Stop();
            Assert.That(testJob1.Finished, Is.True);
            Assert.That(testJob2.Finished, Is.True);
            Assert.That(stopwatch.ElapsedMilliseconds, Is.LessThan(2000));
        }
예제 #5
0
        public void ShouldNotScheduleSameJobTwice()
        {
            //arrange
            var testJob1 = new TestJob {Id = 1, ExecutionTime = 100};
            var testJob2 = new TestJob {Id = 1, ExecutionTime = 100};

            //act
            _jobQueue.Add(testJob1);
            _jobQueue.Add(testJob2);
            Thread.Sleep(200);

            //assert
            Assert.That(testJob1.Finished, Is.True);
            Assert.That(testJob2.Finished, Is.False);
        }