コード例 #1
0
        public void GetBytesUsingThreadStaticBuffer_should_return_expected_result_when_multiple_threads_are_used([RandomSeed] int seed)
        {
            const int threadsCount    = 10;
            const int iterationsCount = 10;
            const int maxSize         = 1024;

            var random   = new Random(seed);
            var encoding = Utf8Encodings.Strict;

            ThreadingUtilities.ExecuteOnNewThreads(threadsCount, _ =>
            {
                for (int j = 0; j < iterationsCount; j++)
                {
                    var sizeCurrent = random.Next(8, maxSize);
                    var str         = GetString(sizeCurrent);

                    using var rentedSegment = encoding.GetBytesUsingThreadStaticBuffer(str);
                    var segment             = rentedSegment.Segment;
                    var encodedExpected     = encoding.GetBytes(str);
                    var encodedActual       = segment.ToArray();

                    encodedActual.ShouldAllBeEquivalentTo(encodedExpected);
                }
            });
        }
コード例 #2
0
        public void SelectServer_loadbalancing_prose_test([Values(false, true)] bool async)
        {
            RequireServer.Check()
            .Supports(Feature.ShardedTransactions, Feature.FailPointsBlockConnection)
            .ClusterType(ClusterType.Sharded);
            RequireMultipleShardRouters();

            // temporary disable the test on Win Auth topologies, due to operations timings irregularities
            if (CoreTestConfiguration.ConnectionString.Tls == true &&
                RequirePlatform.GetCurrentOperatingSystem() == SupportedOperatingSystem.Windows)
            {
                throw new SkipException("Win Auth topologies temporary not supported due to timings irregularities.");
            }

            const string applicationName              = "loadBalancingTest";
            const int    threadsCount                 = 10;
            const int    commandsPerThreadCount       = 10;
            const double maxCommandsOnSlowServerRatio = 0.3; // temporary set slow server load to 30% from 25% until find timings are investigated
            const double operationsCountTolerance     = 0.10;

            var failCommand = BsonDocument.Parse($"{{ configureFailPoint: 'failCommand', mode : {{ times : 10000 }}, data : {{ failCommands : [\"find\"], blockConnection: true, blockTimeMS: 500, appName: '{applicationName}' }} }}");

            DropCollection();
            var eventCapturer = CreateEventCapturer();

            using (var client = CreateDisposableClient(eventCapturer, applicationName))
            {
                var slowServer = client.Cluster.SelectServer(WritableServerSelector.Instance, default);
                var fastServer = client.Cluster.SelectServer(new DelegateServerSelector((_, servers) => servers.Where(s => s.ServerId != slowServer.ServerId)), default);

                using var failPoint = FailPoint.Configure(slowServer, NoCoreSession.NewHandle(), failCommand);

                var database = client.GetDatabase(_databaseName);
                CreateCollection();
                var collection = database.GetCollection <BsonDocument>(_collectionName);

                // warm up connections
                var channels = new ConcurrentBag <IChannelHandle>();
                ThreadingUtilities.ExecuteOnNewThreads(threadsCount, i =>
                {
                    channels.Add(slowServer.GetChannel(default));
                    channels.Add(fastServer.GetChannel(default));
コード例 #3
0
        public async Task PoolClearedError_write_retryablity_test([Values(false, true)] bool async)
        {
            RequireServer.Check()
            .Supports(Feature.FailPointsBlockConnection)
            .ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded);

            var heartbeatInterval = TimeSpan.FromMilliseconds(50);
            var eventsWaitTimeout = TimeSpan.FromMilliseconds(5000);

            var failPointCommand = BsonDocument.Parse(
                $@"{{
                    configureFailPoint : 'failCommand',
                    mode : {{ 'times' : 1 }},
                    data :
                    {{
                        failCommands : [ 'insert' ],
                        errorCode : 91,
                        blockConnection: true,
                        blockTimeMS: 1000,
                        errorLabels: [""RetryableWriteError""]
                    }}
                }}");

            IServerSelector failPointSelector = WritableServerSelector.Instance;
            var             settings          = DriverTestConfiguration.GetClientSettings();

            if (CoreTestConfiguration.Cluster.Description.Type == ClusterType.Sharded)
            {
                var serverAddress = settings.Servers.First();
                settings.Servers = new[] { serverAddress };

                // set settings.DirectConnection = true after removing obsolete ConnectionMode
#pragma warning disable CS0618 // Type or member is obsolete
                settings.ConnectionMode = ConnectionMode.Direct;
#pragma warning restore CS0618 // Type or member is obsolete

                failPointSelector = new EndPointServerSelector(new DnsEndPoint(serverAddress.Host, serverAddress.Port));
            }

            settings.MaxConnectionPoolSize = 1;
            settings.RetryWrites           = true;

            var eventCapturer = new EventCapturer()
                                .Capture <ConnectionPoolClearedEvent>()
                                .Capture <ConnectionPoolCheckedOutConnectionEvent>()
                                .Capture <ConnectionPoolCheckingOutConnectionFailedEvent>()
                                .CaptureCommandEvents("insert");

            var failpointServer = DriverTestConfiguration.Client.Cluster.SelectServer(failPointSelector, default);
            using var failPoint = FailPoint.Configure(failpointServer, NoCoreSession.NewHandle(), failPointCommand);

            using var client = CreateClient(settings, eventCapturer, heartbeatInterval);
            var database   = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
            var collection = database.GetCollection <BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName);

            eventCapturer.Clear();

            if (async)
            {
                await ThreadingUtilities.ExecuteTasksOnNewThreads(2, async _ =>
                {
                    await collection.InsertOneAsync(new BsonDocument("x", 1));
                });
            }
            else
            {
                ThreadingUtilities.ExecuteOnNewThreads(2, _ =>
                {
                    collection.InsertOne(new BsonDocument("x", 1));
                });
            }

            // wait for 2 CommandSucceededEvent events, meaning that all other events should be received
            eventCapturer.WaitForOrThrowIfTimeout(
                events => events.OfType <CommandSucceededEvent>().Count() == 2,
                eventsWaitTimeout);

            eventCapturer.Events.OfType <CommandStartedEvent>().Count().Should().Be(3);
            eventCapturer.Events.OfType <CommandFailedEvent>().Count().Should().Be(1);
            eventCapturer.Events.OfType <CommandSucceededEvent>().Count().Should().Be(2);
            eventCapturer.Events.OfType <ConnectionPoolClearedEvent>().Count().Should().Be(1);
            eventCapturer.Events.OfType <ConnectionPoolCheckedOutConnectionEvent>().Count().Should().Be(3);
            eventCapturer.Events.OfType <ConnectionPoolCheckingOutConnectionFailedEvent>().Count().Should().Be(1);
        }
コード例 #4
0
        public void AquireConnection_non_sufficient_reused_connections_should_timeout(
            [Values(true, false)]
            bool async)
        {
            int       maxConnecting       = MongoInternalDefaults.ConnectionPool.MaxConnecting;
            const int initalAcquiredCount = 2;
            const int maxAcquiringCount   = 4;
            const int queueTimeoutMS      = 50;

            var settings = _settings.With(
                waitQueueSize: maxAcquiringCount + initalAcquiredCount + maxConnecting,
                maxConnections: maxAcquiringCount + initalAcquiredCount + maxConnecting,
                waitQueueTimeout: TimeSpan.FromMilliseconds(queueTimeoutMS),
                minConnections: 0);

            var allAcquiringCountEvent  = new CountdownEvent(maxAcquiringCount + initalAcquiredCount);
            var blockEstablishmentEvent = new ManualResetEventSlim(true);
            var establishingCount       = new CountdownEvent(maxConnecting + initalAcquiredCount);

            var mockConnectionFactory = new Mock <IConnectionFactory>();

            mockConnectionFactory
            .Setup(c => c.CreateConnection(It.IsAny <ServerId>(), It.IsAny <EndPoint>()))
            .Returns(() =>
            {
                var connectionMock = new Mock <IConnection>();

                connectionMock
                .Setup(c => c.ConnectionId)
                .Returns(new ConnectionId(_serverId));
                connectionMock
                .Setup(c => c.Settings)
                .Returns(new ConnectionSettings());
                connectionMock
                .Setup(c => c.Open(It.IsAny <CancellationToken>()))
                .Callback(() =>
                {
                    if (establishingCount.CurrentCount > 0)
                    {
                        establishingCount.Signal();
                    }

                    blockEstablishmentEvent.Wait();
                });
                connectionMock
                .Setup(c => c.OpenAsync(It.IsAny <CancellationToken>()))
                .Returns(() =>
                {
                    if (establishingCount.CurrentCount > 0)
                    {
                        establishingCount.Signal();
                    }

                    blockEstablishmentEvent.Wait();
                    return(Task.FromResult(0));
                });

                return(connectionMock.Object);
            });

            using var subject = CreateSubject(settings, mockConnectionFactory.Object);
            subject.Initialize();

            subject.PendingCount.Should().Be(0);
            var connectionsAcquired = Enumerable.Range(0, initalAcquiredCount)
                                      .Select(i => AcquireConnection(subject, async))
                                      .ToArray();

            // block further establishments
            blockEstablishmentEvent.Reset();

            var allConnections   = new List <IConnection>();
            var actualTimeouts   = 0;
            var expectedTimeouts = maxAcquiringCount - maxConnecting;

            ThreadingUtilities.ExecuteOnNewThreads(maxAcquiringCount + maxConnecting + 1, threadIndex =>
            {
                if (threadIndex < maxConnecting)
                {
                    // maximize maxConnecting
                    allAcquiringCountEvent.Signal();
                    AcquireConnection(subject, async);
                }
                else if (threadIndex < maxConnecting + maxAcquiringCount)
                {
                    // wait until all maxConnecting maximized
                    establishingCount.Wait();
                    subject.PendingCount.Should().Be(maxConnecting);

                    allAcquiringCountEvent.Signal();

                    try
                    {
                        AcquireConnection(subject, async);
                    }
                    catch (TimeoutException)
                    {
                        Interlocked.Increment(ref actualTimeouts);
                    }

                    // speedup the test
                    if (expectedTimeouts == actualTimeouts)
                    {
                        blockEstablishmentEvent.Set();
                    }
                }
                else
                {
                    // wait until all trying to acquire
                    allAcquiringCountEvent.Wait();

                    // return connections
                    foreach (var connection in connectionsAcquired)
                    {
                        connection.Dispose();
                    }
                }
            });

            expectedTimeouts.Should().Be(expectedTimeouts);
            subject.PendingCount.Should().Be(0);
        }