Skip to content

Commit f52fb37

Browse files
author
Oleksandr Poliakov
committed
Fix broken tests
1 parent 1141d7f commit f52fb37

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

src/MongoDB.Driver/MongoClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public MongoClient(string connectionString)
104104
internal MongoClient(IOperationExecutor operationExecutor, MongoClientSettings settings)
105105
: this(settings)
106106
{
107-
_operationExecutor = operationExecutor;
107+
_operationExecutor = new OperationExecutorWrapper(operationExecutor);
108108
}
109109

110110
// public properties
@@ -442,7 +442,7 @@ public IMongoClient WithReadConcern(ReadConcern readConcern)
442442

443443
var newSettings = Settings.Clone();
444444
newSettings.ReadConcern = readConcern;
445-
return new MongoClient(newSettings);
445+
return new MongoClient(_operationExecutor, newSettings);
446446
}
447447

448448
/// <inheritdoc/>
@@ -454,7 +454,7 @@ public IMongoClient WithReadPreference(ReadPreference readPreference)
454454

455455
var newSettings = Settings.Clone();
456456
newSettings.ReadPreference = readPreference;
457-
return new MongoClient(newSettings);
457+
return new MongoClient(_operationExecutor, newSettings);
458458
}
459459

460460
/// <inheritdoc/>
@@ -466,7 +466,7 @@ public IMongoClient WithWriteConcern(WriteConcern writeConcern)
466466

467467
var newSettings = Settings.Clone();
468468
newSettings.WriteConcern = writeConcern;
469-
return new MongoClient(newSettings);
469+
return new MongoClient(_operationExecutor, newSettings);
470470
}
471471

472472
// private methods
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* Copyright 2010-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+
using System.Threading;
18+
using System.Threading.Tasks;
19+
using MongoDB.Driver.Core.Operations;
20+
21+
namespace MongoDB.Driver
22+
{
23+
/// <summary>
24+
/// Wraps provided IOperationExecutor to preserve it from disposal.
25+
/// Used by MongoClient when IOperationExecutor is injected from outside.
26+
/// </summary>
27+
internal sealed class OperationExecutorWrapper : IOperationExecutor
28+
{
29+
private readonly IOperationExecutor _wrappedExecutor;
30+
private bool _isDisposed;
31+
32+
public OperationExecutorWrapper(IOperationExecutor executor)
33+
{
34+
_wrappedExecutor = executor;
35+
}
36+
37+
public void Dispose()
38+
{
39+
_isDisposed = true;
40+
}
41+
42+
public TResult ExecuteReadOperation<TResult>(
43+
IReadOperation<TResult> operation,
44+
ReadOperationOptions options,
45+
IClientSessionHandle session = null,
46+
CancellationToken cancellationToken = default)
47+
{
48+
ThrowIfDisposed();
49+
return _wrappedExecutor.ExecuteReadOperation(operation, options, session, cancellationToken);
50+
}
51+
52+
public Task<TResult> ExecuteReadOperationAsync<TResult>(
53+
IReadOperation<TResult> operation,
54+
ReadOperationOptions options,
55+
IClientSessionHandle session = null,
56+
CancellationToken cancellationToken = default)
57+
{
58+
ThrowIfDisposed();
59+
return _wrappedExecutor.ExecuteReadOperationAsync(operation, options, session, cancellationToken);
60+
}
61+
62+
public TResult ExecuteWriteOperation<TResult>(
63+
IWriteOperation<TResult> operation,
64+
WriteOperationOptions options,
65+
IClientSessionHandle session = null,
66+
CancellationToken cancellationToken = default)
67+
{
68+
ThrowIfDisposed();
69+
return _wrappedExecutor.ExecuteWriteOperation(operation, options, session, cancellationToken);
70+
}
71+
72+
public Task<TResult> ExecuteWriteOperationAsync<TResult>(
73+
IWriteOperation<TResult> operation,
74+
WriteOperationOptions options,
75+
IClientSessionHandle session = null,
76+
CancellationToken cancellationToken = default)
77+
{
78+
ThrowIfDisposed();
79+
return _wrappedExecutor.ExecuteWriteOperationAsync(operation, options, session, cancellationToken);
80+
}
81+
82+
public IClientSessionHandle StartImplicitSession(CancellationToken cancellationToken)
83+
{
84+
ThrowIfDisposed();
85+
return _wrappedExecutor.StartImplicitSession(cancellationToken);
86+
}
87+
88+
public Task<IClientSessionHandle> StartImplicitSessionAsync(CancellationToken cancellationToken)
89+
{
90+
ThrowIfDisposed();
91+
return _wrappedExecutor.StartImplicitSessionAsync(cancellationToken);
92+
}
93+
94+
private void ThrowIfDisposed()
95+
{
96+
if (_isDisposed)
97+
{
98+
throw new ObjectDisposedException(nameof(OperationExecutorWrapper));
99+
}
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)