Example #1
0
        public void RunTestDefinition(JsonDrivenTestCase testCase)
        {
            var testDefinition     = testCase.Test;
            var testData           = BsonSerializer.Deserialize <TestData>(testDefinition);
            var clusterDescription = ServerSelectionTestHelper.BuildClusterDescription(testData.topology_description);

            using var cluster = CreateAndSetupCluster(clusterDescription, testData.mocked_topology_state);
            var readPreferenceSelector = new ReadPreferenceServerSelector(ReadPreference.Nearest);

            var selectionHistogram = testData.outcome.expected_frequencies.Keys
                                     .ToDictionary(s => clusterDescription.Servers.Single(d => d.EndPoint.ToString().EndsWith(s)).ServerId, s => 0);
            var selectionFrequenciesExpected = testData.outcome.expected_frequencies.
                                               ToDictionary(s => clusterDescription.Servers.Single(d => d.EndPoint.ToString().EndsWith(s.Key)).ServerId, s => s.Value);

            for (int i = 0; i < testData.iterations; i++)
            {
                var selectedServer = testData.async
                    ? cluster.SelectServerAsync(readPreferenceSelector, default).GetAwaiter().GetResult()
                    : cluster.SelectServer(readPreferenceSelector, default);

                selectionHistogram[selectedServer.ServerId]++;
            }

            foreach (var pair in selectionHistogram)
            {
                var expectedFrequency = selectionFrequenciesExpected[pair.Key];
                var actualFrequency   = pair.Value / (double)testData.iterations;

                actualFrequency.Should().BeInRange(expectedFrequency - testData.outcome.tolerance, expectedFrequency + testData.outcome.tolerance);
            }
        }
        private void RunNonErrorTest(BsonDocument definition, ClusterDescription clusterDescription, IServerSelector selector, TimeSpan heartbeatInterval)
        {
            var suitableServers = ServerSelectionTestHelper.BuildServerDescriptions((BsonArray)definition["suitable_servers"], _clusterId, heartbeatInterval);
            var selectedServers = selector.SelectServers(clusterDescription, clusterDescription.Servers).ToList();

            AssertServers(suitableServers, selectedServers);

            selector = new CompositeServerSelector(new[] { selector, new LatencyLimitingServerSelector(TimeSpan.FromMilliseconds(15)) });
            var inLatencyWindowServers = ServerSelectionTestHelper.BuildServerDescriptions((BsonArray)definition["in_latency_window"], _clusterId, heartbeatInterval);

            selectedServers = selector.SelectServers(clusterDescription, clusterDescription.Servers).ToList();
            AssertServers(inLatencyWindowServers, selectedServers);
        }
        public void RunTestDefinition(JsonDrivenTestCase testCase)
        {
            var definition = testCase.Test;

            JsonDrivenHelper.EnsureAllFieldsAreValid(definition, "_path", "in_latency_window", "operation", "read_preference", "suitable_servers", "topology_description", "heartbeatFrequencyMS", "error");

            var error              = definition.GetValue("error", false).ToBoolean();
            var heartbeatInterval  = TimeSpan.FromMilliseconds(definition.GetValue("heartbeatFrequencyMS", 10000).ToInt64());
            var clusterDescription = ServerSelectionTestHelper.BuildClusterDescription((BsonDocument)definition["topology_description"], heartbeatInterval);

            IServerSelector selector;

            if (definition.GetValue("operation", "read").AsString == "write")
            {
                selector = WritableServerSelector.Instance;
            }
            else
            {
                ReadPreference readPreference;
                try
                {
                    readPreference = BuildReadPreference(definition["read_preference"].AsBsonDocument);
                }
                catch
                {
                    if (error)
                    {
                        return;
                    }
                    throw;
                }

                selector = new ReadPreferenceServerSelector(readPreference);
            }

            if (error)
            {
                RunErrorTest(clusterDescription, selector);
            }
            else
            {
                RunNonErrorTest(definition, clusterDescription, selector, heartbeatInterval);
            }
        }