-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,28 +199,16 @@ 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 expectedResult) | ||
|
||
{ | ||
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); | ||
|
@@ -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 | ||
|
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)