public void Should_mark_job_finished_when_it_is_completed()
        {
            const string testUrl  = "http://test.com";
            const string testHost = "test.com";

            using (_db.CreateTransaction())
            {
                #region action

                var urlItem = new UrlItem
                {
                    Url  = testUrl,
                    Host = testHost
                };
                var jobRep = new JobRepository(Mock.Of <IActivityLogRepository>());
                var job    = jobRep.Start(urlItem); // adds the 1st log message
                // wait a little bit to force difference in time between two log messages and sort them later
                Thread.Sleep(100);
                jobRep.Complete(job); // marks the job finished

                #endregion

                #region assertion

                // make sure the job is created in DB
                using (var ctx = _db.CreateDbContext())
                {
                    var finishedJob = ctx.JobItems.Single(j => j.Id == job.Id);
                    Assert.True(finishedJob.DateFinish.HasValue);
                }

                #endregion
            }
        }
        public void Should_release_URL_when_job_is_completed()
        {
            const string testUrl = "http://test.com";

            using (_db.CreateTransaction())
            {
                #region action

                var frontier          = new UrlFrontierRepository();
                var nextAvailableTime = new DateTime(2016, 1, 1, 0, 0, 0, DateTimeKind.Utc); // already available
                frontier.AddOrUpdateUrl(testUrl, nextAvailableTime);
                var urlItem = frontier.GetAvailableUrls(1, DateTime.UtcNow).First();         // should return one item

                var jobRep = new JobRepository(Mock.Of <IActivityLogRepository>());
                var job    = jobRep.Start(urlItem); // adds the 1st log message
                // wait a little bit to force difference in time between two log messages and sort them later
                Thread.Sleep(100);
                jobRep.Complete(job); // marks the job finished

                #endregion

                #region assertion

                // make sure the URL is not in progress
                using (var ctx = _db.CreateDbContext())
                {
                    urlItem = ctx.UrlItems.Single(url => url.Id == urlItem.Id);
                    Assert.False(urlItem.IsInProgress);
                }

                #endregion
            }
        }
        public void Should_add_item_to_history_when_job_is_completed()
        {
            var mockLogger = new Mock <IActivityLogRepository>();

            const string testUrl  = "http://test.com";
            const string testHost = "test.com";

            using (_db.CreateTransaction())
            {
                #region action

                var urlItem = new UrlItem
                {
                    Url  = testUrl,
                    Host = testHost
                };

                var jobRep = new JobRepository(mockLogger.Object);
                var job    = jobRep.Start(urlItem); // adds the 1st log message
                // wait a little bit to force difference in time between two log messages and sort them later
                Thread.Sleep(100);
                jobRep.Complete(job); // adds the 2nd log message

                #endregion

                #region assertion

                mockLogger.Verify(m => m.JobStarted(It.IsAny <JobItem>()), Times.Once);
                mockLogger.Verify(m => m.JobCompleted(It.IsAny <JobItem>()), Times.Once);

                #endregion
            }
        }