public async Task TestNonDefaultRetryCountAsync()
        {
            // Set the timeout low enough that the API call is guaranteed to fail
            SendwithusClient.SetTimeoutInMilliseconds(FAILURE_TIMEOUT_MILLISECONDS);

            // Use a non-default number of retries
            SendwithusClient.RetryCount = NON_DEFAULT_RETRY_COUNT;

            // Send a GET request
            try
            {
                var response = await Template.GetTemplateAsync(DEFAULT_TEMPLATE_ID);
            }
            catch (Exception ex)
            {
                SendwithusClientTest.ValidateAggregateException <TaskCanceledException>(NON_DEFAULT_RETRY_COUNT, ex);
            }
            finally
            {
                // Set the timeout back to its default value
                SendwithusClient.SetTimeoutInMilliseconds(SendwithusClient.DEFAULT_TIMEOUT_MILLISECONDS);

                // Set the retry count back to its default value
                SendwithusClient.RetryCount = SendwithusClient.DEFAULT_RETRY_COUNT;
            }
        }
Exemple #2
0
        /// <summary>
        /// Validates whether or not the API call took the expected amount of time to complete.
        /// Assumes the API call failed on a timeout and used the full retry count.
        /// </summary>
        /// <param name="measuredExecutionTimeMilliseconds">The measured execution time of the API call</param>
        public static void ValidateApiCallExecutionTime(double measuredExecutionTimeMilliseconds)
        {
            const int DURATION_WINDOW_SIZE_MILLISECONDS = 1000; // 1000ms A large window to handle variability in run time

            // Print the relevant parameters
            var retryCount                = SendwithusClient.RetryCount;
            var timeoutMilliseconds       = SendwithusClient.GetTimeout().TotalMilliseconds;
            var retryIntervalMilliseconds = SendwithusClient.RetryIntervalMilliseconds;

            Trace.WriteLine(String.Format("Retry Count: {0}", retryCount));
            Trace.WriteLine(String.Format("Timeout: {0}ms", timeoutMilliseconds));
            Trace.WriteLine(String.Format("Retry Interval: {0}ms", retryIntervalMilliseconds));

            // Calculate the expected execution time
            var lowerExecutionTimeLimit = retryCount * timeoutMilliseconds +
                                          (retryCount - 1) * retryIntervalMilliseconds;
            var upperExecutionTimeLimit = lowerExecutionTimeLimit + DURATION_WINDOW_SIZE_MILLISECONDS;

            Trace.WriteLine(String.Format("Measured Execution Time: {0}ms", measuredExecutionTimeMilliseconds.ToString()));
            Trace.WriteLine(String.Format("Minimum Expected Execution Time: {0}ms", lowerExecutionTimeLimit.ToString()));
            Trace.WriteLine(String.Format("Maximum Expected Execution Time: {0}ms", upperExecutionTimeLimit.ToString()));

            // Check if the measured execution time matches the expected execution time
            Assert.IsTrue(measuredExecutionTimeMilliseconds >= lowerExecutionTimeLimit && measuredExecutionTimeMilliseconds <= upperExecutionTimeLimit);
        }
 public EmailService()
 {
     _emailSetting           = EmailHelper.ToEmailSetting();
     SendwithusClient.ApiKey = _emailSetting.EmailConfig.ApiKey;
     SendwithusClient.SetTimeoutInMilliseconds(_emailSetting.EmailConfig.TimeoutInMilliseconds);
     SendwithusClient.RetryCount = _emailSetting.EmailConfig.RetryCount;
     SendwithusClient.RetryIntervalMilliseconds = _emailSetting.EmailConfig.RetryIntervalMilliseconds;
 }
        public EmailMessagingService()
        {
            _emailSettings = SettingsHelper.LoadSettings <EmailSettings>("emailSettings", "EmailSettings");

            SendwithusClient.ApiKey = _emailSettings.ApiKey;
            SendwithusClient.SetTimeoutInMilliseconds(_emailSettings.Timeout);
            SendwithusClient.RetryCount = _emailSettings.RetryCount;
            SendwithusClient.RetryIntervalMilliseconds = _emailSettings.RetryInterval;
        }
Exemple #5
0
        /// <summary>
        /// Sets the base address, Accept type, API key, client info, and timeout for the HTTP Client
        /// </summary>
        /// <param name="client">The client to prepare to setup</param>
        private static void ConfigureHttpClient(HttpClient client)
        {
            client.BaseAddress = RequestManager.BaseAddress;
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var credentialsString = String.Format("{0}:{1}", SendwithusClient.ApiKey, SendwithusClient.ApiPassword);
            var credentialsBytes  = Encoding.ASCII.GetBytes(credentialsString);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentialsBytes));
            var clientStub = String.Format(CultureInfo.InvariantCulture, "{0}-{1}", SendwithusClient.CLIENT_LANGUAGE, SendwithusClient.CLIENT_VERSION);

            client.DefaultRequestHeaders.Add(SendwithusClient.SWU_CLIENT_HEADER, clientStub);
            client.Timeout = SendwithusClient.GetTimeout();
        }
        public async Task TestNonDefaultRetryInterval()
        {
            // Set the timeout low enough that the API call is guaranteed to fail and has a negligible effect on the run time
            SendwithusClient.SetTimeoutInMilliseconds(FAILURE_TIMEOUT_MILLISECONDS);

            // Make sure we're using the default number of retries
            //SendwithusClient.RetryCount = SendwithusClient.DEFAULT_RETRY_COUNT;
            SendwithusClient.RetryCount = NON_DEFAULT_RETRY_COUNT;

            // Make sure we're using the default retry interval
            SendwithusClient.RetryIntervalMilliseconds = NON_DEFAULT_RETRY_INTERVAL_MILLISECONDS;

            // Send a GET request
            var startTime = Stopwatch.GetTimestamp();

            try
            {
                var response = await Template.GetTemplateAsync(DEFAULT_TEMPLATE_ID);
            }
            catch (Exception ex)
            {
                var endTime = Stopwatch.GetTimestamp();

                // Make sure the API call failed on a timeout
                //SendwithusClientTest.ValidateAggregateException<TaskCanceledException>(SendwithusClient.DEFAULT_RETRY_COUNT, ex);
                SendwithusClientTest.ValidateAggregateException <TaskCanceledException>(NON_DEFAULT_RETRY_COUNT, ex);

                // Validate the API call's execution time
                var elapsedTime         = endTime - startTime;
                var elapsedMilliSeconds = elapsedTime * (1000.0 / Stopwatch.Frequency);
                SendwithusClientTest.ValidateApiCallExecutionTime(elapsedMilliSeconds);
            }
            finally
            {
                // Set the timeout back to its default value
                SendwithusClient.SetTimeoutInMilliseconds(SendwithusClient.DEFAULT_TIMEOUT_MILLISECONDS);

                // Set the retry count back to its default value
                SendwithusClient.RetryCount = SendwithusClient.DEFAULT_RETRY_COUNT;

                // Set the retry interval back to its default value
                SendwithusClient.RetryIntervalMilliseconds = SendwithusClient.DEFAULT_RETRY_INTERVAL_MILLISECONDS;
            }
        }
Exemple #7
0
        public async Task TestTimeoutFailure()
        {
            // Set the timeout to a value that is sure to trigger a failure
            SendwithusClient.SetTimeoutInMilliseconds(FAILURE_TIMEOUT_MILLISECONDS);

            // Send the GET request
            try
            {
                var response = await Template.GetTemplateAsync(DEFAULT_TEMPLATE_ID);
            }
            catch (Exception ex)
            {
                SendwithusClientTest.ValidateAggregateException <TaskCanceledException>(SendwithusClient.DEFAULT_RETRY_COUNT, ex);
            }
            finally
            {
                // Set the timeout back to its default value
                SendwithusClient.SetTimeoutInMilliseconds(SendwithusClient.DEFAULT_TIMEOUT_MILLISECONDS);
            }
        }
Exemple #8
0
        public async Task TestTimeoutDefaultTimeout()
        {
            // Make sure the timeout is set to its default value
            SendwithusClient.SetTimeoutInMilliseconds(SendwithusClient.DEFAULT_TIMEOUT_MILLISECONDS);

            // Send the GET request
            try
            {
                var response = await Template.GetTemplateAsync(DEFAULT_TEMPLATE_ID);

                Trace.WriteLine(String.Format("API call completed with default timeout of: {0}ms", SendwithusClient.DEFAULT_TIMEOUT_MILLISECONDS));

                // Make sure we received a valid response
                SendwithusClientTest.ValidateResponse(response);
            }
            catch (AggregateException exception)
            {
                Assert.Fail(exception.ToString());
            }
        }
        public async Task TestDefaultRetryCountWithPostAsync()
        {
            // Set the timeout low enough that the API call is guaranteed to fail
            SendwithusClient.SetTimeoutInMilliseconds(FAILURE_TIMEOUT_MILLISECONDS);

            // Make sure we're using the default number of retries
            SendwithusClient.RetryCount = SendwithusClient.DEFAULT_RETRY_COUNT;

            // Send a POST request
            try
            {
                var createTemplateResponse = await TemplateTest.BuildAndSendCreateTemplateRequestWithAllParametersAsync();
            }
            catch (Exception ex)
            {
                SendwithusClientTest.ValidateAggregateException <TaskCanceledException>(SendwithusClient.DEFAULT_RETRY_COUNT, ex);
            }
            finally
            {
                // Set the timeout back to its default value
                SendwithusClient.SetTimeoutInMilliseconds(SendwithusClient.DEFAULT_TIMEOUT_MILLISECONDS);
            }
        }
        public async Task TestDefaultRetryCountWithDeleteAsync()
        {
            // Set the timeout low enough that the API call is guaranteed to fail
            SendwithusClient.SetTimeoutInMilliseconds(FAILURE_TIMEOUT_MILLISECONDS);

            // Make sure we're using the default number of retries
            SendwithusClient.RetryCount = SendwithusClient.DEFAULT_RETRY_COUNT;

            // Send a DELETE request
            try
            {
                var deleteCustomerReponse = await Customer.DeleteCustomerAsync(CustomerTest.NEW_CUSTOMER_EMAIL_ADDRESS);
            }
            catch (Exception ex)
            {
                SendwithusClientTest.ValidateAggregateException <TaskCanceledException>(SendwithusClient.DEFAULT_RETRY_COUNT, ex);
            }
            finally
            {
                // Set the timeout back to its default value
                SendwithusClient.SetTimeoutInMilliseconds(SendwithusClient.DEFAULT_TIMEOUT_MILLISECONDS);
            }
        }
        public async Task TestDefaultRetryCountWithPutAsync()
        {
            // Set the timeout low enough that the API call is guaranteed to fail
            SendwithusClient.SetTimeoutInMilliseconds(FAILURE_TIMEOUT_MILLISECONDS);

            // Make sure we're using the default number of retries
            SendwithusClient.RetryCount = SendwithusClient.DEFAULT_RETRY_COUNT;

            // Send a PUT request
            try
            {
                var setDefaultEspAccountResponse = await EspAccount.SetDefaultEspAccountAsync(DEFAULT_ESP_ACCOUNT_ID);
            }
            catch (Exception ex)
            {
                SendwithusClientTest.ValidateAggregateException <TaskCanceledException>(SendwithusClient.DEFAULT_RETRY_COUNT, ex);
            }
            finally
            {
                // Set the timeout back to its default value
                SendwithusClient.SetTimeoutInMilliseconds(SendwithusClient.DEFAULT_TIMEOUT_MILLISECONDS);
            }
        }