Exemple #1
0
        public void TestQueryShardsInvalidConnectionSync()
        {
            var badShard = new ShardLocation("badLocation", "badDatabase");
            var bldr     = new SqlConnectionStringBuilder();

            bldr.DataSource     = badShard.DataSource;
            bldr.InitialCatalog = badShard.Database;
            var badConn = new SqlConnection(bldr.ConnectionString);

            try
            {
                using (var conn = new MultiShardConnection(_shardMap.GetShards(), MultiShardTestUtils.ShardConnectionString))
                {
                    conn.GetShardConnections().Add(new Tuple <ShardLocation, DbConnection>(badShard,
                                                                                           badConn));
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "select 1";
                        cmd.ExecuteReader();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is MultiShardAggregateException)
                {
                    var maex = (MultiShardAggregateException)ex;
                    Logger.Log("Exception encountered: " + maex.ToString());
                    throw ((MultiShardException)(maex.InnerException)).InnerException;
                }
                throw;
            }
        }
Exemple #2
0
        public void TestQueryShardsCommandCancellationHandler()
        {
            List <ShardLocation>    cancelledShards = new List <ShardLocation>();
            CancellationTokenSource cts             = new CancellationTokenSource();

            using (MultiShardCommand cmd = _shardConnection.CreateCommand())
            {
                Barrier barrier = new Barrier(cmd.Connection.Shards.Count() + 1);

                // If the threads don't meet the barrier by this time, then give up and fail the test
                TimeSpan barrierTimeout = TimeSpan.FromSeconds(10);

                cmd.CommandText            = "WAITFOR DELAY '00:01:00'";
                cmd.CommandTimeoutPerShard = 12;

                cmd.ShardExecutionCanceled += (obj, args) =>
                {
                    cancelledShards.Add(args.ShardLocation);
                };

                cmd.ShardExecutionBegan += (obj, args) =>
                {
                    // If ShardExecutionBegan were only signaled by one thread,
                    // then this would hang forever.
                    barrier.SignalAndWait(barrierTimeout);
                };

                Task cmdTask = cmd.ExecuteReaderAsync(cts.Token);

                bool syncronized = barrier.SignalAndWait(barrierTimeout);
                Assert.IsTrue(syncronized);

                // Cancel the command once execution begins
                // Sleeps are bad but this is just to really make sure
                // sqlclient has had a chance to begin command execution
                // Will not effect the test outcome
                Thread.Sleep(TimeSpan.FromSeconds(1));
                cts.Cancel();

                // Validate that the task was cancelled
                AssertExtensions.WaitAndAssertThrows <TaskCanceledException>(cmdTask);

                // Validate that the cancellation event was fired for all shards
                List <ShardLocation> allShards = _shardConnection.GetShardConnections().Select(l => l.Item1).ToList();
                CollectionAssert.AreEquivalent(allShards, cancelledShards, "Expected command canceled event to be fired for all shards!");
            }
        }
Exemple #3
0
        public void TestInvalidMultiShardConnectionString()
        {
            MultiShardConnection conn;

            try
            {
                conn = new MultiShardConnection(_shardMap.GetShards(), connectionString: null);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ArgumentNullException, "Expected ArgumentNullException!");
            }

            try
            {
                conn = new MultiShardConnection(_shardMap.GetShards(), MultiShardTestUtils.ShardMapManagerConnectionString);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ArgumentException, "Expected ArgumentException!");
            }

            // Validate that the ApplicationName is updated properly
            var applicationStringBldr = new StringBuilder();

            for (int i = 0; i < ApplicationNameHelper.MaxApplicationNameLength; i++)
            {
                applicationStringBldr.Append('x');
            }
            string applicationName = applicationStringBldr.ToString();
            SqlConnectionStringBuilder connStringBldr = new SqlConnectionStringBuilder(MultiShardTestUtils.ShardConnectionString);

            connStringBldr.ApplicationName = applicationName;
            conn = new MultiShardConnection(_shardMap.GetShards(), connStringBldr.ConnectionString);

            string updatedApplicationName = new SqlConnectionStringBuilder
                                                (conn.GetShardConnections()[0].Item2.ConnectionString).ApplicationName;

            Assert.IsTrue(updatedApplicationName.Length == ApplicationNameHelper.MaxApplicationNameLength &&
                          updatedApplicationName.EndsWith(MultiShardConnection.ApplicationNameSuffix), "ApplicationName not appended with {0}!",
                          MultiShardConnection.ApplicationNameSuffix);
        }