Beispiel #1
0
        public async Task Session_GetState_Should_Return_A_Snapshot_Of_The_Pools_State()
        {
            var poolingOptions = PoolingOptions.Create().SetCoreConnectionsPerHost(HostDistance.Local, 2);

            using (var cluster = Cluster.Builder()
                                 .AddContactPoint(_testCluster.InitialContactPoint)
                                 .WithPoolingOptions(poolingOptions)
                                 .Build())
            {
                var           session = cluster.Connect();
                var           counter = 0;
                ISessionState state   = null;
                // Warmup
                await TestHelper.TimesLimit(() => session.ExecuteAsync(new SimpleStatement(Query)), 64, 32).ConfigureAwait(false);

                const int limit = 100;
                // Perform several queries and get a snapshot somewhere
                await TestHelper.TimesLimit(async() =>
                {
                    var count = Interlocked.Increment(ref counter);
                    if (count == 180)
                    {
                        // after some requests
                        state = session.GetState();
                    }
                    return(await session.ExecuteAsync(new SimpleStatement(Query)).ConfigureAwait(false));
                }, 280, 100).ConfigureAwait(false);

                Assert.NotNull(state);
                var stringState = state.ToString();
                CollectionAssert.AreEquivalent(cluster.AllHosts(), state.GetConnectedHosts());
                foreach (var host in cluster.AllHosts())
                {
                    Assert.AreEqual(2, state.GetOpenConnections(host));
                    StringAssert.Contains($"\"{host.Address}\": {{", stringState);
                }
                var totalInFlight = cluster.AllHosts().Sum(h => state.GetInFlightQueries(h));
                Assert.Greater(totalInFlight, 0);
                Assert.LessOrEqual(totalInFlight, limit);
            }
        }