|
| 1 | +using EphemeralMongo; |
| 2 | + |
| 3 | +#pragma warning disable AV1553 // Do not use optional parameters with default value null for strings, collections or tasks |
| 4 | + |
| 5 | +namespace TestBuildingBlocks; |
| 6 | + |
| 7 | +// Based on https://gist.github.com/asimmon/612b2d54f1a0d2b4e1115590d456e0be. |
| 8 | +internal sealed class MongoRunnerProvider |
| 9 | +{ |
| 10 | + public static readonly MongoRunnerProvider Instance = new(); |
| 11 | + |
| 12 | + private readonly object _lockObject = new(); |
| 13 | + private IMongoRunner? _runner; |
| 14 | + private int _useCounter; |
| 15 | + |
| 16 | + private MongoRunnerProvider() |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + public IMongoRunner Get() |
| 21 | + { |
| 22 | + lock (_lockObject) |
| 23 | + { |
| 24 | + if (_runner == null) |
| 25 | + { |
| 26 | + var runnerOptions = new MongoRunnerOptions |
| 27 | + { |
| 28 | + // Single-node replica set mode is required for transaction support in MongoDB. |
| 29 | + UseSingleNodeReplicaSet = true, |
| 30 | + KillMongoProcessesWhenCurrentProcessExits = true, |
| 31 | + AdditionalArguments = "--quiet" |
| 32 | + }; |
| 33 | + |
| 34 | + _runner = MongoRunner.Run(runnerOptions); |
| 35 | + } |
| 36 | + |
| 37 | + _useCounter++; |
| 38 | + return new MongoRunnerWrapper(this, _runner); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private void Detach() |
| 43 | + { |
| 44 | + lock (_lockObject) |
| 45 | + { |
| 46 | + if (_runner != null) |
| 47 | + { |
| 48 | + _useCounter--; |
| 49 | + |
| 50 | + if (_useCounter == 0) |
| 51 | + { |
| 52 | + _runner.Dispose(); |
| 53 | + _runner = null; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private sealed class MongoRunnerWrapper : IMongoRunner |
| 60 | + { |
| 61 | + private readonly MongoRunnerProvider _owner; |
| 62 | + private IMongoRunner? _underlyingMongoRunner; |
| 63 | + |
| 64 | + public string ConnectionString => _underlyingMongoRunner?.ConnectionString ?? throw new ObjectDisposedException(nameof(IMongoRunner)); |
| 65 | + |
| 66 | + public MongoRunnerWrapper(MongoRunnerProvider owner, IMongoRunner underlyingMongoRunner) |
| 67 | + { |
| 68 | + _owner = owner; |
| 69 | + _underlyingMongoRunner = underlyingMongoRunner; |
| 70 | + } |
| 71 | + |
| 72 | + public void Import(string database, string collection, string inputFilePath, string? additionalArguments = null, bool drop = false) |
| 73 | + { |
| 74 | + if (_underlyingMongoRunner == null) |
| 75 | + { |
| 76 | + throw new ObjectDisposedException(nameof(IMongoRunner)); |
| 77 | + } |
| 78 | + |
| 79 | + _underlyingMongoRunner.Import(database, collection, inputFilePath, additionalArguments, drop); |
| 80 | + } |
| 81 | + |
| 82 | + public void Export(string database, string collection, string outputFilePath, string? additionalArguments = null) |
| 83 | + { |
| 84 | + if (_underlyingMongoRunner == null) |
| 85 | + { |
| 86 | + throw new ObjectDisposedException(nameof(IMongoRunner)); |
| 87 | + } |
| 88 | + |
| 89 | + _underlyingMongoRunner.Export(database, collection, outputFilePath, additionalArguments); |
| 90 | + } |
| 91 | + |
| 92 | + public void Dispose() |
| 93 | + { |
| 94 | + if (_underlyingMongoRunner != null) |
| 95 | + { |
| 96 | + _underlyingMongoRunner = null; |
| 97 | + _owner.Detach(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments