Exemplo n.º 1
0
        public void DoJobError()
        {
            // Arrange
            var service = Substitute.For<IService>();
            var db = Substitute.For<IJobsDb>();
            var worker = new Worker(service, db);
            service.When(s => s.CompareDynamic(Arg.Any<dynamic>(), Arg.Any<dynamic>(), Arg.Any<bool>()))
                   .Do(x => { throw new Exception("Comparison failed"); });

            // Act
            var job = FakeJobs.PendingCompareJob;
            worker.DoJob(job);

            // Assert
            Assert.AreEqual("E", job.Status);
            Assert.IsTrue(string.IsNullOrWhiteSpace(job.CompletedId));
            Assert.IsTrue(job.ErrorText.Contains("Comparison failed"));
            db.Received(1).UpdateJob(Arg.Any<Job>());
        }
Exemplo n.º 2
0
        public void DoJobSuccess()
        {
            // Arrange
            var service = Substitute.For<IService>();
            var db = Substitute.For<IJobsDb>();
            var worker = new Worker(service, db);
            var id = Randomator.RandomFromSample(24, "0123456789ABCDEF");
            SubstituteExtensions.ReturnsForAnyArgs(
                service.CompareDynamic(Arg.Any<dynamic>(), Arg.Any<dynamic>(), Arg.Any<bool>()),
                new[] { new Comparison { Id = new ObjectId(id) } });

            // Act
            var job = FakeJobs.PendingCompareJob;
            worker.DoJob(job);

            // Assert
            Assert.AreEqual("C", job.Status);
            Assert.AreEqual(job.CompletedId.ToLower(), id.ToLower());
            db.Received(1).UpdateJob(Arg.Any<Job>());
        }
Exemplo n.º 3
0
        public void WorkerSleepsIfNoJobsFound()
        {
            // Arrange
            var service   = Substitute.For<IService>();
            var db        = Substitute.For<IJobsDb>();
            db.GetNextJob().ReturnsForAnyArgs((Job)null);
            var worker    = new Worker(service, db) { PollRate = 1000 };
            worker.DoWork = false;

            // Act
            var sw = Stopwatch.StartNew();
            worker.Poll();
            sw.Stop();

            // Assert
            Assert.That(sw.ElapsedMilliseconds, Is.AtLeast(950));
        }
Exemplo n.º 4
0
        public void WorkerSleepsUntilJobFound()
        {
            // Arrange
            var service = Substitute.For<IService>();
            var db = Substitute.For<IJobsDb>();
            var worker = new Worker(service, db) { PollRate = 500 };
            SubstituteExtensions.ReturnsForAnyArgs(
                service.CompareDynamic(Arg.Any<dynamic>(), Arg.Any<dynamic>(), Arg.Any<bool>()),
                new[] { new Comparison { Id = new ObjectId(Randomator.RandomFromSample(24, "0123456789ABCDEF")) } });

            // Third try will return a job
            var job = FakeJobs.PendingCompareJob;
            db.GetNextJob().ReturnsForAnyArgs(null, null, job);
            var counter = 0;

            // Stop work on the third call
            db.When(j => j.GetNextJob()).Do(x => { if (++counter == 4) worker.DoWork = false; });

            //  Act
            var sw = Stopwatch.StartNew();
            worker.Poll();
            sw.Stop();

            // Assert
            Assert.That(sw.ElapsedMilliseconds, Is.AtLeast(950));
            Assert.AreEqual("C", job.Status);
        }