Skip to content

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 2 commits into from
May 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Copy link
Member Author

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 using Moq (CreateSubject method already constructing that mock)

var mockedServerSessionPool = new CoreServerSessionPool(mockedCluster);
var mockSession = new Mock<ICoreServerSession>();
var lastUsedAt = DateTime.UtcNow.AddSeconds(1741);
Copy link
Member Author

@sanych-sun sanych-sun May 26, 2025

Choose a reason for hiding this comment

The 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
Expand All @@ -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);
Expand All @@ -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
Expand Down