Example #1
0
        public void TestSqlXmlCommandReader()
        {
            Random rnd = RandomInstance;

            using (DataStressConnection conn = Factory.CreateConnection(rnd))
            {
                if (!OpenConnection(conn))
                {
                    return;
                }
                DataStressFactory.TableMetadata table = Factory.GetRandomTable(rnd);
                SqlCommand com = (SqlCommand)Factory.GetCommand(rnd, table, conn, query: true, isXml: true);
                com.CommandText = com.CommandText + " FOR XML AUTO";

                // Cancel 1/10 commands
                bool cancel = rnd.Next(10) == 0;
                if (cancel)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(CommandCancel), com);
                }

                try
                {
                    XmlReader reader = com.ExecuteXmlReader();

                    while (reader.Read())
                    {
                        if (rnd.Next(10) == 0)
                        {
                            break;
                        }
                        if (rnd.Next(2) == 0)
                        {
                            continue;
                        }
                        reader.ReadElementContentAsString();
                    }
                    if (rnd.Next(10) != 0)
                    {
                        reader.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    if (cancel && IsCommandCancelledException(ex))
                    {
                        // expected, ignore
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Example #2
0
        public void TestSqlAsyncMARS()
        {
            const int MaxCmds = 11;
            Random    rnd     = RandomInstance;

            using (DataStressConnection conn = Factory.CreateConnection(rnd, DataStressFactory.ConnectionStringOptions.EnableMars))
            {
                if (!OpenConnection(conn))
                {
                    return;
                }
                DataStressFactory.TableMetadata table = Factory.GetRandomTable(rnd);

                // MARS session cache is by default 10.
                // This is documented here: http://msdn.microsoft.com/en-us/library/h32h3abf.aspx
                // We want to stress test this by allowing 11 concurrent commands. Hence the max in rnd.Next below is 12.
                MARSCommand[] cmds = new MARSCommand[rnd.Next(5, MaxCmds + 1)];

                for (int i = 0; i < cmds.Length; i++)
                {
                    cmds[i] = new MARSCommand();

                    // Make every 3rd query xml reader
                    if (i % 3 == 0)
                    {
                        cmds[i].query = true;
                        cmds[i].xml   = true;
                    }
                    else
                    {
                        cmds[i].query = rnd.NextBool();
                        cmds[i].xml   = false;
                    }

                    cmds[i].cmd    = (SqlCommand)Factory.GetCommand(rnd, table, conn, cmds[i].query, cmds[i].xml);
                    cmds[i].result = SqlCommandBeginExecute(cmds[i].cmd, cmds[i].query, cmds[i].xml, rnd.NextBool());
                    if (cmds[i].result != null)
                    {
                        WaitForAsyncOpToComplete(rnd, cmds[i].result, true, false);
                    }
                }

                // After all commands have been launched, wait for them to complete now.
                for (int i = 0; i < cmds.Length; i++)
                {
                    SqlCommandEndExecute(rnd, cmds[i].result, cmds[i].cmd, cmds[i].query, cmds[i].xml, false);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Utility function used for testing cancellation on Execute*Async APIs.
        /// </summary>
        private void TestSqlAsyncCancellation(Random rnd, bool read, bool xml)
        {
            using (DataStressConnection conn = Factory.CreateConnection(rnd))
            {
                if (!OpenConnection(conn))
                {
                    return;
                }
                DataStressFactory.TableMetadata table = Factory.GetRandomTable(rnd);
                SqlCommand com = (SqlCommand)Factory.GetCommand(rnd, table, conn, read, xml);

                CancellationTokenSource cts = new CancellationTokenSource();
                Task t = (Task)SqlCommandBeginExecute(com, read, xml, false, cts);

                cts.CancelAfter(rnd.Next(2000));
                SqlCommandEndExecute(rnd, (IAsyncResult)t, com, read, xml, true, cts);
            }
        }
Example #4
0
        /// <summary>
        /// Utility function for tests
        /// </summary>
        /// <param name="rnd"></param>
        /// <param name="read"></param>
        /// <param name="poll"></param>
        /// <param name="handle"></param>
        /// <param name="xml"></param>
        private void TestSqlAsync(Random rnd, bool read, bool poll, bool handle, bool xml)
        {
            using (DataStressConnection conn = Factory.CreateConnection(rnd))
            {
                if (!OpenConnection(conn))
                {
                    return;
                }
                DataStressFactory.TableMetadata table = Factory.GetRandomTable(rnd);
                SqlCommand com         = (SqlCommand)Factory.GetCommand(rnd, table, conn, read, xml);
                bool       useBeginAPI = rnd.NextBool();

                IAsyncResult result = SqlCommandBeginExecute(com, read, xml, useBeginAPI);
                // Cancel 1/10 commands
                bool cancel = (rnd.Next(10) == 0);
                if (cancel)
                {
                    if (com.Connection.State != ConnectionState.Closed)
                    {
                        com.Cancel();
                    }
                }

                if (result != null)
                {
                    WaitForAsyncOpToComplete(rnd, result, poll, handle);
                }
                // At random end query or forget it
                if (rnd.Next(2) == 0)
                {
                    SqlCommandEndExecute(rnd, result, com, read, xml, cancel);
                }

                // Randomly wait for the command to complete after closing the connection to verify devdiv bug 200550.
                // This was fixed for .NET 4.5 Task-based API, but not for the older Begin/End IAsyncResult API.
                conn.Close();
                if (!useBeginAPI && rnd.NextBool())
                {
                    result.AsyncWaitHandle.WaitOne();
                }
            }
        }