Skip to content

Commit 495a645

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 495a645

13 files changed

Lines changed: 97 additions & 50 deletions

File tree

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: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ public void ChangeStreamExample1()
3737
var inventory = database.GetCollection<BsonDocument>("inventory");
3838

3939
var document = new BsonDocument("x", 1);
40-
new Thread(() =>
40+
var thread = new Thread(() =>
4141
{
4242
Thread.Sleep(TimeSpan.FromMilliseconds(100));
4343
inventory.InsertOne(document);
44-
})
45-
.Start();
44+
});
45+
46+
thread.Start();
4647

4748
// Start Changestream Example 1
4849
var cursor = inventory.Watch();
@@ -52,6 +53,9 @@ public void ChangeStreamExample1()
5253
// End Changestream Example 1
5354

5455
next.FullDocument.Should().Be(document);
56+
57+
// Make sure worker thread is finished before we let the next test run.
58+
thread.Join();
5559
}
5660

5761
[Fact]
@@ -64,14 +68,15 @@ public void ChangeStreamExample2()
6468

6569
var document = new BsonDocument("x", 1);
6670
inventory.InsertOne(document);
67-
new Thread(() =>
71+
var thread = new Thread(() =>
6872
{
6973
Thread.Sleep(TimeSpan.FromMilliseconds(100));
7074
var filter = new BsonDocument("_id", document["_id"]);
7175
var update = "{ $set : { x : 2 } }";
7276
inventory.UpdateOne(filter, update);
73-
})
74-
.Start();
77+
});
78+
79+
thread.Start();
7580

7681
// Start Changestream Example 2
7782
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
@@ -83,6 +88,9 @@ public void ChangeStreamExample2()
8388

8489
var expectedFullDocument = document.Set("x", 2);
8590
next.FullDocument.Should().Be(expectedFullDocument);
91+
92+
// Make sure worker thread is finished before we let the next test run.
93+
thread.Join();
8694
}
8795

8896
[Fact]
@@ -101,15 +109,19 @@ public void ChangeStreamExample3()
101109

102110
IChangeStreamCursor<ChangeStreamDocument<BsonDocument>> previousCursor;
103111
{
104-
new Thread(() =>
112+
var thread = new Thread(() =>
105113
{
106114
Thread.Sleep(TimeSpan.FromMilliseconds(100));
107115
inventory.InsertMany(documents);
108-
})
109-
.Start();
116+
});
117+
118+
thread.Start();
110119

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

115127
{
@@ -135,22 +147,24 @@ public void ChangestreamExample4()
135147
var database = client.GetDatabase("ChangeStreamExamples");
136148
database.DropCollection("inventory");
137149

138-
using var cancelationTokenSource = new CancellationTokenSource();
139-
try
150+
var workerEnabled = true;
151+
var document = new BsonDocument("username", "alice");
152+
153+
var thread = new Thread(() =>
140154
{
141-
var document = new BsonDocument("username", "alice");
155+
var inventoryCollection = database.GetCollection<BsonDocument>("inventory");
142156

143-
Task.Run(() =>
157+
while (workerEnabled)
144158
{
145-
var inventoryCollection = database.GetCollection<BsonDocument>("inventory");
159+
Thread.Sleep(TimeSpan.FromMilliseconds(100));
160+
document["_id"] = ObjectId.GenerateNewId();
161+
inventoryCollection.InsertOne(document);
162+
}
163+
});
146164

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

155169
// Start Changestream Example 4
156170
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>()
@@ -170,7 +184,11 @@ public void ChangestreamExample4()
170184
}
171185
finally
172186
{
173-
cancelationTokenSource.Cancel();
187+
workerEnabled = false;
188+
189+
// Make sure worker thread is finished before we let the next test run.
190+
// Especially important for this test case since the thread continually inserts documents.
191+
thread.Join();
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)