Skip to content

Commit 9b311b0

Browse files
committed
CSHARP-5930: Enable SmoteTests and Driver.Examples running locally
I have one env var set: MONGODB_URI I'm using Atlas Local 8.2, which is a standalone cluster by default, but tests that pass were being excluded from running. If people are using non-Atlas and this breaks, then we'll have to figure something out.
1 parent e71abe2 commit 9b311b0

File tree

13 files changed

+102
-55
lines changed

13 files changed

+102
-55
lines changed

src/MongoDB.Driver/Encryption/AutoEncryptionProviderRegistry.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ internal sealed class AutoEncryptionProviderRegistry : IAutoEncryptionProviderRe
2828

2929
public void Register(Func<IMongoClient, AutoEncryptionOptions, IAutoEncryptionLibMongoCryptController> factory)
3030
{
31+
if(ReferenceEquals(factory, _autoCryptClientControllerFactory))
32+
{
33+
return;
34+
}
35+
3136
if (_autoCryptClientControllerFactory != null)
3237
{
3338
throw new MongoConfigurationException("AutoEncryption Provider already registered.");
3439
}
40+
3541
_autoCryptClientControllerFactory = factory;
3642
}
3743

tests/MongoDB.Driver.Examples/CausalConsistencyExamples.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ public void Causal_Consistency_Example_3()
120120
DropCollection(CreateClient(), "myDatabase", "myCollection");
121121

122122
// Start Tunable Consistency Controls Example
123-
var connectionString = "mongodb://localhost/?readPreference=secondaryPreferred";
123+
var clientSettings = MongoClientSettings.FromConnectionString(CoreTestConfiguration.ConnectionString.ToString());
124+
clientSettings.ReadPreference = ReadPreference.SecondaryPreferred;
124125

125-
var client = new MongoClient(connectionString);
126+
var client = new MongoClient(clientSettings);
126127
var database = client.GetDatabase("myDatabase");
127128
var collection = database.GetCollection<BsonDocument>("myCollection");
128129

@@ -147,11 +148,7 @@ public void Causal_Consistency_Example_3()
147148
result.Should().Be("{ name: 'John' }");
148149
}
149150

150-
private IMongoClient CreateClient()
151-
{
152-
var connectionString = CoreTestConfiguration.ConnectionString.ToString();
153-
return new MongoClient(connectionString);
154-
}
151+
private IMongoClient CreateClient() => new MongoClient(CoreTestConfiguration.ConnectionString.ToString());
155152

156153
private void DropCollection(IMongoClient client, string databaseName, string collectionName)
157154
{

tests/MongoDB.Driver.Examples/ChangeStreamExamples.cs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using System;
1717
using System.Linq;
1818
using System.Threading;
19-
using System.Threading.Tasks;
2019
using FluentAssertions;
2120
using MongoDB.Bson;
2221
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
@@ -37,12 +36,13 @@ public void ChangeStreamExample1()
3736
var inventory = database.GetCollection<BsonDocument>("inventory");
3837

3938
var document = new BsonDocument("x", 1);
40-
new Thread(() =>
39+
var thread = new Thread(() =>
4140
{
4241
Thread.Sleep(TimeSpan.FromMilliseconds(100));
4342
inventory.InsertOne(document);
44-
})
45-
.Start();
43+
});
44+
45+
thread.Start();
4646

4747
// Start Changestream Example 1
4848
var cursor = inventory.Watch();
@@ -52,6 +52,9 @@ public void ChangeStreamExample1()
5252
// End Changestream Example 1
5353

5454
next.FullDocument.Should().Be(document);
55+
56+
// Make sure worker thread is finished before we let the next test run.
57+
thread.Join();
5558
}
5659

5760
[Fact]
@@ -64,14 +67,15 @@ public void ChangeStreamExample2()
6467

6568
var document = new BsonDocument("x", 1);
6669
inventory.InsertOne(document);
67-
new Thread(() =>
70+
var thread = new Thread(() =>
6871
{
6972
Thread.Sleep(TimeSpan.FromMilliseconds(100));
7073
var filter = new BsonDocument("_id", document["_id"]);
7174
var update = "{ $set : { x : 2 } }";
7275
inventory.UpdateOne(filter, update);
73-
})
74-
.Start();
76+
});
77+
78+
thread.Start();
7579

7680
// Start Changestream Example 2
7781
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
@@ -83,6 +87,9 @@ public void ChangeStreamExample2()
8387

8488
var expectedFullDocument = document.Set("x", 2);
8589
next.FullDocument.Should().Be(expectedFullDocument);
90+
91+
// Make sure worker thread is finished before we let the next test run.
92+
thread.Join();
8693
}
8794

8895
[Fact]
@@ -101,15 +108,19 @@ public void ChangeStreamExample3()
101108

102109
IChangeStreamCursor<ChangeStreamDocument<BsonDocument>> previousCursor;
103110
{
104-
new Thread(() =>
111+
var thread = new Thread(() =>
105112
{
106113
Thread.Sleep(TimeSpan.FromMilliseconds(100));
107114
inventory.InsertMany(documents);
108-
})
109-
.Start();
115+
});
116+
117+
thread.Start();
110118

111119
previousCursor = inventory.Watch(new ChangeStreamOptions { BatchSize = 1 });
112120
while (previousCursor.MoveNext() && previousCursor.Current.Count() == 0) { } // keep calling MoveNext until we've read the first batch
121+
122+
// Make sure worker thread is finished before we let the next test run.
123+
thread.Join();
113124
}
114125

115126
{
@@ -135,22 +146,24 @@ public void ChangestreamExample4()
135146
var database = client.GetDatabase("ChangeStreamExamples");
136147
database.DropCollection("inventory");
137148

138-
using var cancelationTokenSource = new CancellationTokenSource();
139-
try
149+
var stopEvent = new ManualResetEventSlim(false);
150+
var document = new BsonDocument("username", "alice");
151+
152+
var thread = new Thread(() =>
140153
{
141-
var document = new BsonDocument("username", "alice");
154+
var inventoryCollection = database.GetCollection<BsonDocument>("inventory");
142155

143-
Task.Run(() =>
156+
while (!stopEvent.IsSet)
144157
{
145-
var inventoryCollection = database.GetCollection<BsonDocument>("inventory");
158+
Thread.Sleep(TimeSpan.FromMilliseconds(100));
159+
document["_id"] = ObjectId.GenerateNewId();
160+
inventoryCollection.InsertOne(document);
161+
}
162+
});
146163

147-
while (!cancelationTokenSource.IsCancellationRequested)
148-
{
149-
Thread.Sleep(TimeSpan.FromMilliseconds(100));
150-
document["_id"] = ObjectId.GenerateNewId();
151-
inventoryCollection.InsertOne(document);
152-
}
153-
}, cancelationTokenSource.Token);
164+
try
165+
{
166+
thread.Start();
154167

155168
// Start Changestream Example 4
156169
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>()
@@ -170,7 +183,12 @@ public void ChangestreamExample4()
170183
}
171184
finally
172185
{
173-
cancelationTokenSource.Cancel();
186+
stopEvent.Set();
187+
188+
// Make sure the worker thread is finished before we let the next test run.
189+
// Especially important for this test case since the thread continually inserts documents.
190+
thread.Join();
191+
stopEvent.Dispose();
174192
}
175193
}
176194
}

tests/MongoDB.Driver.Examples/ClientEncryptionExamples.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ public void ClientSideEncryptionSimpleTour()
5656
var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault");
5757
var autoEncryptionOptions = new AutoEncryptionOptions(keyVaultNamespace, kmsProviders);
5858

59-
var mongoClientSettings = new MongoClientSettings
60-
{
61-
AutoEncryptionOptions = autoEncryptionOptions
62-
};
59+
var mongoClientSettings = MongoClientSettings.FromConnectionString(InfrastructureUtilities.MongoUri);
60+
mongoClientSettings.AutoEncryptionOptions = autoEncryptionOptions;
6361

6462
var client = new MongoClient(mongoClientSettings);
6563
var database = client.GetDatabase("test");
@@ -87,7 +85,7 @@ public void ClientSideEncryptionAutoEncryptionSettingsTour()
8785
kmsProviders.Add("local", localKey);
8886

8987
var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault");
90-
var keyVaultMongoClient = new MongoClient();
88+
var keyVaultMongoClient = new MongoClient(InfrastructureUtilities.MongoUri);
9189
var clientEncryptionSettings = new ClientEncryptionOptions(
9290
keyVaultMongoClient,
9391
keyVaultNamespace,
@@ -124,10 +122,9 @@ public void ClientSideEncryptionAutoEncryptionSettingsTour()
124122
{
125123
{ collectionNamespace.ToString(), BsonDocument.Parse(schemaMap) }
126124
});
127-
var clientSettings = new MongoClientSettings
128-
{
129-
AutoEncryptionOptions = autoEncryptionSettings
130-
};
125+
126+
var clientSettings = MongoClientSettings.FromConnectionString(InfrastructureUtilities.MongoUri);
127+
clientSettings.AutoEncryptionOptions = autoEncryptionSettings;
131128

132129
var client = new MongoClient(clientSettings);
133130
var database = client.GetDatabase("test");

tests/MongoDB.Driver.Examples/ClientSideEncryption2Examples.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ClientSideEncryption2Examples
3737
[Fact]
3838
public void FLE2AutomaticEncryption()
3939
{
40-
RequireServer.Check().Supports(Feature.Csfle2).ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.LoadBalanced);
40+
RequireServer.Check().Supports(Feature.Csfle2).ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.LoadBalanced, ClusterType.Standalone);
4141

4242
var unencryptedClient = DriverTestConfiguration.Client;
4343

@@ -52,7 +52,7 @@ public void FLE2AutomaticEncryption()
5252
};
5353
kmsProviders.Add("local", localKey);
5454

55-
var keyVaultClient = new MongoClient();
55+
var keyVaultClient = new MongoClient(InfrastructureUtilities.MongoUri);
5656

5757
// Create two data keys.
5858
var clientEncryptionOptions = new ClientEncryptionOptions(keyVaultClient, KeyVaultNamespace, kmsProviders);
@@ -92,7 +92,9 @@ public void FLE2AutomaticEncryption()
9292
};
9393

9494
var autoEncryptionOptions = new AutoEncryptionOptions(KeyVaultNamespace, kmsProviders, encryptedFieldsMap: encryptedFieldsMap);
95-
var encryptedClient = new MongoClient(new MongoClientSettings { AutoEncryptionOptions = autoEncryptionOptions });
95+
var mongoClientSettings = MongoClientSettings.FromConnectionString(InfrastructureUtilities.MongoUri);
96+
mongoClientSettings.AutoEncryptionOptions = autoEncryptionOptions;
97+
var encryptedClient = new MongoClient(mongoClientSettings);
9698

9799
// Create an FLE 2 collection.
98100
var database = encryptedClient.GetDatabase(CollectionNamespace.DatabaseNamespace.DatabaseName);

tests/MongoDB.Driver.Examples/DocumentationExamples.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ public void Example_50()
987987
// End Example 50
988988

989989
Render(filter).Should().Be("{ status: \"A\" }");
990-
Render(projection).Should().Be("{ item: 1, status: 1, instock: { $slice: -1 } }");
990+
Render(projection).Should().Be("{ item: 1, status: 1, instock: { $slice: [ '$instock', -1 ] } }");
991991
}
992992

993993
[Fact]

tests/MongoDB.Driver.Examples/ExplicitEncryptionExamples.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public void ClientSideExplicitEncryptionAndDecryptionTour()
5454
kmsProviders.Add("local", localKey);
5555

5656
var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault");
57-
var keyVaultClient = new MongoClient("mongodb://localhost");
57+
var clientSettings = MongoClientSettings.FromConnectionString(InfrastructureUtilities.MongoUri);
58+
var keyVaultClient = new MongoClient(clientSettings);
5859
var keyVaultDatabase = keyVaultClient.GetDatabase(keyVaultNamespace.DatabaseNamespace.DatabaseName);
5960
keyVaultDatabase.DropCollection(keyVaultNamespace.CollectionName);
6061

@@ -109,14 +110,15 @@ public void ClientSideExplicitEncryptionAndAutoDecryptionTour()
109110
keyVaultNamespace,
110111
kmsProviders,
111112
bypassAutoEncryption: true);
112-
var clientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost");
113+
var clientSettings = MongoClientSettings.FromConnectionString(InfrastructureUtilities.MongoUri);
113114
clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
114115
var mongoClient = new MongoClient(clientSettings);
115116
var database = mongoClient.GetDatabase(collectionNamespace.DatabaseNamespace.DatabaseName);
116117
database.DropCollection(collectionNamespace.CollectionName);
117118
var collection = database.GetCollection<BsonDocument>(collectionNamespace.CollectionName);
118119

119-
var keyVaultClient = new MongoClient("mongodb://localhost");
120+
var keyVaultClientSettings = MongoClientSettings.FromConnectionString(InfrastructureUtilities.MongoUri);
121+
var keyVaultClient = new MongoClient(keyVaultClientSettings);
120122
var keyVaultDatabase = keyVaultClient.GetDatabase(keyVaultNamespace.DatabaseNamespace.DatabaseName);
121123
keyVaultDatabase.DropCollection(keyVaultNamespace.CollectionName);
122124

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Copyright 2017-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace MongoDB.Driver.Examples;
19+
20+
internal static class InfrastructureUtilities
21+
{
22+
public static readonly string MongoUri = Environment.GetEnvironmentVariable("MONGODB_URI") ??
23+
Environment.GetEnvironmentVariable("MONGO_URI") ??
24+
"mongodb://localhost";
25+
}

tests/MongoDB.Driver.Examples/StableApiExamples.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class StableApiExamples
2626
public void ConfigureServerApi()
2727
{
2828
// Start Stable API Example 1
29-
var connectionString = "mongodb://localhost";
29+
var connectionString = InfrastructureUtilities.MongoUri;
3030
var serverApi = new ServerApi(ServerApiVersion.V1);
3131
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
3232
mongoClientSettings.ServerApi = serverApi;
@@ -38,7 +38,7 @@ public void ConfigureServerApi()
3838
public void ConfigureServerApiStrict()
3939
{
4040
// Start Stable API Example 2
41-
var connectionString = "mongodb://localhost";
41+
var connectionString = InfrastructureUtilities.MongoUri;
4242
var serverApi = new ServerApi(ServerApiVersion.V1, strict: true);
4343
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
4444
mongoClientSettings.ServerApi = serverApi;
@@ -50,7 +50,7 @@ public void ConfigureServerApiStrict()
5050
public void ConfigureServerApiNonStrict()
5151
{
5252
// Start Stable API Example 3
53-
var connectionString = "mongodb://localhost";
53+
var connectionString = InfrastructureUtilities.MongoUri;
5454
var serverApi = new ServerApi(ServerApiVersion.V1, strict: false);
5555
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
5656
mongoClientSettings.ServerApi = serverApi;
@@ -62,7 +62,7 @@ public void ConfigureServerApiNonStrict()
6262
public void ConfigureServerApiDeprecationErrors()
6363
{
6464
// Start Stable API Example 4
65-
var connectionString = "mongodb://localhost";
65+
var connectionString = InfrastructureUtilities.MongoUri;
6666
var serverApi = new ServerApi(ServerApiVersion.V1, deprecationErrors: true);
6767
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
6868
mongoClientSettings.ServerApi = serverApi;
@@ -73,7 +73,7 @@ public void ConfigureServerApiDeprecationErrors()
7373
[Fact]
7474
public void StableAPI_Strict_Migration_Example()
7575
{
76-
var connectionString = "mongodb://localhost";
76+
var connectionString = InfrastructureUtilities.MongoUri;
7777
var serverApi = new ServerApi(ServerApiVersion.V1, strict: true);
7878
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
7979
mongoClientSettings.ServerApi = serverApi;

tests/MongoDB.Driver.Examples/TransactionExamplesForDocs/WithTransactionExample1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class WithTransactionExample1
2929
[Fact]
3030
public void Example1()
3131
{
32-
RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded).Supports(Feature.Transactions);
32+
RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.Standalone).Supports(Feature.Transactions);
3333

3434
var connectionString = CoreTestConfiguration.ConnectionString.ToString();
3535
DropCollections(

0 commit comments

Comments
 (0)