-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update to latest hangfire adds recommended indexes #330 removes jobQueueDto, now using JobDto instead optimizing Monitoring API * fix possible null * use service for tests instead of mongo2go * skip test which needs replicaset * set connection string * trying to remove test
- Loading branch information
Showing
33 changed files
with
596 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"testExplorer.useNativeTesting": true, | ||
"dotnetCoreExplorer.searchpatterns":"**/bin/**/*.{dll,exe}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/Hangfire.Mongo.Tests/Migration/Version20MigrationStepFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Hangfire.Mongo.Migration; | ||
using Hangfire.Mongo.Migration.Steps; | ||
using Hangfire.Mongo.Migration.Steps.Version20; | ||
using Hangfire.Mongo.Tests.Utils; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Hangfire.Mongo.Tests.Migration | ||
{ | ||
public class Version20MigrationStepFacts | ||
{ | ||
private readonly Mock<IMongoMigrationContext> _mongoMigrationBagMock; | ||
private readonly IMongoDatabase _database; | ||
private readonly Random _random; | ||
private readonly IMongoMigrationStep _migration; | ||
public Version20MigrationStepFacts() | ||
{ | ||
var dbContext = ConnectionUtils.CreateDbContext(); | ||
_database = dbContext.Database; | ||
_mongoMigrationBagMock = new Mock<IMongoMigrationContext>(MockBehavior.Strict); | ||
_random = new Random(); | ||
_migration = new RemoveJobQueueDto(); | ||
} | ||
|
||
[Fact] | ||
public void ExecuteStep01_MergeJobQueueIntoJobDto_Success() | ||
{ | ||
// ARRANGE | ||
var collection = _database.GetCollection<BsonDocument>("hangfire.jobGraph"); | ||
collection.DeleteMany("{}"); | ||
collection.InsertMany(CreateJobQueueDtos()); | ||
|
||
// ACT | ||
var result = _migration.Execute(_database, new MongoStorageOptions(), _mongoMigrationBagMock.Object); | ||
|
||
// ASSERT | ||
Assert.True(result, "Expected migration to be successful, reported 'false'"); | ||
var migrated = collection.Find(new BsonDocument("_t", "JobDto")).ToList(); | ||
foreach (var doc in migrated) | ||
{ | ||
Assert.True(doc.Contains("Queue")); | ||
Assert.True(doc.Contains("FetchedAt")); | ||
} | ||
var migratedFrom = collection.Find(new BsonDocument("_t", "JobQueueDto")).ToList(); | ||
|
||
Assert.Empty(migratedFrom); | ||
} | ||
|
||
[Fact] | ||
public void ExecuteStep01_AlreadyRunJobsGetsFetchedAtSet_Success() | ||
{ | ||
// ARRANGE | ||
var collection = _database.GetCollection<BsonDocument>("hangfire.jobGraph"); | ||
collection.DeleteMany("{}"); | ||
var jobs = CreateJobQueueDtos(jobQueueCount: 5, jobDtoCount: 3); | ||
collection.InsertMany(jobs); | ||
|
||
// ACT | ||
var result = _migration.Execute(_database, new MongoStorageOptions(), _mongoMigrationBagMock.Object); | ||
|
||
// ASSERT | ||
Assert.True(result, "Expected migration to be successful, reported 'false'"); | ||
var migrated = collection.Find(new BsonDocument("_t", "JobDto")).ToList(); | ||
foreach (var doc in migrated) | ||
{ | ||
Assert.True(doc.Contains("Queue")); | ||
Assert.True(doc.Contains("FetchedAt")); | ||
} | ||
var migratedFrom = collection.Find(new BsonDocument("_t", "JobQueueDto")).ToList(); | ||
Assert.Empty(migratedFrom); | ||
|
||
var fetchedJobs = migrated.Where(b => b["FetchedAt"] != BsonNull.Value).ToList(); | ||
foreach (var item in fetchedJobs) | ||
{ | ||
var job = jobs.Find(j => j["_id"] == item["_id"]); | ||
Assert.Equal(job["StateHistory"][0]["CreatedAt"], item["FetchedAt"]); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ExecuteStep02_AddIndexes_Success() | ||
{ | ||
// ARRANGE | ||
var collection = _database.GetCollection<BsonDocument>("hangfire.jobGraph"); | ||
collection.DeleteMany("{}"); | ||
collection.InsertMany(CreateJobQueueDtos(0, 5)); | ||
var migration = new CompoundIndexes(); | ||
// ACT | ||
var result = migration.Execute(_database, new MongoStorageOptions(), _mongoMigrationBagMock.Object); | ||
|
||
// ASSERT | ||
var index = collection.Indexes.List().ToList().FirstOrDefault(b => b["name"].AsString == "IX_SetType_T_Score"); | ||
var index2 = collection.Indexes.List().ToList().FirstOrDefault(b => b["name"].AsString == "T_ExpireAt"); | ||
Assert.NotNull(index); | ||
Assert.NotNull(index2); | ||
} | ||
|
||
public List<BsonDocument> CreateJobQueueDtos(int jobQueueCount = 5, int jobDtoCount = 5) | ||
{ | ||
var list = new List<BsonDocument>(); | ||
var count = new[] { jobQueueCount, jobDtoCount }.Max(); | ||
|
||
for (int i = 0; i < count; i++) | ||
{ | ||
BsonDocument jobQueueDto = null; | ||
if (jobQueueCount > i) | ||
{ | ||
jobQueueDto = new BsonDocument | ||
{ | ||
["FetchedAt"] = BsonNull.Value, | ||
["Queue"] = "default", | ||
["JobId"] = ObjectId.GenerateNewId(), | ||
["_t"] = "JobQueueDto", | ||
["_id"] = ObjectId.GenerateNewId() | ||
}; | ||
list.Add(jobQueueDto); | ||
} | ||
|
||
if (jobDtoCount > i) | ||
{ | ||
var jobDto = new BsonDocument | ||
{ | ||
["JobId"] = jobQueueDto != null ? jobQueueDto["_id"] : ObjectId.GenerateNewId(), | ||
["_t"] = "JobDto", | ||
["_id"] = ObjectId.GenerateNewId(), | ||
["StateName"] = "Enqueued", | ||
["StateHistory"] = new BsonArray | ||
{ | ||
new BsonDocument | ||
{ | ||
["CreatedAt"] = DateTime.UtcNow.AddDays(-1) | ||
} | ||
} | ||
}; | ||
list.Add(jobDto); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.