Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Nov 3, 2024
1 parent 228c64f commit f7a5bf9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
15 changes: 6 additions & 9 deletions src/Mongo.Tests/MongoReplicaSetResourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

namespace Squadron
{
public class MongoReplicaSetResourceTests
: IClassFixture<MongoReplicaSetResource>
public class MongoReplicaSetResourceTests : IClassFixture<MongoReplicaSetResource>
{
private readonly MongoReplicaSetResource _mongoRsResource;

Expand All @@ -25,13 +24,11 @@ public void CommitTransaction_NoError()
//Act
Action action = () =>
{
using (IClientSessionHandle session = _mongoRsResource.Client.StartSession())
{
IMongoCollection<BsonDocument> collection = _mongoRsResource.CreateCollection<BsonDocument>("bar");
session.StartTransaction();
collection.InsertOne(session, new BsonDocument("name", "test"));
session.CommitTransaction();
}
using IClientSessionHandle session = _mongoRsResource.Client.StartSession();
IMongoCollection<BsonDocument> collection = _mongoRsResource.CreateCollection<BsonDocument>("bar");
session.StartTransaction();
collection.InsertOne(session, new BsonDocument("name", "test"));
session.CommitTransaction();
};

//Assert
Expand Down
21 changes: 11 additions & 10 deletions src/Mongo/MongoReplicaSetResource.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
using System;
using System;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;

namespace Squadron
{
/// <inheritdoc/>
/// <inheritdoc/>
public class MongoReplicaSetResource : MongoReplicaSetResource<MongoReplicaSetDefaultOptions> { }

/// <summary>
/// Represents a mongo database replica set resource that can be used by unit tests.
/// </summary>
/// <seealso cref="IDisposable"/>
public class MongoReplicaSetResource<TOptions> :
MongoResource<TOptions>
public class MongoReplicaSetResource<TOptions> : MongoResource<TOptions>
where TOptions : MongoReplicaSetDefaultOptions, new()
{
public override async Task InitializeAsync()
{
await base.InitializeAsync();

var client = new MongoClient(ConnectionString + "/?connect=direct");
BsonDocument rsConfig = CreateReplicaSetConfiguration();
var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument
{
{"replSetInitiate", rsConfig}
});

var command = new BsonDocumentCommand<BsonDocument>(
new BsonDocument
{
{"replSetInitiate", rsConfig}
});

await client.GetDatabase("admin")
.RunCommandAsync(command);
await client.GetDatabase("admin").RunCommandAsync(command);

await Initializer.WaitAsync(new MongoReplicaSetStatus(ConnectionString));
}
Expand Down

0 comments on commit f7a5bf9

Please sign in to comment.