예제 #1
0
        /// <summary>
        /// Utility function used by async tests
        /// </summary>
        /// <param name="rnd"> Used to randomize reader.Read() call, whether it should continue or break, and is passed down to ConsumeReaderAsync</param>
        /// <param name="result"> The Async result from Begin operation.</param>
        /// <param name="com"> The Sql Command to Execute</param>
        /// <param name="query">Indicates if data is being queried and where ExecuteQuery or Non-query to be used with the reader</param>
        /// <param name="xml">Indicates if the query should be executed as an Xml</param>
        /// <param name="cancelled">Indicates if command was cancelled and is used to throw exception if a Command cancellation related exception is encountered</param>
        /// <param name="cts">The Cancellation Token Source</param>
        private void SqlCommandEndExecute(Random rnd, IAsyncResult result, SqlCommand com, bool query, bool xml, bool cancelled, CancellationTokenSource cts = null)
        {
            try
            {
                bool closeReader = ShouldCloseDataReader();
                if (xml)
                {
                    XmlReader reader = null;
                    if (result != null && result is Task <XmlReader> )
                    {
                        reader = AsyncUtils.GetResult <XmlReader>(result);
                    }
                    else
                    {
                        reader = AsyncUtils.ExecuteXmlReader(com);
                    }

                    while (reader.Read())
                    {
                        if (rnd.Next(10) == 0)
                        {
                            break;
                        }
                        if (rnd.Next(2) == 0)
                        {
                            continue;
                        }
                        reader.ReadElementContentAsString();
                    }
                    if (closeReader)
                    {
                        reader.Dispose();
                    }
                }
                else if (query)
                {
                    DataStressReader reader = null;
                    if (result != null && result is Task <SqlDataReader> )
                    {
                        reader = new DataStressReader(AsyncUtils.GetResult <SqlDataReader>(result));
                    }
                    else
                    {
                        reader = new DataStressReader(AsyncUtils.ExecuteReader(com));
                    }

                    CancellationToken token = (cts != null) ? cts.Token : CancellationToken.None;

                    AsyncUtils.WaitAndUnwrapException(ConsumeReaderAsync(reader, false, token, rnd));

                    if (closeReader)
                    {
                        reader.Close();
                    }
                }
                else
                {
                    if (result != null && result is Task <int> )
                    {
                        int temp = AsyncUtils.GetResult <int>(result);
                    }
                    else
                    {
                        AsyncUtils.ExecuteNonQuery(com);
                    }
                }
            }
            catch (Exception e)
            {
                if (cancelled && IsCommandCancelledException(e))
                {
                    // expected exception, ignore
                }
                else
                {
                    throw;
                }
            }
        }