public void TestMarkJobFailedImpl() { Guid jobId = Guid.NewGuid(); var testJob = new MongoExtractJobDoc( jobId, MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider), "1234", ExtractJobStatus.Failed, "test/dir", _dateTimeProvider.UtcNow(), "1.2.3.4", 123, "MR", isIdentifiableExtraction: true, isNoFilterExtraction: true, null); var client = new TestMongoClient(); var store = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider); // Assert that an exception is thrown for a non-existent job Assert.Throws <ApplicationException>(() => store.MarkJobFailed(Guid.NewGuid(), new Exception())); client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once); client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never); // Assert that a job can't be failed twice client = new TestMongoClient(); store = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider); client.ExtractionDatabase.InProgressCollection.InsertOne(testJob); client.MockSessionHandle.Reset(); Assert.Throws <ApplicationException>(() => store.MarkJobFailed(jobId, new Exception())); client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once); client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never); // Check we handle a bad ReplaceOneResult client = new TestMongoClient(); store = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider); testJob.JobStatus = ExtractJobStatus.WaitingForCollectionInfo; client.ExtractionDatabase.InProgressCollection.InsertOne(testJob); client.ExtractionDatabase.InProgressCollection.RejectChanges = true; client.MockSessionHandle.Reset(); Assert.Throws <ApplicationException>(() => store.MarkJobFailed(jobId, new Exception())); client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once); client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never); // Check happy path client = new TestMongoClient(); store = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider); testJob.JobStatus = ExtractJobStatus.WaitingForCollectionInfo; testJob.FailedJobInfoDoc = null; client.ExtractionDatabase.InProgressCollection.InsertOne(testJob); client.MockSessionHandle.Reset(); store.MarkJobFailed(jobId, new Exception("TestMarkJobFailedImpl")); client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Never); client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Once); Dictionary <Guid, MongoExtractJobDoc> docs = client.ExtractionDatabase.InProgressCollection.Documents; Assert.AreEqual(1, docs.Count); MongoExtractJobDoc failedDoc = docs[jobId]; Assert.AreEqual(ExtractJobStatus.Failed, failedDoc.JobStatus); Assert.NotNull(failedDoc.FailedJobInfoDoc); Assert.AreEqual("TestMarkJobFailedImpl", failedDoc.FailedJobInfoDoc.ExceptionMessage); }