Skip to content

CSHARP-5204: Test Heartbeats don't gossip cluster time #1665

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -21,6 +21,7 @@
using MongoDB.Bson;
using MongoDB.Bson.TestHelpers;
using MongoDB.Driver.Core;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Events;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.TestHelpers.Logging;
Expand Down Expand Up @@ -302,6 +303,59 @@ public async Task Ensure_server_session_are_allocated_only_on_connection_checkou
await eventsTask.WithTimeout(1000);
}

// https://specifications.readthedocs.io/en/latest/sessions/tests/#20-drivers-do-not-gossip-clustertime-on-sdam-commands
[Fact]
public void Ensure_cluster_times_are_not_gossiped_on_SDAM_commands()
{
RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded);

var eventCapturer = new EventCapturer()
.Capture<ServerHeartbeatStartedEvent>()
.Capture<ServerHeartbeatSucceededEvent>()
.Capture<CommandStartedEvent>();

using var c1 = DriverTestConfiguration.CreateMongoClient(
settings =>
{
settings.ClusterConfigurator = c => c.Subscribe(eventCapturer);
settings.DirectConnection = true;
settings.HeartbeatInterval = TimeSpan.FromMilliseconds(10);
if (settings.Servers.Count() > 1)
{
settings.Servers = settings.Servers.Take(1);
}
});

var pingCommand = new BsonDocument("ping", 1);
var pingResult = c1.GetDatabase("admin").RunCommand<BsonDocument>(pingCommand);

var clusterTime = pingResult["$clusterTime"];

var c2 = DriverTestConfiguration.Client;
c2.GetDatabase("test").GetCollection<BsonDocument>("test").InsertOne(new BsonDocument("advance", "$clusterTime"));

eventCapturer.Clear();

eventCapturer.WaitForOrThrowIfTimeout(
events =>
{
var capturedEvents = events.ToArray();
return capturedEvents.Count(e => e is ServerHeartbeatSucceededEvent) > 1 &&
capturedEvents
.Where((e, i) =>
e is ServerHeartbeatStartedEvent &&
capturedEvents[i + 1] is ServerHeartbeatSucceededEvent)
.Any();
}, TimeSpan.FromSeconds(1), "Didn't get any server heartbeat pairs");

c1.GetDatabase("admin").RunCommand<BsonDocument>(pingCommand);

var commandStartedEvents = eventCapturer.Events.OfType<CommandStartedEvent>().ToArray();
commandStartedEvents.Length.Should().Be(1);
commandStartedEvents[0].CommandName.Should().Be("ping");
commandStartedEvents[0].Command["$clusterTime"].Should().Be(clusterTime);
}

private sealed class MongocryptdContext : IDisposable
{
public IMongoClient MongoClient { get; }
Expand Down