public void Equals_should_not_be_the_same_when_a_field_is_different() { var subject1 = new ReplicaSetConfig(_endPoints, _name, _endPoints[0], _version); var subject2 = new ReplicaSetConfig(new[] { _endPoints[0] }, _name, _endPoints[0], _version); var subject3 = new ReplicaSetConfig(_endPoints, null, _endPoints[0], _version); var subject4 = new ReplicaSetConfig(_endPoints, _name, _endPoints[1], _version); var subject5 = new ReplicaSetConfig(_endPoints, _name, null, _version); var subject6 = new ReplicaSetConfig(_endPoints, _name, _endPoints[0], _version + 1); var subject7 = new ReplicaSetConfig(_endPoints, _name, _endPoints[0], null); subject1.Should().NotBe(subject2); subject1.Should().NotBe(subject3); subject1.Should().NotBe(subject4); subject1.Should().NotBe(subject5); subject1.Should().NotBe(subject6); subject1.Should().NotBe(subject7); subject2.Should().NotBe(subject1); subject3.Should().NotBe(subject1); subject4.Should().NotBe(subject1); subject5.Should().NotBe(subject1); subject6.Should().NotBe(subject1); subject7.Should().NotBe(subject1); }
private void PublishDescription(ICluster cluster, EndPoint endPoint, ServerType serverType, IEnumerable <EndPoint> hosts = null, string setName = null, EndPoint primary = null, ElectionId electionId = null, EndPoint canonicalEndPoint = null, int?setVersion = null) { var current = _serverFactory.GetServerDescription(endPoint); var config = new ReplicaSetConfig( hosts ?? new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint }, setName ?? "test", primary, setVersion); var serverDescription = current.With( averageRoundTripTime: TimeSpan.FromMilliseconds(10), replicaSetConfig: serverType.IsReplicaSetMember() ? config : null, canonicalEndPoint: canonicalEndPoint, electionId: electionId, state: ServerState.Connected, tags: null, type: serverType, version: new SemanticVersion(2, 6, 3), wireVersionRange: new Range <int>(0, int.MaxValue)); var currentClusterDescription = cluster.Description; _serverFactory.PublishDescription(serverDescription); SpinWait.SpinUntil(() => !object.ReferenceEquals(cluster.Description, currentClusterDescription), 100); // sometimes returns false and that's OK }
public void Constructor_with_multiple_parameters_should_return_properly_initialized_instance() { var averageRoundTripTime = TimeSpan.FromSeconds(1); var replicaSetConfig = new ReplicaSetConfig( new [] { new DnsEndPoint("localhost", 27017), new DnsEndPoint("localhost", 27018) }, "name", new DnsEndPoint("localhost", 27017), 1); var state = ServerState.Connected; var tags = new TagSet(new [] { new Tag("x", "a") }); var type = ServerType.ReplicaSetPrimary; var version = new SemanticVersion(2, 6, 3); var wireVersionRange = new Range<int>(2, 3); var subject = new ServerDescription( __serverId, __endPoint, state, type, averageRoundTripTime, replicaSetConfig, tags, version, wireVersionRange); subject.AverageRoundTripTime.Should().Be(TimeSpan.FromSeconds(1)); subject.EndPoint.Should().Be(__endPoint); subject.ReplicaSetConfig.Should().Be(replicaSetConfig); subject.ServerId.Should().Be(__serverId); subject.State.Should().Be(state); subject.Tags.Should().Be(tags); subject.Type.Should().Be(type); }
public void Equals_should_ignore_revision() { var subject1 = new ReplicaSetConfig(_endPoints, _name, _endPoints[0], _version); var subject2 = new ReplicaSetConfig(_endPoints, _name, _endPoints[0], _version); subject1.Equals(subject2).Should().BeTrue(); subject1.Equals((object)subject2).Should().BeTrue(); subject1.GetHashCode().Should().Be(subject2.GetHashCode()); }
public void Constructor_should_assign_properties_correctly() { var subject = new ReplicaSetConfig(_endPoints, _name, _endPoints[0], _version); subject.Members.Should().BeEquivalentTo <EndPoint>(_endPoints); subject.Name.Should().Be(_name); subject.Primary.Should().Be(_endPoints[0]); subject.Version.Should().Be(_version); }
public void Constructor_should_assign_properties_correctly() { var subject = new ReplicaSetConfig(_endPoints, _name, _endPoints[0], _version); subject.Members.Should().BeEquivalentTo(_endPoints); subject.Name.Should().Be(_name); subject.Primary.Should().Be(_endPoints[0]); subject.Version.Should().Be(_version); }
private void PublishDescription(EndPoint endPoint, ServerType serverType, ReplicaSetConfig replicaSetConfig = null) { var current = _mockServerFactory.GetServerDescription(endPoint); var description = current.With( state: ServerState.Connected, type: serverType, replicaSetConfig: replicaSetConfig); _mockServerFactory.PublishDescription(description); }
private ClusterDescription CreateSubject(string notEqualField = null) { var clusterId = new ClusterId(1); var type = ClusterType.ReplicaSet; var servers = new[] { __serverDescription1, __serverDescription2 }; var replicaSetConfig = new ReplicaSetConfig(new[] { __endPoint1, __endPoint2 }, "name", __endPoint1, 1); switch (notEqualField) { case "ClusterId": clusterId = new ClusterId(2); break; case "Type": type = ClusterType.Unknown; break; case "Servers": servers = new[] { __serverDescription1 }; break; } return(new ClusterDescription(clusterId, type, servers)); }
private void PublishDescription(EndPoint endPoint, ServerType serverType, IEnumerable <EndPoint> hosts = null, string setName = null, EndPoint primary = null) { var current = _serverFactory.GetServerDescription(endPoint); var config = new ReplicaSetConfig( hosts ?? new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint }, setName ?? "test", primary, null); var description = current.WithHeartbeatInfo( TimeSpan.FromMilliseconds(10), serverType.IsReplicaSetMember() ? config : null, null, serverType, new SemanticVersion(2, 6, 3), new Range <int>(0, int.MaxValue)); _serverFactory.PublishDescription(description); }
private void PublishDescription(EndPoint endPoint, ServerType serverType, IEnumerable <EndPoint> hosts = null, string setName = null, EndPoint primary = null) { var current = _serverFactory.GetServerDescription(endPoint); var config = new ReplicaSetConfig( hosts ?? new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint }, setName ?? "test", primary, null); var description = current.With( averageRoundTripTime: TimeSpan.FromMilliseconds(10), replicaSetConfig: serverType.IsReplicaSetMember() ? config : null, state: ServerState.Connected, tags: null, type: serverType, version: new SemanticVersion(2, 6, 3), wireVersionRange: new Range <int>(0, int.MaxValue)); _serverFactory.PublishDescription(description); }
public void ServerDescription_type_should_be_replaced_with_Unknown_when_isMaster_setName_is_different( [Values(null, "wrong")] string isMasterSetName) { _settings = _settings.With( connectionMode: ClusterConnectionMode.Direct, replicaSetName: "rs"); var subject = CreateSubject(); subject.Initialize(); _capturedEvents.Clear(); var replicaSetConfig = new ReplicaSetConfig(new[] { _endPoint }, name: isMasterSetName, _endPoint, 1); PublishDescription(_endPoint, ServerType.Standalone, replicaSetConfig); subject.Description.Type.Should().Be(ClusterType.Unknown); var resultServers = subject.Description.Servers; resultServers.Count.Should().Be(1); resultServers.First().Type.Should().Be(ServerType.Unknown); _capturedEvents.Next().Should().BeOfType <ClusterDescriptionChangedEvent>(); _capturedEvents.Any().Should().BeFalse(); }
public void ServerDescription_type_should_be_replaced_with_Unknown_when_legacy_hello_setName_is_different( [Values(null, "wrong")] string legacyHelloSetName) { _settings = _settings.With( #pragma warning disable CS0618 // Type or member is obsolete connectionMode: ClusterConnectionMode.Direct, connectionModeSwitch: ConnectionModeSwitch.UseConnectionMode, #pragma warning restore CS0618 // Type or member is obsolete replicaSetName: "rs"); var subject = CreateSubject(); subject.Initialize(); _capturedEvents.Clear(); var replicaSetConfig = new ReplicaSetConfig(new[] { _endPoint }, name: legacyHelloSetName, _endPoint, 1); PublishDescription(_endPoint, ServerType.Standalone, replicaSetConfig); subject.Description.Type.Should().Be(ClusterType.Unknown); var resultServers = subject.Description.Servers; resultServers.Count.Should().Be(1); resultServers.First().Type.Should().Be(ServerType.Unknown); _capturedEvents.Next().Should().BeOfType <ClusterDescriptionChangedEvent>(); _capturedEvents.Any().Should().BeFalse(); }
private void PublishDescription(EndPoint endPoint, ServerType serverType, IEnumerable<EndPoint> hosts = null, string setName = null, EndPoint primary = null) { var current = _serverFactory.GetServerDescription(endPoint); var config = new ReplicaSetConfig( hosts ?? new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint }, setName ?? "test", primary, null); var description = current.WithHeartbeatInfo( TimeSpan.FromMilliseconds(10), serverType.IsReplicaSetMember() ? config : null, null, serverType, new SemanticVersion(2, 6, 3), new Range<int>(0, int.MaxValue)); _serverFactory.PublishDescription(description); }
private void PublishDescription(ICluster cluster, EndPoint endPoint, ServerType serverType, IEnumerable<EndPoint> hosts = null, string setName = null, EndPoint primary = null, ElectionId electionId = null, EndPoint canonicalEndPoint = null, int? setVersion = null) { var current = _serverFactory.GetServerDescription(endPoint); var config = new ReplicaSetConfig( hosts ?? new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint }, setName ?? "test", primary, setVersion); var serverDescription = current.With( averageRoundTripTime: TimeSpan.FromMilliseconds(10), replicaSetConfig: serverType.IsReplicaSetMember() ? config : null, canonicalEndPoint: canonicalEndPoint, electionId: electionId, state: ServerState.Connected, tags: null, type: serverType, version: new SemanticVersion(2, 6, 3), wireVersionRange: new Range<int>(0, int.MaxValue)); var currentClusterDescription = cluster.Description; _serverFactory.PublishDescription(serverDescription); SpinWait.SpinUntil(() => !object.ReferenceEquals(cluster.Description, currentClusterDescription), 100); // sometimes returns false and that's OK }
public void Equals_should_return_false_when_any_field_is_not_equal(string notEqualField) { var averageRoundTripTime = TimeSpan.FromSeconds(1); var endPoint = new DnsEndPoint("localhost", 27017); var replicaSetConfig = new ReplicaSetConfig( new[] { new DnsEndPoint("localhost", 27017), new DnsEndPoint("localhost", 27018) }, "name", new DnsEndPoint("localhost", 27017), 1); var serverId = new ServerId(__clusterId, endPoint); var state = ServerState.Connected; var tags = new TagSet(new[] { new Tag("x", "a") }); var type = ServerType.ReplicaSetPrimary; var version = new SemanticVersion(2, 6, 3); var wireVersionRange = new Range<int>(2, 3); var subject = new ServerDescription( serverId, endPoint, state, type, averageRoundTripTime, replicaSetConfig, tags, version, wireVersionRange); switch (notEqualField) { case "AverageRoundTripTime": averageRoundTripTime = averageRoundTripTime.Add(TimeSpan.FromSeconds(1)); break; case "EndPoint": endPoint = new DnsEndPoint(endPoint.Host, endPoint.Port + 1); serverId = new ServerId(__clusterId, endPoint); break; case "ReplicaSetConfig": replicaSetConfig = new ReplicaSetConfig(replicaSetConfig.Members, "newname", replicaSetConfig.Primary, replicaSetConfig.Version); break; case "State": state = ServerState.Disconnected; break; case "ServerId": serverId = new ServerId(new ClusterId(), endPoint); break; case "Tags": tags = new TagSet(new[] { new Tag("x", "b") }); break; case "Type": type = ServerType.ReplicaSetSecondary; break; case "Version": version = new SemanticVersion(version.Major, version.Minor, version.Patch + 1); break; case "WireVersionRange": wireVersionRange = new Range<int>(0, 0); break; } var serverDescription2 = new ServerDescription( serverId, endPoint, state, type, averageRoundTripTime, replicaSetConfig, tags, version, wireVersionRange); subject.Equals(serverDescription2).Should().BeFalse(); subject.Equals((object)serverDescription2).Should().BeFalse(); subject.GetHashCode().Should().NotBe(serverDescription2.GetHashCode()); }
private void PublishDescription(EndPoint endPoint, ServerType serverType, IEnumerable<EndPoint> hosts = null, string setName = null, EndPoint primary = null, ElectionId electionId = null, EndPoint canonicalEndPoint = null) { var current = _serverFactory.GetServerDescription(endPoint); var config = new ReplicaSetConfig( hosts ?? new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint }, setName ?? "test", primary, null); var description = current.With( averageRoundTripTime: TimeSpan.FromMilliseconds(10), replicaSetConfig: serverType.IsReplicaSetMember() ? config : null, canonicalEndPoint: canonicalEndPoint, electionId: electionId, state: ServerState.Connected, tags: null, type: serverType, version: new SemanticVersion(2, 6, 3), wireVersionRange: new Range<int>(0, int.MaxValue)); _serverFactory.PublishDescription(description); }
private ClusterDescription CreateSubject(string notEqualField = null) { var clusterId = new ClusterId(1); var type = ClusterType.ReplicaSet; var servers = new[] { __serverDescription1, __serverDescription2 }; var replicaSetConfig = new ReplicaSetConfig(new[] { __endPoint1, __endPoint2 }, "name", __endPoint1, 1); switch (notEqualField) { case "ClusterId": clusterId = new ClusterId(2); break; case "Type": type = ClusterType.Unknown; break; case "Servers": servers = new[] { __serverDescription1 }; break; } return new ClusterDescription(clusterId, type, servers); }
public void WithHeartbeat_should_return_new_instance_when_a_field_is_not_equal(string notEqualField) { var averageRoundTripTime = TimeSpan.FromSeconds(1); var canonicalEndPoint = new DnsEndPoint("localhost", 27017); var electionId = new ElectionId(ObjectId.GenerateNewId()); var replicaSetConfig = new ReplicaSetConfig( new[] { new DnsEndPoint("localhost", 27017), new DnsEndPoint("localhost", 27018) }, "name", new DnsEndPoint("localhost", 27017), 1); var state = ServerState.Connected; var tags = new TagSet(new[] { new Tag("x", "a") }); var type = ServerType.ReplicaSetPrimary; var version = new SemanticVersion(2, 6, 3); var wireVersionRange = new Range<int>(2, 3); var subject = new ServerDescription( __serverId, __endPoint, state: state, type: type, averageRoundTripTime: averageRoundTripTime, electionId: electionId, replicaSetConfig: replicaSetConfig, tags: tags, version: version, wireVersionRange: wireVersionRange); switch (notEqualField) { case "AverageRoundTripTime": averageRoundTripTime = averageRoundTripTime.Add(TimeSpan.FromSeconds(1)); break; case "CanonicalEndPoint": canonicalEndPoint = new DnsEndPoint("localhost", 27018); break; case "ElectionId": electionId = new ElectionId(ObjectId.Empty); break; case "ReplicaSetConfig": replicaSetConfig = new ReplicaSetConfig(replicaSetConfig.Members, "newname", replicaSetConfig.Primary, replicaSetConfig.Version); break; case "Tags": tags = new TagSet(new[] { new Tag("x", "b") }); break; case "Type": type = ServerType.ReplicaSetSecondary; break; case "Version": version = new SemanticVersion(version.Major, version.Minor, version.Patch + 1); break; case "WireVersionRange": wireVersionRange = new Range<int>(0, 0); break; } var serverDescription2 = subject.With( averageRoundTripTime: averageRoundTripTime, canonicalEndPoint: canonicalEndPoint, replicaSetConfig: replicaSetConfig, state: ServerState.Connected, electionId: electionId, tags: tags, type: type, version: version, wireVersionRange: wireVersionRange); subject.Equals(serverDescription2).Should().BeFalse(); subject.Equals((object)serverDescription2).Should().BeFalse(); subject.GetHashCode().Should().NotBe(serverDescription2.GetHashCode()); }
public void WithHeartbeat_should_return_same_instance_when_all_fields_are_equal() { var averageRoundTripTime = TimeSpan.FromSeconds(1); var replicaSetConfig = new ReplicaSetConfig( new[] { new DnsEndPoint("localhost", 27017), new DnsEndPoint("localhost", 27018) }, "name", new DnsEndPoint("localhost", 27017), 1); var state = ServerState.Connected; var tags = new TagSet(new[] { new Tag("x", "a") }); var type = ServerType.ReplicaSetPrimary; var version = new SemanticVersion(2, 6, 3); var wireVersionRange = new Range<int>(0, 2); var subject = new ServerDescription( __serverId, __endPoint, state: state, type: type, averageRoundTripTime: averageRoundTripTime, replicaSetConfig: replicaSetConfig, tags: tags, version: version, wireVersionRange: wireVersionRange); var serverDescription2 = subject.With( averageRoundTripTime: averageRoundTripTime, replicaSetConfig: replicaSetConfig, state: ServerState.Connected, tags: tags, type: type, version: version, wireVersionRange: wireVersionRange); serverDescription2.Should().BeSameAs(subject); }