From ec221e25ca141ba8755aba6cb17721ff4beea07a Mon Sep 17 00:00:00 2001 From: Ferdinando Papale <4850119+papafe@users.noreply.github.com> Date: Tue, 23 Jun 2026 15:14:15 +0200 Subject: [PATCH 1/6] CSHARP-2043: Add AssumeIndexesExist option to GridFSBucketOptions Lets users opt out of the automatic GridFS index check and creation on first upload. Useful for sharded clusters (where the empty-files check is broadcast to all shards and fails if any shard is down) and write-only users. Based on the original work by John Gibbons (jg11jg) in #294. --- src/MongoDB.Driver/GridFS/GridFSBucket.cs | 10 ++++ .../GridFS/GridFSBucketOptions.cs | 28 ++++++++++ .../GridFS/GridFSBucketOptionsTests.cs | 50 ++++++++++++++++- .../GridFS/GridFSBucketTests.cs | 55 +++++++++++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) diff --git a/src/MongoDB.Driver/GridFS/GridFSBucket.cs b/src/MongoDB.Driver/GridFS/GridFSBucket.cs index a7296fa7436..b4d1e4ba0ae 100644 --- a/src/MongoDB.Driver/GridFS/GridFSBucket.cs +++ b/src/MongoDB.Driver/GridFS/GridFSBucket.cs @@ -897,6 +897,11 @@ private GridFSUploadStream CreateUploadStream(IReadWriteBindingHandle b private void EnsureIndexes(OperationContext operationContext, IReadWriteBindingHandle binding) { + if (_options.AssumeIndexesExist) + { + return; + } + _ensureIndexesSemaphore.Wait(operationContext.RemainingTimeout, operationContext.CancellationToken); try { @@ -926,6 +931,11 @@ private void EnsureIndexes(OperationContext operationContext, IReadWriteBindingH private async Task EnsureIndexesAsync(OperationContext operationContext, IReadWriteBindingHandle binding) { + if (_options.AssumeIndexesExist) + { + return; + } + await _ensureIndexesSemaphore.WaitAsync(operationContext.RemainingTimeout, operationContext.CancellationToken).ConfigureAwait(false); try { diff --git a/src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs b/src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs index 1ef76936f0f..320ff25d85a 100644 --- a/src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs +++ b/src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs @@ -24,6 +24,7 @@ namespace MongoDB.Driver.GridFS public class GridFSBucketOptions { // fields + private bool _assumeIndexesExist; private string _bucketName; private int _chunkSizeBytes; private ReadConcern _readConcern; @@ -46,6 +47,7 @@ public GridFSBucketOptions() public GridFSBucketOptions(GridFSBucketOptions other) { Ensure.IsNotNull(other, nameof(other)); + _assumeIndexesExist = other.AssumeIndexesExist; _bucketName = other.BucketName; _chunkSizeBytes = other.ChunkSizeBytes; _readConcern = other.ReadConcern; @@ -60,6 +62,7 @@ public GridFSBucketOptions(GridFSBucketOptions other) public GridFSBucketOptions(ImmutableGridFSBucketOptions other) { Ensure.IsNotNull(other, nameof(other)); + _assumeIndexesExist = other.AssumeIndexesExist; _bucketName = other.BucketName; _chunkSizeBytes = other.ChunkSizeBytes; _readConcern = other.ReadConcern; @@ -68,6 +71,18 @@ public GridFSBucketOptions(ImmutableGridFSBucketOptions other) } // properties + /// + /// Gets or sets whether to assume the GridFS indexes already exist, skipping the index check and creation before the first upload. + /// + /// + /// true if the GridFS indexes are assumed to exist; otherwise, false. + /// + public bool AssumeIndexesExist + { + get { return _assumeIndexesExist; } + set { _assumeIndexesExist = value; } + } + /// /// Gets or sets the bucket name. /// @@ -160,6 +175,7 @@ public static ImmutableGridFSBucketOptions Defaults #endregion // fields + private readonly bool _assumeIndexesExist; private readonly string _bucketName; private readonly int _chunkSizeBytes; private readonly ReadConcern _readConcern; @@ -183,6 +199,7 @@ public ImmutableGridFSBucketOptions() public ImmutableGridFSBucketOptions(GridFSBucketOptions other) { Ensure.IsNotNull(other, nameof(other)); + _assumeIndexesExist = other.AssumeIndexesExist; _bucketName = other.BucketName; _chunkSizeBytes = other.ChunkSizeBytes; _readConcern = other.ReadConcern; @@ -191,6 +208,17 @@ public ImmutableGridFSBucketOptions(GridFSBucketOptions other) } // properties + /// + /// Gets whether to assume the GridFS indexes already exist, skipping the index check and creation before the first upload. + /// + /// + /// true if the GridFS indexes are assumed to exist; otherwise, false. + /// + public bool AssumeIndexesExist + { + get { return _assumeIndexesExist; } + } + /// /// Gets the bucket name. /// diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs index d80fcef35a2..5a8d6587299 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs @@ -27,6 +27,32 @@ namespace MongoDB.Driver.Tests.GridFS { public class GridFSBucketOptionsTests { + [Theory] + [ParameterAttributeData] + public void AssumeIndexesExist_get_should_return_expected_result( + [Values(false, true)] + bool value) + { + var subject = new GridFSBucketOptions { AssumeIndexesExist = value }; + + var result = subject.AssumeIndexesExist; + + result.Should().Be(value); + } + + [Theory] + [ParameterAttributeData] + public void AssumeIndexesExist_set_should_have_expected_result( + [Values(false, true)] + bool value) + { + var subject = new GridFSBucketOptions(); + + subject.AssumeIndexesExist = value; + + subject.AssumeIndexesExist.Should().Be(value); + } + [Fact] public void BucketName_get_should_return_expected_result() { @@ -103,11 +129,12 @@ public void ChunkSizeBytes_set_should_throw_when_value_is_invalid( [Fact] public void constructor_with_immutable_other_should_initialize_instance() { - var mutable = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority }; + var mutable = new GridFSBucketOptions { AssumeIndexesExist = true, BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority }; var other = new ImmutableGridFSBucketOptions(mutable); var result = new GridFSBucketOptions(other); + result.AssumeIndexesExist.Should().Be(other.AssumeIndexesExist); result.BucketName.Should().Be(other.BucketName); result.ChunkSizeBytes.Should().Be(other.ChunkSizeBytes); result.ReadConcern.Should().Be(other.ReadConcern); @@ -118,10 +145,11 @@ public void constructor_with_immutable_other_should_initialize_instance() [Fact] public void constructor_with_mutable_other_should_initialize_instance() { - var other = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority }; + var other = new GridFSBucketOptions { AssumeIndexesExist = true, BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority }; var result = new GridFSBucketOptions(other); + result.AssumeIndexesExist.Should().Be(other.AssumeIndexesExist); result.BucketName.Should().Be(other.BucketName); result.ChunkSizeBytes.Should().Be(other.ChunkSizeBytes); result.ReadConcern.Should().Be(other.ReadConcern); @@ -134,6 +162,7 @@ public void constructor_with_no_arguments_should_initialize_instance_with_defaul { var result = new GridFSBucketOptions(); + result.AssumeIndexesExist.Should().BeFalse(); result.BucketName.Should().Be("fs"); result.ChunkSizeBytes.Should().Be(255 * 1024); result.ReadPreference.Should().BeNull(); @@ -203,6 +232,19 @@ public void WriteConcern_set_should_have_expected_result() public class ImmutableGridFSBucketOptionsTests { + [Theory] + [ParameterAttributeData] + public void AssumeIndexesExist_get_should_return_expected_result( + [Values(false, true)] + bool value) + { + var subject = new ImmutableGridFSBucketOptions(new GridFSBucketOptions { AssumeIndexesExist = value }); + + var result = subject.AssumeIndexesExist; + + result.Should().Be(value); + } + [Fact] public void BucketName_get_should_return_expected_result() { @@ -228,6 +270,7 @@ public void constructor_with_arguments_should_initialize_instance() { var mutable = new GridFSBucketOptions { + AssumeIndexesExist = true, BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, @@ -237,6 +280,7 @@ public void constructor_with_arguments_should_initialize_instance() var result = new ImmutableGridFSBucketOptions(mutable); + result.AssumeIndexesExist.Should().BeTrue(); result.BucketName.Should().Be("bucket"); result.ChunkSizeBytes.Should().Be(123); result.ReadConcern.Should().Be(ReadConcern.Majority); @@ -249,6 +293,7 @@ public void constructor_with_no_arguments_should_initialize_instance_with_defaul { var result = new GridFSBucketOptions(); + result.AssumeIndexesExist.Should().BeFalse(); result.BucketName.Should().Be("fs"); result.ChunkSizeBytes.Should().Be(255 * 1024); result.ReadConcern.Should().BeNull(); @@ -270,6 +315,7 @@ public void Defaults_get_should_return_expected_result() { var result = ImmutableGridFSBucketOptions.Defaults; + result.AssumeIndexesExist.Should().BeFalse(); result.BucketName.Should().Be("fs"); result.ChunkSizeBytes.Should().Be(255 * 1024); result.ReadConcern.Should().BeNull(); diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs index c7403469eb6..ff31aab7d67 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs @@ -703,6 +703,61 @@ public void GridFS_should_work_with_strict_stable_api( } } + [Theory] + [ParameterAttributeData] + [Trait("Category", "Integration")] + public void Upload_should_not_create_indexes_when_AssumeIndexesExist_is_true( + [Values(false, true)] bool async) + { + RequireServer.Check(); + var client = DriverTestConfiguration.Client; + var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); + var options = new GridFSBucketOptions { AssumeIndexesExist = true }; + var subject = new GridFSBucket(database, options); + subject.Drop(); + + if (async) + { + subject.UploadFromBytesAsync("filename", new byte[] { 0 }).GetAwaiter().GetResult(); + } + else + { + subject.UploadFromBytes("filename", new byte[] { 0 }); + } + + var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); + filesIndexes.Should().OnlyContain(index => index["name"] == "_id_"); + var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); + chunksIndexes.Should().NotContain(index => index["name"] == "files_id_1_n_1"); + } + + [Theory] + [ParameterAttributeData] + [Trait("Category", "Integration")] + public void Upload_should_create_indexes_when_AssumeIndexesExist_is_false( + [Values(false, true)] bool async) + { + RequireServer.Check(); + var client = DriverTestConfiguration.Client; + var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); + var subject = new GridFSBucket(database); + subject.Drop(); + + if (async) + { + subject.UploadFromBytesAsync("filename", new byte[] { 0 }).GetAwaiter().GetResult(); + } + else + { + subject.UploadFromBytes("filename", new byte[] { 0 }); + } + + var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); + filesIndexes.Should().Contain(index => index["name"] == "filename_1_uploadDate_1"); + var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); + chunksIndexes.Should().Contain(index => index["name"] == "files_id_1_n_1"); + } + // private methods private GridFSBucket CreateSubject(GridFSBucketOptions options = null) { From b5c1d6eeef45fee5d3d83bfc05dc509cb562ef3a Mon Sep 17 00:00:00 2001 From: Ferdinando Papale <4850119+papafe@users.noreply.github.com> Date: Tue, 23 Jun 2026 15:35:43 +0200 Subject: [PATCH 2/6] Address PR review: fix immutable defaults test and loosen index assertion - ImmutableGridFSBucketOptions defaults test now constructs the immutable type. - Assert GridFS index absence rather than OnlyContain(_id_) to avoid flakiness. --- tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs | 2 +- tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs index 5a8d6587299..0bc09d4ec9a 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketOptionsTests.cs @@ -291,7 +291,7 @@ public void constructor_with_arguments_should_initialize_instance() [Fact] public void constructor_with_no_arguments_should_initialize_instance_with_default_values() { - var result = new GridFSBucketOptions(); + var result = new ImmutableGridFSBucketOptions(); result.AssumeIndexesExist.Should().BeFalse(); result.BucketName.Should().Be("fs"); diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs index ff31aab7d67..3d956511f54 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs @@ -726,7 +726,7 @@ public void Upload_should_not_create_indexes_when_AssumeIndexesExist_is_true( } var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); - filesIndexes.Should().OnlyContain(index => index["name"] == "_id_"); + filesIndexes.Should().NotContain(index => index["name"] == "filename_1_uploadDate_1"); var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); chunksIndexes.Should().NotContain(index => index["name"] == "files_id_1_n_1"); } From c0bc8483ad07b04cec42f9226ba29ccabe8a4893 Mon Sep 17 00:00:00 2001 From: Ferdinando Papale <4850119+papafe@users.noreply.github.com> Date: Tue, 23 Jun 2026 18:54:21 +0200 Subject: [PATCH 3/6] Fix CI: clean up shared GridFS collections in AssumeIndexesExist test Upload_should_not_create_indexes_when_AssumeIndexesExist_is_true left the shared fs.files/fs.chunks non-empty without the GridFS indexes. A later test sharing the same bucket (Upload_of_duplicate_file_should_not_invalidate_existing_data) then found a non-empty files collection, so EnsureIndexes skipped index creation, and the missing unique {files_id:1,n:1} index meant the expected duplicate-key MongoBulkWriteException was never thrown. Drop the bucket at the end of the test to leave the collections empty. --- tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs index 3d956511f54..946ae22ece1 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs @@ -729,6 +729,10 @@ public void Upload_should_not_create_indexes_when_AssumeIndexesExist_is_true( filesIndexes.Should().NotContain(index => index["name"] == "filename_1_uploadDate_1"); var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); chunksIndexes.Should().NotContain(index => index["name"] == "files_id_1_n_1"); + + // clean up: this test leaves the shared collections non-empty without the GridFS indexes, + // which would suppress index creation for later tests that share the same bucket + subject.Drop(); } [Theory] From c9a7a6cbb70563d79c370130196c21dab1ae8ddd Mon Sep 17 00:00:00 2001 From: Ferdinando Papale <4850119+papafe@users.noreply.github.com> Date: Tue, 23 Jun 2026 19:36:22 +0200 Subject: [PATCH 4/6] Address PR review: clean up GridFS buckets in finally for test isolation Wrap the AssumeIndexesExist integration tests in try/finally and drop the bucket in the finally block (async-aware via a DropBucket helper) so shared fs.* state is restored even if an assertion fails. --- .../GridFS/GridFSBucketTests.cs | 72 ++++++++++++------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs index 946ae22ece1..dfc2f010f60 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs @@ -714,25 +714,30 @@ public void Upload_should_not_create_indexes_when_AssumeIndexesExist_is_true( var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); var options = new GridFSBucketOptions { AssumeIndexesExist = true }; var subject = new GridFSBucket(database, options); - subject.Drop(); + DropBucket(subject, async); - if (async) + try { - subject.UploadFromBytesAsync("filename", new byte[] { 0 }).GetAwaiter().GetResult(); + if (async) + { + subject.UploadFromBytesAsync("filename", new byte[] { 0 }).GetAwaiter().GetResult(); + } + else + { + subject.UploadFromBytes("filename", new byte[] { 0 }); + } + + var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); + filesIndexes.Should().NotContain(index => index["name"] == "filename_1_uploadDate_1"); + var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); + chunksIndexes.Should().NotContain(index => index["name"] == "files_id_1_n_1"); } - else + finally { - subject.UploadFromBytes("filename", new byte[] { 0 }); + // restore shared state: without cleanup this test leaves fs.* non-empty and without the + // GridFS indexes, which would suppress index creation for later tests reusing the default bucket + DropBucket(subject, async); } - - var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); - filesIndexes.Should().NotContain(index => index["name"] == "filename_1_uploadDate_1"); - var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); - chunksIndexes.Should().NotContain(index => index["name"] == "files_id_1_n_1"); - - // clean up: this test leaves the shared collections non-empty without the GridFS indexes, - // which would suppress index creation for later tests that share the same bucket - subject.Drop(); } [Theory] @@ -745,21 +750,28 @@ public void Upload_should_create_indexes_when_AssumeIndexesExist_is_false( var client = DriverTestConfiguration.Client; var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); var subject = new GridFSBucket(database); - subject.Drop(); + DropBucket(subject, async); - if (async) + try { - subject.UploadFromBytesAsync("filename", new byte[] { 0 }).GetAwaiter().GetResult(); + if (async) + { + subject.UploadFromBytesAsync("filename", new byte[] { 0 }).GetAwaiter().GetResult(); + } + else + { + subject.UploadFromBytes("filename", new byte[] { 0 }); + } + + var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); + filesIndexes.Should().Contain(index => index["name"] == "filename_1_uploadDate_1"); + var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); + chunksIndexes.Should().Contain(index => index["name"] == "files_id_1_n_1"); } - else + finally { - subject.UploadFromBytes("filename", new byte[] { 0 }); + DropBucket(subject, async); } - - var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); - filesIndexes.Should().Contain(index => index["name"] == "filename_1_uploadDate_1"); - var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); - chunksIndexes.Should().Contain(index => index["name"] == "files_id_1_n_1"); } // private methods @@ -782,5 +794,17 @@ private void EnsureBucketExists(IGridFSBucket bucket) { bucket.UploadFromBytes("filename", new byte[0]); } + + private void DropBucket(IGridFSBucket bucket, bool async) + { + if (async) + { + bucket.DropAsync().GetAwaiter().GetResult(); + } + else + { + bucket.Drop(); + } + } } } From 4f01a35e7e1a3b7234dcf29018b4b0e849b064a9 Mon Sep 17 00:00:00 2001 From: Ferdinando Papale <4850119+papafe@users.noreply.github.com> Date: Tue, 23 Jun 2026 20:26:11 +0200 Subject: [PATCH 5/6] Address PR review: document AssumeIndexesExist index responsibility Add a remarks block warning that when true the driver never verifies or creates the GridFS indexes, so the caller must ensure they exist. --- src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs b/src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs index 320ff25d85a..437b1c4e933 100644 --- a/src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs +++ b/src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs @@ -74,6 +74,9 @@ public GridFSBucketOptions(ImmutableGridFSBucketOptions other) /// /// Gets or sets whether to assume the GridFS indexes already exist, skipping the index check and creation before the first upload. /// + /// + /// When true, the driver never verifies or creates the GridFS indexes, so the caller is responsible for ensuring they exist. + /// /// /// true if the GridFS indexes are assumed to exist; otherwise, false. /// @@ -211,6 +214,9 @@ public ImmutableGridFSBucketOptions(GridFSBucketOptions other) /// /// Gets whether to assume the GridFS indexes already exist, skipping the index check and creation before the first upload. /// + /// + /// When true, the driver never verifies or creates the GridFS indexes, so the caller is responsible for ensuring they exist. + /// /// /// true if the GridFS indexes are assumed to exist; otherwise, false. /// From 8154809b5f6c66fa17e80b86036a54721cd9055c Mon Sep 17 00:00:00 2001 From: Ferdinando Papale <4850119+papafe@users.noreply.github.com> Date: Thu, 25 Jun 2026 10:13:47 +0200 Subject: [PATCH 6/6] Address PR review: combine AssumeIndexesExist index tests into one async theory Per review feedback, merge the two index-behavior integration tests into a single async Task theory parameterized over assumeIndexesExist, using await instead of GetAwaiter().GetResult(). DropBucket helper is now async. --- .../GridFS/GridFSBucketTests.cs | 56 ++++++------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs index dfc2f010f60..8e7dd35bb0b 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSBucketTests.cs @@ -18,6 +18,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Threading.Tasks; using FluentAssertions; using MongoDB.Bson; using MongoDB.Driver.Core.Clusters; @@ -706,21 +707,22 @@ public void GridFS_should_work_with_strict_stable_api( [Theory] [ParameterAttributeData] [Trait("Category", "Integration")] - public void Upload_should_not_create_indexes_when_AssumeIndexesExist_is_true( + public async Task Upload_should_create_indexes_unless_AssumeIndexesExist_is_true( + [Values(false, true)] bool assumeIndexesExist, [Values(false, true)] bool async) { RequireServer.Check(); var client = DriverTestConfiguration.Client; var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); - var options = new GridFSBucketOptions { AssumeIndexesExist = true }; + var options = new GridFSBucketOptions { AssumeIndexesExist = assumeIndexesExist }; var subject = new GridFSBucket(database, options); - DropBucket(subject, async); + await DropBucketAsync(subject, async); try { if (async) { - subject.UploadFromBytesAsync("filename", new byte[] { 0 }).GetAwaiter().GetResult(); + await subject.UploadFromBytesAsync("filename", new byte[] { 0 }); } else { @@ -728,49 +730,23 @@ public void Upload_should_not_create_indexes_when_AssumeIndexesExist_is_true( } var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); - filesIndexes.Should().NotContain(index => index["name"] == "filename_1_uploadDate_1"); var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); - chunksIndexes.Should().NotContain(index => index["name"] == "files_id_1_n_1"); - } - finally - { - // restore shared state: without cleanup this test leaves fs.* non-empty and without the - // GridFS indexes, which would suppress index creation for later tests reusing the default bucket - DropBucket(subject, async); - } - } - - [Theory] - [ParameterAttributeData] - [Trait("Category", "Integration")] - public void Upload_should_create_indexes_when_AssumeIndexesExist_is_false( - [Values(false, true)] bool async) - { - RequireServer.Check(); - var client = DriverTestConfiguration.Client; - var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); - var subject = new GridFSBucket(database); - DropBucket(subject, async); - - try - { - if (async) + if (assumeIndexesExist) { - subject.UploadFromBytesAsync("filename", new byte[] { 0 }).GetAwaiter().GetResult(); + filesIndexes.Should().NotContain(index => index["name"] == "filename_1_uploadDate_1"); + chunksIndexes.Should().NotContain(index => index["name"] == "files_id_1_n_1"); } else { - subject.UploadFromBytes("filename", new byte[] { 0 }); + filesIndexes.Should().Contain(index => index["name"] == "filename_1_uploadDate_1"); + chunksIndexes.Should().Contain(index => index["name"] == "files_id_1_n_1"); } - - var filesIndexes = database.GetCollection("fs.files").Indexes.List().ToList(); - filesIndexes.Should().Contain(index => index["name"] == "filename_1_uploadDate_1"); - var chunksIndexes = database.GetCollection("fs.chunks").Indexes.List().ToList(); - chunksIndexes.Should().Contain(index => index["name"] == "files_id_1_n_1"); } finally { - DropBucket(subject, async); + // restore shared state: with AssumeIndexesExist the upload leaves fs.* non-empty and without the + // GridFS indexes, which would suppress index creation for later tests reusing the default bucket + await DropBucketAsync(subject, async); } } @@ -795,11 +771,11 @@ private void EnsureBucketExists(IGridFSBucket bucket) bucket.UploadFromBytes("filename", new byte[0]); } - private void DropBucket(IGridFSBucket bucket, bool async) + private async Task DropBucketAsync(IGridFSBucket bucket, bool async) { if (async) { - bucket.DropAsync().GetAwaiter().GetResult(); + await bucket.DropAsync(); } else {