Skip to content

Commit bfe9403

Browse files
committed
CSHARP-1375: Add sync stack to Core and refactor Legacy API to use new sync stack.
1 parent 699018e commit bfe9403

File tree

177 files changed

+8454
-2371
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+8454
-2371
lines changed

src/MongoDB.Driver.Core.TestHelpers/CoreTestConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ public static DatabaseNamespace GetDatabaseNamespaceForTestFixture()
170170
{
171171
var testFixtureType = GetTestFixtureTypeFromCallStack();
172172
var databaseName = TruncateDatabaseNameIfTooLong(__databaseNamespace.DatabaseName + "-" + testFixtureType.Name);
173+
if (databaseName.Length >= 64)
174+
{
175+
databaseName = databaseName.Substring(0, 63);
176+
}
173177
return new DatabaseNamespace(databaseName);
174178
}
175179

src/MongoDB.Driver.Core.Tests/BatchTransformingAsyncCursorTests.cs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,104 +27,123 @@ namespace MongoDB.Driver
2727
public class BatchTransformingAsyncCursorTests
2828
{
2929
[Test]
30-
public async Task Should_provide_back_all_results()
30+
public void Should_provide_back_all_results(
31+
[Values(false, true)]
32+
bool async)
3133
{
3234
var source = Enumerable.Range(0, 15);
3335
var cursor = new ListBasedAsyncCursor<int>(source, 5);
3436

3537
var subject = new BatchTransformingAsyncCursor<int, string>(cursor, x => x.Select(y => y.ToString()));
3638

37-
var result = await subject.MoveNextAsync(CancellationToken.None);
39+
var result = MoveNext(subject, async);
3840
result.Should().BeTrue();
3941
var batch = subject.Current.ToList();
4042
batch.Should().Equal("0", "1", "2", "3", "4");
4143

42-
result = await subject.MoveNextAsync(CancellationToken.None);
44+
result = MoveNext(subject, async);
4345
result.Should().BeTrue();
4446
batch = subject.Current.ToList();
4547
batch.Should().Equal("5", "6", "7", "8", "9");
4648

47-
result = await subject.MoveNextAsync(CancellationToken.None);
49+
result = MoveNext(subject, async);
4850
result.Should().BeTrue();
4951
batch = subject.Current.ToList();
5052
batch.Should().Equal("10", "11", "12", "13", "14");
5153

52-
result = await subject.MoveNextAsync(CancellationToken.None);
54+
result = MoveNext(subject, async);
5355
result.Should().BeFalse();
5456
}
5557

5658
[Test]
57-
public async Task Should_provide_back_a_filtered_list()
59+
public void Should_provide_back_a_filtered_list(
60+
[Values(false, true)]
61+
bool async)
5862
{
5963
var source = Enumerable.Range(0, 15);
6064
var cursor = new ListBasedAsyncCursor<int>(source, 5);
6165

6266
var subject = new BatchTransformingAsyncCursor<int, int>(cursor, x => x.Where(y => y % 2 == 0));
6367

64-
var result = await subject.MoveNextAsync(CancellationToken.None);
68+
var result = MoveNext(subject, async);
6569
result.Should().BeTrue();
6670
var batch = subject.Current.ToList();
6771
batch.Should().Equal(0, 2, 4);
6872

69-
result = await subject.MoveNextAsync(CancellationToken.None);
73+
result = MoveNext(subject, async);
7074
result.Should().BeTrue();
7175
batch = subject.Current.ToList();
7276
batch.Should().Equal(6, 8);
7377

74-
result = await subject.MoveNextAsync(CancellationToken.None);
78+
result = MoveNext(subject, async);
7579
result.Should().BeTrue();
7680
batch = subject.Current.ToList();
7781
batch.Should().Equal(10, 12, 14);
7882

79-
result = await subject.MoveNextAsync(CancellationToken.None);
83+
result = MoveNext(subject, async);
8084
result.Should().BeFalse();
8185
}
8286

8387
[Test]
84-
public async Task Should_skip_empty_batches()
88+
public void Should_skip_empty_batches(
89+
[Values(false, true)]
90+
bool async)
8591
{
8692
var source = Enumerable.Range(0, 15);
8793
var cursor = new ListBasedAsyncCursor<int>(source, 5);
8894

8995
// skip the second batch
9096
var subject = new BatchTransformingAsyncCursor<int, int>(cursor, x => x.Where(y => y < 5 || y > 9));
9197

92-
var result = await subject.MoveNextAsync(CancellationToken.None);
98+
var result = MoveNext(subject, async);
9399
result.Should().BeTrue();
94100
var batch = subject.Current.ToList();
95101
batch.Should().Equal(0, 1, 2, 3, 4);
96102

97-
result = await subject.MoveNextAsync(CancellationToken.None);
103+
result = MoveNext(subject, async);
98104
result.Should().BeTrue();
99105
batch = subject.Current.ToList();
100106
batch.Should().Equal(10, 11, 12, 13, 14);
101107

102-
result = await subject.MoveNextAsync(CancellationToken.None);
108+
result = MoveNext(subject, async);
103109
result.Should().BeFalse();
104110
}
105111

106112
[Test]
107-
public async Task Should_return_false_when_all_remaining_batches_are_empty()
113+
public void Should_return_false_when_all_remaining_batches_are_empty(
114+
[Values(false, true)]
115+
bool async)
108116
{
109117
var source = Enumerable.Range(0, 15);
110118
var cursor = new ListBasedAsyncCursor<int>(source, 5);
111119

112120
var subject = new BatchTransformingAsyncCursor<int, int>(cursor, x => x.Where(y => y < 8));
113121

114-
var result = await subject.MoveNextAsync(CancellationToken.None);
122+
var result = MoveNext(subject, async);
115123
result.Should().BeTrue();
116124
var batch = subject.Current.ToList();
117125
batch.Should().Equal(0, 1, 2, 3, 4);
118126

119-
result = await subject.MoveNextAsync(CancellationToken.None);
127+
result = MoveNext(subject, async);
120128
result.Should().BeTrue();
121129
batch = subject.Current.ToList();
122130
batch.Should().Equal(5, 6, 7);
123131

124-
result = await subject.MoveNextAsync(CancellationToken.None);
132+
result = MoveNext(subject, async);
125133
result.Should().BeFalse();
126134
}
127135

136+
private bool MoveNext<T>(IAsyncCursor<T> cursor, bool async)
137+
{
138+
if (async)
139+
{
140+
return cursor.MoveNextAsync().GetAwaiter().GetResult();
141+
}
142+
else
143+
{
144+
return cursor.MoveNext();
145+
}
146+
}
128147

129148
private class ListBasedAsyncCursor<T> : IAsyncCursor<T>
130149
{
@@ -152,12 +171,12 @@ public System.Collections.Generic.IEnumerable<T> Current
152171
}
153172
}
154173

155-
public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
174+
public bool MoveNext(CancellationToken cancellationToken)
156175
{
157176
if (_index >= _full.Count)
158177
{
159178
_current = null;
160-
return Task.FromResult(false);
179+
return false;
161180
}
162181

163182
var count = _batchSize;
@@ -167,7 +186,12 @@ public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
167186
}
168187
_current = _full.GetRange(_index, count);
169188
_index += count;
170-
return Task.FromResult(true);
189+
return true;
190+
}
191+
192+
public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
193+
{
194+
return Task.FromResult(MoveNext(cancellationToken));
171195
}
172196

173197
public void Dispose()
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* Copyright 2015 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.Threading;
17+
using System.Threading.Tasks;
18+
using FluentAssertions;
19+
using NUnit.Framework;
20+
21+
namespace MongoDB.Driver.Core.Async
22+
{
23+
[TestFixture]
24+
public class AsyncLockTests
25+
{
26+
[Test]
27+
public void Request_Dispose_should_cancel_request_when_lock_was_not_taken()
28+
{
29+
var subject = new AsyncLock();
30+
var request1 = subject.Request();
31+
var request2 = subject.Request();
32+
var request3 = subject.Request();
33+
34+
request2.Dispose();
35+
request1.Dispose();
36+
37+
request2.Task.Status.Should().Be(TaskStatus.Canceled);
38+
request3.Task.Status.Should().Be(TaskStatus.RanToCompletion);
39+
}
40+
41+
[Test]
42+
public void Request_Dispose_should_give_lock_to_next_waiter_when_there_is_a_waiter()
43+
{
44+
var subject = new AsyncLock();
45+
var request1 = subject.Request();
46+
var request2 = subject.Request();
47+
48+
request1.Dispose();
49+
50+
request2.Task.Status.Should().Be(TaskStatus.RanToCompletion);
51+
}
52+
53+
[Test]
54+
public void Request_Dispose_should_release_lock_when_there_is_no_waiter()
55+
{
56+
var subject = new AsyncLock();
57+
58+
subject.Request().Dispose();
59+
60+
subject.Request().Task.Status.Should().Be(TaskStatus.RanToCompletion);
61+
}
62+
63+
[Test]
64+
public void Request_should_be_cancelled_when_cancellation_is_requested_before_lock_was_taken()
65+
{
66+
var subject = new AsyncLock();
67+
var cts = new CancellationTokenSource();
68+
var cancellationToken = cts.Token;
69+
var request1 = subject.Request();
70+
var request2 = subject.Request(cancellationToken);
71+
var request3 = subject.Request();
72+
73+
cts.Cancel();
74+
request1.Dispose();
75+
76+
request3.Task.Status.Should().Be(TaskStatus.RanToCompletion);
77+
}
78+
79+
[Test]
80+
public void Request_should_not_be_cancelled_when_cancellation_is_requested_after_lock_was_taken()
81+
{
82+
var subject = new AsyncLock();
83+
var cts = new CancellationTokenSource();
84+
var cancellationToken = cts.Token;
85+
var request = subject.Request(cancellationToken);
86+
87+
cts.Cancel();
88+
89+
request.Task.Status.Should().Be(TaskStatus.RanToCompletion);
90+
}
91+
92+
[Test]
93+
public void Request_should_queue_waiter_when_lock_is_alread_taken()
94+
{
95+
var subject = new AsyncLock();
96+
subject.Request();
97+
98+
var result = subject.Request();
99+
100+
result.Task.Status.Should().Be(TaskStatus.WaitingForActivation);
101+
}
102+
103+
[Test]
104+
public void Request_should_take_lock_when_lock_is_not_already_taken()
105+
{
106+
var subject = new AsyncLock();
107+
108+
var result = subject.Request();
109+
110+
result.Task.Status.Should().Be(TaskStatus.RanToCompletion);
111+
}
112+
}
113+
}

src/MongoDB.Driver.Core.Tests/Core/Authentication/AuthenticationHelperTests.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public void MongoPasswordDigest_should_create_the_correct_hash(string username,
4848
}
4949

5050
[Test]
51-
public void AuthenticateAsync_should_invoke_authenticators_when_they_exist()
51+
public void Authenticate_should_invoke_authenticators_when_they_exist(
52+
[Values(false, true)]
53+
bool async)
5254
{
5355
var description = new ConnectionDescription(
5456
new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017))),
@@ -62,13 +64,24 @@ public void AuthenticateAsync_should_invoke_authenticators_when_they_exist()
6264
connection.Description.Returns(description);
6365
connection.Settings.Returns(settings);
6466

65-
AuthenticationHelper.AuthenticateAsync(connection, description, CancellationToken.None).Wait();
67+
if (async)
68+
{
69+
AuthenticationHelper.AuthenticateAsync(connection, description, CancellationToken.None).GetAwaiter().GetResult();
6670

67-
authenticator.ReceivedWithAnyArgs().AuthenticateAsync(null, null, CancellationToken.None);
71+
authenticator.ReceivedWithAnyArgs().AuthenticateAsync(null, null, CancellationToken.None);
72+
}
73+
else
74+
{
75+
AuthenticationHelper.Authenticate(connection, description, CancellationToken.None);
76+
77+
authenticator.ReceivedWithAnyArgs().Authenticate(null, null, CancellationToken.None);
78+
}
6879
}
6980

7081
[Test]
71-
public void AuthenticateAsync_should_not_invoke_authenticators_when_connected_to_an_arbiter()
82+
public void Authenticate_should_not_invoke_authenticators_when_connected_to_an_arbiter(
83+
[Values(false, true)]
84+
bool async)
7285
{
7386
var description = new ConnectionDescription(
7487
new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017))),
@@ -82,9 +95,18 @@ public void AuthenticateAsync_should_not_invoke_authenticators_when_connected_to
8295
connection.Description.Returns(description);
8396
connection.Settings.Returns(settings);
8497

85-
AuthenticationHelper.AuthenticateAsync(connection, description, CancellationToken.None).Wait();
98+
if (async)
99+
{
100+
AuthenticationHelper.AuthenticateAsync(connection, description, CancellationToken.None).GetAwaiter().GetResult();
86101

87-
authenticator.DidNotReceiveWithAnyArgs().AuthenticateAsync(null, null, CancellationToken.None);
102+
authenticator.DidNotReceiveWithAnyArgs().AuthenticateAsync(null, null, CancellationToken.None);
103+
}
104+
else
105+
{
106+
AuthenticationHelper.Authenticate(connection, description, CancellationToken.None);
107+
108+
authenticator.DidNotReceiveWithAnyArgs().Authenticate(null, null, CancellationToken.None);
109+
}
88110
}
89111
}
90112
}

0 commit comments

Comments
 (0)