Example #1
0
        protected virtual async Task DoSingleUserWorkAsync(CancellationToken cancellationToken)
        {
            int iteration = 0;
            var random    = new Random();
            var customers = await customersClient.GetCustomersAsync();

            var customer = customers.ElementAt(random.Next(1, customers.Count()));

            Interlocked.Increment(ref totalRequests);

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var products5 = await productsClient.GetProductsAsync("for testing 5", true, cancellationToken);

                    var products4 = await productsClient.GetProductsAsync("for testing 4", true, cancellationToken);

                    var products6 = await productsClient.GetProductsAsync("for testing 6", true, cancellationToken);

                    var orderItems = products4.Take(1)
                                     .Concat(products5.Take(1))
                                     .Concat(products6.Take(1))
                                     .Select(x => new ApiModel.Order.NewOrderItemInfo()
                    {
                        ProductId = x.Id,
                        Quantity  = 4
                    })
                                     .ToList();

                    bool shouldAddOrder = (iteration++) % 4 == 0;

                    if (shouldAddOrder)
                    {
                        await ordersClient.AddNewOrderAsync(new ApiModel.Order.Request.AddOrderRequest()
                        {
                            CustomerId     = customer.Id,
                            EmployeeId     = 2,
                            Freight        = 1,
                            RequiredDate   = DateTime.Now.AddDays(3),
                            ShipAddress    = "Test address",
                            ShipCity       = "Never",
                            ShipCountry    = "Neverland",
                            ShipName       = "Test ship name",
                            ShipPostalCode = "12345",
                            ShipRegion     = "Test region",
                            OrderItems     = orderItems
                        }, cancellationToken);

                        Interlocked.Increment(ref totalRequests);
                    }

                    var orders = await ordersClient.GetOrdersAsync(customerId : customer.Id,
                                                                   cancellationToken : cancellationToken);

                    Interlocked.Increment(ref totalRequests);
                    Interlocked.Increment(ref totalRequests);
                    Interlocked.Increment(ref totalRequests);
                    Interlocked.Increment(ref totalRequests);
                }
                catch (Exception ex) when(ex is IOException || ex is TaskCanceledException)
                {
                    ;
                }
            }
        }