Skip to content

Commit

Permalink
Automatically run MongoDB during the tests (with EphemeralMongo) (#337)
Browse files Browse the repository at this point in the history
Also remove the mongodb service in GitHub actions.
  • Loading branch information
0xced authored Jan 30, 2023
1 parent b84fa24 commit 31aa962
Show file tree
Hide file tree
Showing 30 changed files with 479 additions and 489 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ jobs:
build:

runs-on: ubuntu-latest
services:
mongodb:
image: mongo
ports:
- 27017:27017

steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ jobs:
build_release:
name: create release
runs-on: ubuntu-latest
services:
mongodb:
image: mongo
ports:
- 27017:27017

steps:
- uses: actions/checkout@v2
Expand Down
23 changes: 12 additions & 11 deletions src/Hangfire.Mongo.Tests/ExpirationManagerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ public class ExpirationManagerFacts : IDisposable
private readonly HangfireDbContext _dbContext;
private readonly CancellationToken _token;

public ExpirationManagerFacts()
public ExpirationManagerFacts(MongoDbFixture fixture)
{
_dbContext = ConnectionUtils.CreateDbContext();
fixture.CleanDatabase();
_dbContext = fixture.CreateDbContext();

_token = new CancellationToken(true);
}

[Fact, CleanDatabase]
[Fact]
public void Execute_RemovesOutdatedRecords()
{
CreateExpirationEntries(_dbContext, DateTime.UtcNow.AddMonths(-1));
Expand All @@ -35,7 +36,7 @@ public void Execute_RemovesOutdatedRecords()
Assert.True(IsEntryExpired(_dbContext));
}

[Fact, CleanDatabase]
[Fact]
public void Execute_DoesNotRemoveEntries_WithNoExpirationTimeSet()
{
CreateExpirationEntries(_dbContext, null);
Expand All @@ -46,7 +47,7 @@ public void Execute_DoesNotRemoveEntries_WithNoExpirationTimeSet()
Assert.False(IsEntryExpired(_dbContext));
}

[Fact, CleanDatabase]
[Fact]
public void Execute_DoesNotRemoveEntries_WithFreshExpirationTime()
{
CreateExpirationEntries(_dbContext, DateTime.UtcNow.AddMonths(1));
Expand All @@ -58,7 +59,7 @@ public void Execute_DoesNotRemoveEntries_WithFreshExpirationTime()
Assert.False(IsEntryExpired(_dbContext));
}

[Fact, CleanDatabase]
[Fact]
public void Execute_Processes_CounterTable()
{
// Arrange
Expand All @@ -80,7 +81,7 @@ public void Execute_Processes_CounterTable()
Assert.Equal(0, count);
}

[Fact, CleanDatabase]
[Fact]
public void Execute_Processes_JobTable()
{
// Arrange
Expand All @@ -103,7 +104,7 @@ public void Execute_Processes_JobTable()
Assert.Equal(0, count);
}

[Fact, CleanDatabase]
[Fact]
public void Execute_Processes_ListTable()
{
// Arrange
Expand All @@ -126,7 +127,7 @@ public void Execute_Processes_ListTable()
Assert.Equal(0, count);
}

[Fact, CleanDatabase]
[Fact]
public void Execute_Processes_SetTable()
{
// Arrange
Expand All @@ -151,7 +152,7 @@ public void Execute_Processes_SetTable()
Assert.Equal(0, count);
}

[Fact, CleanDatabase]
[Fact]
public void Execute_Processes_HashTable()
{
// Arrange
Expand All @@ -176,7 +177,7 @@ public void Execute_Processes_HashTable()
}


[Fact, CleanDatabase]
[Fact]
public void Execute_Processes_AggregatedCounterTable()
{
// Arrange
Expand Down
6 changes: 5 additions & 1 deletion src/Hangfire.Mongo.Tests/Hangfire.Mongo.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<NoWarn>$(NoWarn);CS0618</NoWarn>
Expand Down Expand Up @@ -26,6 +26,7 @@
<ProjectReference Include="../Hangfire.Mongo/Hangfire.Mongo.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="EphemeralMongo6" Version="0.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand All @@ -41,6 +42,9 @@
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<None Update="Migration\Hangfire-Mongo-Schema-004.zip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
32 changes: 20 additions & 12 deletions src/Hangfire.Mongo.Tests/Migration/Mongo/MigrationFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ namespace Hangfire.Mongo.Tests.Migration.Mongo
[Collection("Database")]
public class MigrationFacts
{
private readonly MongoDbFixture _fixture;

public MigrationFacts(MongoDbFixture fixture)
{
fixture.CleanDatabase();
_fixture = fixture;
}

[Theory]
[InlineData(null, false)]
[InlineData("Hangfire-Mongo-Schema-004.zip", false)]
Expand All @@ -43,15 +51,15 @@ public class MigrationFacts
public void Migrate_Full_Success(string seedFile, bool assertCollectionHasItems)
{

var dbContext = ConnectionUtils.CreateDbContext("Hangfire-Mongo-Migration-Tests");
var dbContext = _fixture.CreateDbContext("Hangfire-Mongo-Migration-Tests");

// ARRANGE
dbContext.Client.DropDatabase(dbContext.Database.DatabaseNamespace.DatabaseName);
if (seedFile != null)
{
SeedCollectionFromZipArchive(dbContext, Path.Combine("Migration", seedFile));
}

var storageOptions = new MongoStorageOptions
{
MigrationOptions = new MongoMigrationOptions
Expand All @@ -70,10 +78,10 @@ public void Migrate_Full_Success(string seedFile, bool assertCollectionHasItems)
AssertDataIntegrity(dbContext, assertCollectionHasItems);
}

[Fact, CleanDatabase]
[Fact]
public void Migrate_MultipleInstances_ThereCanBeOnlyOne()
{
var dbContext = ConnectionUtils.CreateDbContext();
var dbContext = _fixture.CreateDbContext();
// ARRANGE
dbContext.Database.DropCollection(dbContext.Schema.CollectionNamespace.CollectionName);
var storageOptions = new MongoStorageOptions
Expand Down Expand Up @@ -114,12 +122,12 @@ public void Migrate_MultipleInstances_ThereCanBeOnlyOne()
// ASSERT
Assert.True(tasks.Select(t => t.Result).Single(b => b));
}

[Fact]
public void Migrate_DropNoBackup_Success()
{
var dbContext = ConnectionUtils.CreateDbContext("Hangfire-Mongo-Migration-Tests");
var dbContext = _fixture.CreateDbContext("Hangfire-Mongo-Migration-Tests");

// ARRANGE
dbContext.Client.DropDatabase(dbContext.Database.DatabaseNamespace.DatabaseName);
SeedCollectionFromZipArchive(dbContext, Path.Combine("Migration", "Hangfire-Mongo-Schema-006.zip"));
Expand All @@ -146,22 +154,22 @@ private static void AssertDataIntegrity(HangfireDbContext dbContext, bool assert
var locks = dbContext.DistributedLock.Find(new BsonDocument()).ToList();
var schema = dbContext.Schema.Find(new BsonDocument()).ToList();
var servers = dbContext.Server.Find(new BsonDocument()).ToList();


if (assertCollectionHasItems)
{
AssertCollectionNotEmpty(jobGraphDtos, nameof(dbContext.JobGraph));
AssertCollectionNotEmpty(locks, nameof(dbContext.DistributedLock));
AssertCollectionNotEmpty(schema, nameof(dbContext.Schema));
AssertCollectionNotEmpty(servers, nameof(dbContext.Server));
AssertCollectionNotEmpty(servers, nameof(dbContext.Server));
}
}

private static void AssertCollectionNotEmpty(IEnumerable<object> collection, string collectionName)
{
Assert.True(collection.Any(), $"Expected '{collectionName}' to have items");
}

#region Private Helper Methods

private static void SeedCollectionFromZipArchive(HangfireDbContext connection, string fileName)
Expand Down Expand Up @@ -204,10 +212,10 @@ private static void SeedCollectionFromJson(HangfireDbContext connection, string
catch (Exception e)
{
Console.WriteLine($"Error seeding collection {collectionName}: {e}");

throw;
}

}
}

Expand Down
17 changes: 12 additions & 5 deletions src/Hangfire.Mongo.Tests/Migration/Mongo/MongoDatabaseFiller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ namespace Hangfire.Mongo.Tests.Migration.Mongo
[Collection("Database")]
public class MongoDatabaseFiller
{
private readonly MongoDbFixture _fixture;

public MongoDatabaseFiller(MongoDbFixture fixture)
{
_fixture = fixture;
}

//[Fact, Trait("Category", "DataGeneration")]
public void Clean_Database_Filled()
{
var databaseName = "Mongo-Hangfire-Filled";
var context = ConnectionUtils.CreateDbContext(databaseName);
var context = _fixture.CreateDbContext(databaseName);
// Make sure we start from scratch
context.Database.Client.DropDatabase(databaseName);

Expand All @@ -40,8 +47,8 @@ public void Clean_Database_Filled()
{
ShutdownTimeout = TimeSpan.FromSeconds(15)
};
JobStorage.Current = ConnectionUtils.CreateStorage(databaseName);

JobStorage.Current = _fixture.CreateStorage(databaseName);

using (new BackgroundJobServer(serverOptions))
{
Expand Down Expand Up @@ -93,7 +100,7 @@ public void Clean_Database_Filled()
if (MongoMigrationManager.RequiredSchemaVersion >= MongoSchema.Version09 &&
MongoMigrationManager.RequiredSchemaVersion <= MongoSchema.Version15)
{
// Signal collection work was initiated in schema version 9,
// Signal collection work was initiated in schema version 9,
// and still not put to use in schema version 15.
allowedEmptyCollections.Add($@"{storageOptions.Prefix}.signal");
}
Expand All @@ -106,7 +113,7 @@ private void BackupDatabaseToStream(string databaseName, Stream stream, params s
{
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
{
var context = ConnectionUtils.CreateDbContext(databaseName);
var context = _fixture.CreateDbContext(databaseName);
foreach (var collectionName in context.Database.ListCollections().ToList()
.Select(c => c["name"].AsString))
{
Expand Down
Loading

0 comments on commit 31aa962

Please sign in to comment.