Skip to content

Commit

Permalink
CSHARP-4245: Dispose CancellationTokenSource in tests (#846)
Browse files Browse the repository at this point in the history
* CSHARP-4245: Dispose CancellationTokenSource in tests
  • Loading branch information
BorisDog authored Jul 13, 2022
1 parent d0b8de5 commit 50db4b7
Show file tree
Hide file tree
Showing 36 changed files with 345 additions and 189 deletions.
2 changes: 1 addition & 1 deletion tests/AstrolabeWorkloadExecutor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void Main(string[] args)
var connectionString = args[0];
var driverWorkload = BsonDocument.Parse(args[1]);

var astrolabeCancellationTokenSource = new CancellationTokenSource();
using var astrolabeCancellationTokenSource = new CancellationTokenSource();
ConsoleCancelEventHandler cancelHandler = (o, e) => HandleCancel(e, astrolabeCancellationTokenSource);

var resultsDir = Environment.GetEnvironmentVariable("RESULTS_DIR") ?? "";
Expand Down
24 changes: 16 additions & 8 deletions tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ public void CopyToAsync_should_call_wrapped_stream()
var subject = new BsonStreamAdapter(mockStream.Object);
var mockDestination = new Mock<Stream>();
var bufferSize = 1;
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
var task = new TaskCompletionSource<object>().Task;
mockStream.Setup(s => s.CopyToAsync(mockDestination.Object, bufferSize, cancellationToken)).Returns(task);

Expand All @@ -322,7 +323,8 @@ public void CopyToAsync_should_throw_when_subject_is_disposed()
var subject = new BsonStreamAdapter(mockStream.Object);
var mockDestination = new Mock<Stream>();
var bufferSize = 1;
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
subject.Dispose();

Action action = () => subject.CopyToAsync(mockDestination.Object, bufferSize, cancellationToken);
Expand Down Expand Up @@ -463,7 +465,8 @@ public void FlushAsync_should_call_wrapped_stream()
var mockStream = new Mock<Stream>();
var subject = new BsonStreamAdapter(mockStream.Object);
var task = new TaskCompletionSource<object>().Task;
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
mockStream.Setup(s => s.FlushAsync(cancellationToken)).Returns(task);

var result = subject.FlushAsync(cancellationToken);
Expand All @@ -477,7 +480,8 @@ public void FlushAsync_should_throw_when_subject_is_disposed()
{
var mockStream = new Mock<Stream>();
var subject = new BsonStreamAdapter(mockStream.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
subject.Dispose();

Action action = () => subject.FlushAsync(cancellationToken);
Expand Down Expand Up @@ -608,7 +612,8 @@ public void ReadAsync_should_call_wrapped_stream()
var buffer = new byte[3];
var offset = 1;
var count = 2;
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
mockStream.Setup(s => s.ReadAsync(buffer, offset, count, cancellationToken)).Returns(task);

var result = subject.ReadAsync(buffer, offset, count, cancellationToken);
Expand All @@ -625,7 +630,8 @@ public void ReadAsync_should_throw_when_subject_is_disposed()
var buffer = new byte[3];
var offset = 1;
var count = 2;
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
subject.Dispose();

Action action = () => subject.ReadAsync(buffer, offset, count, cancellationToken);
Expand Down Expand Up @@ -1185,7 +1191,8 @@ public void WriteAsync_should_call_wrapped_stream()
var buffer = new byte[0];
var offset = 1;
var count = 2;
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
mockStream.Setup(s => s.WriteAsync(buffer, offset, count, cancellationToken)).Returns(task);

var result = subject.WriteAsync(buffer, offset, count, cancellationToken);
Expand All @@ -1203,7 +1210,8 @@ public void WriteAsync_should_throw_when_subject_is_disposed()
var buffer = new byte[0];
var offset = 1;
var count = 2;
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
subject.Dispose();

Action action = () => subject.WriteAsync(buffer, offset, count, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using MongoDB.Driver.Core.Async;
using Xunit;

namespace MongoDB.Driver.Core.Async
Expand Down Expand Up @@ -88,7 +87,7 @@ public void Dequeue_should_complete_when_an_item_is_added_to_the_queue()
[Fact]
public void Dequeue_should_cancel_when_cancellation_is_requested()
{
var cancellationTokenSource = new CancellationTokenSource();
using var cancellationTokenSource = new CancellationTokenSource();
var subject = new AsyncQueue<int>();
var result = subject.DequeueAsync(cancellationTokenSource.Token);
cancellationTokenSource.Cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public void GetChannel_should_return_expected_result(
{
var mockChannel = new Mock<IChannelHandle>();
var subject = CreateSubject(channel: mockChannel.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
var expectedResult = new Mock<IChannelHandle>().Object;
mockChannel.Setup(m => m.Fork()).Returns(expectedResult);

Expand All @@ -169,7 +170,8 @@ public void GetChannel_should_throw_when_disposed(
[Values(false, true)] bool async)
{
var subject = CreateDisposedSubject();
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var exception = Record.Exception(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public void GetReadChannelSource_should_return_expected_result(
var mockChannel = new Mock<IChannelHandle>();
var mockSession = new Mock<ICoreSessionHandle>();
var subject = CreateSubject(channel: mockChannel.Object, session: mockSession.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var forkedChannel = new Mock<IChannelHandle>().Object;
var forkedSession = new Mock<ICoreSessionHandle>().Object;
Expand Down Expand Up @@ -187,7 +188,8 @@ public void GetReadChannelSource_should_throw_when_disposed(
[Values(false, true)] bool async)
{
var subject = CreateDisposedSubject();
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var exception = Record.Exception(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public void GetReadChannelSource_should_return_expected_result(
var mockChannel = new Mock<IChannelHandle>();
var mockSession = new Mock<ICoreSessionHandle>();
var subject = CreateSubject(channel: mockChannel.Object, session: mockSession.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var forkedChannel = new Mock<IChannelHandle>().Object;
var forkedSession = new Mock<ICoreSessionHandle>().Object;
Expand Down Expand Up @@ -170,7 +171,8 @@ public void GetWriteChannelSource_should_return_expected_result(
var mockChannel = new Mock<IChannelHandle>();
var mockSession = new Mock<ICoreSessionHandle>();
var subject = CreateSubject(channel: mockChannel.Object, session: mockSession.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var forkedChannel = new Mock<IChannelHandle>().Object;
var forkedSession = new Mock<ICoreSessionHandle>().Object;
Expand Down Expand Up @@ -200,7 +202,8 @@ public void GetReadChannelSource_should_throw_when_disposed(
[Values(false, true)] bool async)
{
var subject = CreateDisposedSubject();
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var exception = Record.Exception(() =>
{
Expand All @@ -224,7 +227,8 @@ public void GetWriteChannelSource_should_throw_when_disposed(
[Values(false, true)] bool async)
{
var subject = CreateDisposedSubject();
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var exception = Record.Exception(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ public void GetReadChannelSource_should_fork_the_session(
{
var mockSession = new Mock<ICoreSessionHandle>();
var subject = new ReadPreferenceBinding(_mockCluster.Object, ReadPreference.Primary, mockSession.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var selectedServer = new Mock<IServer>().Object;
_mockCluster.Setup(m => m.SelectServer(It.IsAny<IServerSelector>(), cancellationToken)).Returns(selectedServer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public void GetReadChannelSource_should_return_expected_result(
{
var mockSession = new Mock<ICoreSessionHandle>();
var subject = CreateSubject(session: mockSession.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var forkedSession = new Mock<ICoreSessionHandle>().Object;
mockSession.Setup(m => m.Fork()).Returns(forkedSession);
Expand All @@ -158,7 +159,8 @@ public void GetReadChannelSource_should_throw_when_disposed(
[Values(false, true)] bool async)
{
var subject = CreateDisposedSubject();
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var exception = Record.Exception(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public void GetReadChannelSource_should_return_expected_result(
{
var mockSession = new Mock<ICoreSessionHandle>();
var subject = CreateSubject(session: mockSession.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var forkedSession = new Mock<ICoreSessionHandle>().Object;
mockSession.Setup(m => m.Fork()).Returns(forkedSession);
Expand All @@ -144,7 +145,8 @@ public void GetReadChannelSource_should_throw_when_disposed(
[Values(false, true)] bool async)
{
var subject = CreateDisposedSubject();
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var exception = Record.Exception(() =>
{
Expand All @@ -169,7 +171,8 @@ public void GetWriteChannelSource_should_return_expected_result(
{
var mockSession = new Mock<ICoreSessionHandle>();
var subject = CreateSubject(session: mockSession.Object);
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var forkedSession = new Mock<ICoreSessionHandle>().Object;
mockSession.Setup(m => m.Fork()).Returns(forkedSession);
Expand All @@ -196,7 +199,8 @@ public void GetWriteChannelSource_should_throw_when_disposed(
[Values(false, true)] bool async)
{
var subject = CreateDisposedSubject();
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var exception = Record.Exception(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ public void ResolveSrvRecords_should_throw_when_cancellation_is_already_requeste
{
var subject = CreateSubject();
var service = "_mongodb._tcp.test5.test.build.10gen.cc";
var cts = new CancellationTokenSource();
cts.Cancel();
using var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();

Exception exception;
if (async)
{
exception = Record.Exception(() => subject.ResolveSrvRecordsAsync(service, cts.Token).GetAwaiter().GetResult());
exception = Record.Exception(() => subject.ResolveSrvRecordsAsync(service, cancellationTokenSource.Token).GetAwaiter().GetResult());
}
else
{
exception = Record.Exception(() => subject.ResolveSrvRecords(service, cts.Token));
exception = Record.Exception(() => subject.ResolveSrvRecords(service, cancellationTokenSource.Token));
}

exception.Should().Match<Exception>(e => e is OperationCanceledException || e.InnerException is OperationCanceledException);
Expand Down Expand Up @@ -159,17 +159,17 @@ public void ResolveTxtRecords_should_throw_when_cancellation_is_already_requeste
{
var subject = CreateSubject();
var domainName = "test5.test.build.10gen.cc";
var cts = new CancellationTokenSource();
cts.Cancel();
using var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();

Exception exception;
if (async)
{
exception = Record.Exception(() => subject.ResolveTxtRecordsAsync(domainName, cts.Token).GetAwaiter().GetResult());
exception = Record.Exception(() => subject.ResolveTxtRecordsAsync(domainName, cancellationTokenSource.Token).GetAwaiter().GetResult());
}
else
{
exception = Record.Exception(() => subject.ResolveTxtRecords(domainName, cts.Token));
exception = Record.Exception(() => subject.ResolveTxtRecords(domainName, cancellationTokenSource.Token));
}

exception.Should().Match<Exception>(e => e is OperationCanceledException || e.InnerException is OperationCanceledException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void CreateDnsMonitor_should_return_expected_result()
var subject = new DnsMonitorFactory(mockEventSubscriber.Object);
var cluster = Mock.Of<IDnsMonitoringCluster>();
var lookupDomainName = "a.b.com";
var cancellationToken = new CancellationTokenSource().Token;
using var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

var result = subject.CreateDnsMonitor(cluster, lookupDomainName, cancellationToken);

Expand Down
Loading

0 comments on commit 50db4b7

Please sign in to comment.