Пример #1
0
        protected async virtual Task CommandExecuteAsync(Random rnd, DbCommand com, bool query)
        {
            CancellationTokenSource cts = null;

            // Cancel 1/10 commands
            Task cancelTask    = null;
            bool cancelCommand = rnd.NextBool(0.1);

            if (cancelCommand)
            {
                if (rnd.NextBool())
                {
                    // Use DbCommand.Cancel
                    cancelTask = Task.Run(() => CommandCancel(com));
                }
                else
                {
                    // Use CancellationTokenSource
                    if (cts == null)
                    {
                        cts = new CancellationTokenSource();
                    }
                    cancelTask = Task.Run(() => cts.Cancel());
                }
            }

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

            DataStressReader reader = null;

            try
            {
                if (query)
                {
                    CommandBehavior commandBehavior = CommandBehavior.Default;
                    if (rnd.NextBool(0.5))
                    {
                        commandBehavior |= CommandBehavior.SequentialAccess;
                    }
                    if (rnd.NextBool(0.25))
                    {
                        commandBehavior |= CommandBehavior.KeyInfo;
                    }
                    if (rnd.NextBool(0.1))
                    {
                        commandBehavior |= CommandBehavior.SchemaOnly;
                    }

                    // Get the reader
                    reader = new DataStressReader(await com.ExecuteReaderSyncOrAsync(commandBehavior, token, rnd));

                    // Consume the reader's data
                    await ConsumeReaderAsync(reader, commandBehavior.HasFlag(CommandBehavior.SequentialAccess), token, rnd);
                }
                else
                {
                    await com.ExecuteNonQuerySyncOrAsync(token, rnd);
                }
            }
            catch (Exception e)
            {
                if (cancelCommand && IsCommandCancelledException(e))
                {
                    // Catch command canceled exception
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (cancelTask != null)
                {
                    AsyncUtils.WaitAndUnwrapException(cancelTask);
                }
                if (reader != null && ShouldCloseDataReader())
                {
                    reader.Close();
                }
            }
        }
Пример #2
0
 public void TestCommandAndReaderAsync()
 {
     // Since we're calling an "async" method, we need to do a Wait() here.
     AsyncUtils.WaitAndUnwrapException(TestCommandAndReaderAsyncInternal());
 }
Пример #3
0
 /// <summary>
 /// Utility function used by command tests
 /// </summary>
 protected virtual void CommandExecute(Random rnd, DbCommand com, bool query)
 {
     AsyncUtils.WaitAndUnwrapException(CommandExecuteAsync(rnd, com, query));
 }