From e72e3283ba86a065f425efc694a6f13df48cd458 Mon Sep 17 00:00:00 2001 From: Joshua Harms Date: Thu, 22 Jun 2023 20:33:40 -0500 Subject: [PATCH] Improve reliability of request cancellation tests Added a short, one-second delay to each of the TestService's request methods to ensure the cancellation token is used before the http request is formed and sent. Also added Shopify's mock.shop API test endpoint as the TestService's target domain so an HttpRequest will actually send if it does start, rather than ending immediately with an invalid domain. --- .../ExecuteRequestCancellation_Tests.cs | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ShopifySharp.Tests/ExecuteRequestCancellation_Tests.cs b/ShopifySharp.Tests/ExecuteRequestCancellation_Tests.cs index 4b38df7d..ab3d2760 100644 --- a/ShopifySharp.Tests/ExecuteRequestCancellation_Tests.cs +++ b/ShopifySharp.Tests/ExecuteRequestCancellation_Tests.cs @@ -15,43 +15,57 @@ public class ExecuteRequestCancellation_Tests { private class TestService : ShopifyService { - public TestService() : base("someurl", "sometoken") + // Use Shopify's mock.shop API as an API endpoint which doesn't require an access token. + private const string MyShopifyDomain = "https://mock.shop"; + + /// Number of seconds to delay before sending request to the mock.shop API. This is only used to ensure the + /// cancellationToken is canceled before the request is actually made. + private readonly TimeSpan _delay = TimeSpan.FromSeconds(1); + + public TestService() : base(MyShopifyDomain, "fake-token") { } - // these are what services call to execute their public API requests - public new Task ExecuteGetAsync(string path, string resultRootElt, string fields, CancellationToken cancellationToken = default) + // These are what services call to execute their public API requests + public Task ExecuteGetAsync(string path, string resultRootElt, string fields, CancellationToken cancellationToken = default) { + Thread.Sleep(_delay); return base.ExecuteGetAsync(path, resultRootElt, fields, cancellationToken); } - public new Task ExecuteGetAsync(string path, string resultRootElt, Parameterizable queryParams = null, CancellationToken cancellationToken = default) + public Task ExecuteGetAsync(string path, string resultRootElt, Parameterizable queryParams = null, CancellationToken cancellationToken = default) { + Thread.Sleep(_delay); return base.ExecuteGetAsync(path, resultRootElt, queryParams, cancellationToken); } - public new Task> ExecuteGetListAsync(string path, string resultRootElt, ListFilter filter, CancellationToken cancellationToken = default) + public Task> ExecuteGetListAsync(string path, string resultRootElt, ListFilter filter, CancellationToken cancellationToken = default) { + Thread.Sleep(_delay); return base.ExecuteGetListAsync(path, resultRootElt, filter, cancellationToken); } - public new Task ExecutePostAsync(string path, string resultRootElt, object jsonContent = null, CancellationToken cancellationToken = default) + public Task ExecutePostAsync(string path, string resultRootElt, object jsonContent = null, CancellationToken cancellationToken = default) { + Thread.Sleep(_delay); return base.ExecutePostAsync(path, resultRootElt, cancellationToken, jsonContent); } - public new Task ExecutePutAsync(string path, string resultRootElt, object jsonContent = null, CancellationToken cancellationToken = default) + public Task ExecutePutAsync(string path, string resultRootElt, object jsonContent = null, CancellationToken cancellationToken = default) { + Thread.Sleep(_delay); return base.ExecutePutAsync(path, resultRootElt, cancellationToken, jsonContent); } public new Task ExecuteDeleteAsync(string path, CancellationToken cancellationToken) { + Thread.Sleep(_delay); return base.ExecuteDeleteAsync(path, cancellationToken); } - public new Task> ExecuteRequestAsync(RequestUri uri, HttpMethod method, HttpContent content = null, CancellationToken cancellationToken = default) + public Task> ExecuteRequestAsync(RequestUri uri, HttpMethod method, HttpContent content = null, CancellationToken cancellationToken = default) { + Thread.Sleep(_delay); return base.ExecuteRequestAsync(uri, method, cancellationToken, content); } }