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 1 commit
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,54 @@ public async Task Ensure_server_session_are_allocated_only_on_connection_checkou
await eventsTask.WithTimeout(1000);
}

[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.FromSeconds(2000);
});

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code could be simplified a little by using the following approach:

capturedEvents
    .SkipUntil(e => <first event condition>)
    .Any(e => <second event condition>)

.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