-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5598: Fix CoreServerSessionPool.IsAboutToExpire test #1699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,10 @@ | |
using System.Linq; | ||
using System.Net; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using MongoDB.Driver.Core.Bindings; | ||
using MongoDB.Driver.Core.Clusters; | ||
using MongoDB.Driver.Core.Clusters.ServerSelectors; | ||
using MongoDB.Driver.Core.Configuration; | ||
using MongoDB.Driver.Core.Misc; | ||
using MongoDB.Driver.Core.Servers; | ||
using MongoDB.Driver.Encryption; | ||
using Moq; | ||
using Xunit; | ||
|
||
|
@@ -205,35 +199,23 @@ public void ReleaseSession_should_have_expected_result(int[] pooledSessionWasRec | |
} | ||
} | ||
|
||
[Fact] | ||
public void IsAboutToExpire_should_never_expire_in_load_balancing_mode() | ||
{ | ||
var subject = CreateSubject(); | ||
var mockedCluster = new TestCluster(ClusterType.LoadBalanced); | ||
var mockedServerSessionPool = new CoreServerSessionPool(mockedCluster); | ||
var mockSession = new Mock<ICoreServerSession>(); | ||
var lastUsedAt = DateTime.UtcNow.AddSeconds(1741); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line of code is incorrect, we should add negative seconds to make session looks expired (see line 231 before the changes). |
||
mockSession.SetupGet(m => m.LastUsedAt).Returns(lastUsedAt); | ||
|
||
var result = mockedServerSessionPool.IsAboutToExpire(mockSession.Object); | ||
|
||
result.Should().BeFalse(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, true)] | ||
[InlineData(1741, true)] | ||
[InlineData(1739, false)] | ||
public void IsAboutToExpire_should_return_expected_result(int? lastUsedSecondsAgo, bool expectedResult) | ||
[InlineData(ClusterType.Sharded, null, true)] | ||
[InlineData(ClusterType.Sharded, 1741, true)] | ||
[InlineData(ClusterType.Sharded, 1739, false)] | ||
[InlineData(ClusterType.LoadBalanced, null, false)] | ||
[InlineData(ClusterType.LoadBalanced, 1741, false)] | ||
[InlineData(ClusterType.LoadBalanced, 1739, false)] | ||
public void IsAboutToExpire_should_return_expected_result(ClusterType clusterType, int? lastUsedSecondsAgo, bool isAboutToExpire) | ||
{ | ||
var subject = CreateSubject(); | ||
var subject = CreateSubject(clusterType); | ||
var mockSession = new Mock<ICoreServerSession>(); | ||
var lastUsedAt = lastUsedSecondsAgo == null ? (DateTime?)null : DateTime.UtcNow.AddSeconds(-lastUsedSecondsAgo.Value); | ||
mockSession.SetupGet(m => m.LastUsedAt).Returns(lastUsedAt); | ||
|
||
var result = subject.IsAboutToExpire(mockSession.Object); | ||
|
||
result.Should().Be(expectedResult); | ||
result.Should().Be(isAboutToExpire); | ||
} | ||
|
||
// private methods | ||
|
@@ -256,7 +238,7 @@ private Mock<ICoreServerSession> CreateMockSession(bool recentlyUsed) | |
return recentlyUsed ? CreateMockRecentlyUsedSession() : CreateMockExpiredSession(); | ||
} | ||
|
||
private CoreServerSessionPool CreateSubject() | ||
private CoreServerSessionPool CreateSubject(ClusterType clusterType = ClusterType.Sharded) | ||
{ | ||
var clusterId = new ClusterId(); | ||
var endPoint = new DnsEndPoint("localhost", 27017); | ||
|
@@ -270,36 +252,13 @@ private CoreServerSessionPool CreateSubject() | |
version: new SemanticVersion(3, 6, 0), | ||
wireVersionRange: new Range<int>(6, 14)); | ||
|
||
var clusterDescription = new ClusterDescription(clusterId, false, null, ClusterType.Sharded, [serverDescription]); | ||
var clusterDescription = new ClusterDescription(clusterId, false, null, clusterType, [serverDescription]); | ||
|
||
var mockCluster = new Mock<IClusterInternal>(); | ||
mockCluster.SetupGet(m => m.Description).Returns(clusterDescription); | ||
|
||
return new CoreServerSessionPool(mockCluster.Object); | ||
} | ||
|
||
private class TestCluster : IClusterInternal | ||
{ | ||
public TestCluster(ClusterType clusterType) | ||
{ | ||
Description = new ClusterDescription(new ClusterId(), false, null, clusterType, Enumerable.Empty<ServerDescription>()); | ||
} | ||
|
||
public ClusterId ClusterId => throw new NotImplementedException(); | ||
|
||
public ClusterDescription Description { get; } | ||
|
||
public ClusterSettings Settings => throw new NotImplementedException(); | ||
|
||
public event EventHandler<ClusterDescriptionChangedEventArgs> DescriptionChanged; | ||
|
||
public ICoreServerSession AcquireServerSession() => throw new NotImplementedException(); | ||
public void Dispose() => throw new NotImplementedException(); | ||
public void Initialize() => DescriptionChanged?.Invoke(this, new ClusterDescriptionChangedEventArgs(Description, Description)); | ||
public IServer SelectServer(IServerSelector selector, CancellationToken cancellationToken) => throw new NotImplementedException(); | ||
public Task<IServer> SelectServerAsync(IServerSelector selector, CancellationToken cancellationToken) => throw new NotImplementedException(); | ||
public ICoreSessionHandle StartSession(CoreSessionOptions options = null) => throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
internal static class CoreServerSessionPoolReflector | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In scope of CSOT I've updated
IClusterInternal
interface, instead of mirroring the changes here, I've decided to remove local implementation by usingMoq
(CreateSubject
method already constructing that mock)