Exemple #1
0
        /// <summary>
        /// System.CommandLine.CommandHandler implementation
        /// </summary>
        /// <param name="config">configuration</param>
        /// <returns>non-zero on failure</returns>
        public static async Task <int> Run(Config config)
        {
            if (config == null)
            {
                Console.WriteLine("CommandOptions is null");
                return(-1);
            }

            // set any missing values
            config.SetDefaultValues();

            // don't run the test on a dry run
            if (config.DryRun)
            {
                return(DoDryRun(config));
            }

            // create the test
            try
            {
                ValidationTest lrt = new ValidationTest(config);

                if (config.DelayStart > 0)
                {
                    Console.WriteLine($"Waiting {config.DelayStart} seconds to start test ...\n");

                    // wait to start the test run
                    await Task.Delay(config.DelayStart * 1000, TokenSource.Token).ConfigureAwait(false);
                }

                if (config.RunLoop)
                {
                    // build and run the web host
                    IHost host = BuildWebHost();
                    _ = host.StartAsync(TokenSource.Token);

                    // run in a loop
                    int res = lrt.RunLoop(config, TokenSource.Token);

                    // stop and dispose the web host
                    await host.StopAsync(TimeSpan.FromMilliseconds(100)).ConfigureAwait(false);

                    host.Dispose();
                    host = null;

                    return(res);
                }
                else
                {
                    // run one iteration
                    return(await lrt.RunOnce(config, TokenSource.Token).ConfigureAwait(false));
                }
            }
            catch (TaskCanceledException tce)
            {
                // log exception
                if (!tce.Task.IsCompleted)
                {
                    Console.WriteLine($"Exception: {tce}");
                    return(1);
                }

                // task is completed
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\nException:{ex.Message}");
                return(1);
            }
        }
Exemple #2
0
        /// <summary>
        /// System.CommandLine.CommandHandler implementation
        /// </summary>
        /// <param name="config">configuration</param>
        /// <returns>non-zero on failure</returns>
        public static async Task <int> Run(Config config)
        {
            if (config == null)
            {
                Console.WriteLine("CommandOptions is null");
                return(-1);
            }

            // set any missing values
            config.SetDefaultValues();

            // don't run the test on a dry run
            if (config.DryRun)
            {
                return(DoDryRun(config));
            }

            // set json options based on --strict-json
            App.JsonSerializerOptions = new JsonSerializerOptions
            {
                PropertyNamingPolicy        = JsonNamingPolicy.CamelCase,
                PropertyNameCaseInsensitive = !config.StrictJson,
                AllowTrailingCommas         = !config.StrictJson,
                ReadCommentHandling         = config.StrictJson ? JsonCommentHandling.Disallow : JsonCommentHandling.Skip,
            };

            // create the test
            try
            {
                ValidationTest lrt = new ValidationTest(config);

                if (config.DelayStart > 0)
                {
                    Console.WriteLine($"Waiting {config.DelayStart} seconds to start test ...\n");

                    // wait to start the test run
                    await Task.Delay(config.DelayStart * 1000, TokenSource.Token).ConfigureAwait(false);
                }

                if (config.RunLoop)
                {
                    // run in a loop
                    return(lrt.RunLoop(config, TokenSource.Token));
                }
                else
                {
                    // run one iteration
                    return(await lrt.RunOnce(config, TokenSource.Token).ConfigureAwait(false));
                }
            }
            catch (TaskCanceledException tce)
            {
                // log exception
                if (!tce.Task.IsCompleted)
                {
                    Console.WriteLine($"Exception: {tce}");
                    return(1);
                }

                // task is completed
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\nException:{ex.Message}");
                return(1);
            }
        }