Skip to content

Commit

Permalink
Refactor CustomerService.Search test to wait up to 5 seconds for sear…
Browse files Browse the repository at this point in the history
…ch index and then test results
  • Loading branch information
nozzlegear committed Jul 18, 2024
1 parent f540daa commit 17b9806
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions ShopifySharp.Tests/Services/CustomerServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,33 @@ public async Task Updates_Customers_With_Options()
[Fact]
public async Task Searches_For_Customers()
{
// It takes anywhere between 3 seconds to 30 seconds for Shopify to index new customers for searches.
// Rather than putting a 20 second Thread.Sleep in the test, we'll just assume it's successful if the
// test doesn't throw an exception.
bool threw = false;
var searchTry = 0;

try
{
var search = await Fixture.Service.SearchAsync(new CustomerSearchListFilter { Query = "John" });
}
catch (ShopifyException ex)
// Setup
var customer = await Fixture.Create();
var filter = new CustomerSearchListFilter
{
Query = $"email:{customer.Email}",
};

threw = true;
// Act
var search = await Fixture.Service.SearchAsync(filter);

while (!search.Items.Any() && searchTry < 4)
{
// The search index has a bit of a delay to it. Try up to 4 times before asserting.
searchTry++;
await Task.Delay(1000);
search = await Fixture.Service.SearchAsync(filter);
}

Assert.False(threw);
// Assert
search.Should()
.NotBeNull();
search.Items.Should()
.NotBeEmpty()
.And.HaveCount(1)
.And.AllSatisfy(c => c.Email.Should().Be(customer.Email));
}

[Fact]
Expand Down

0 comments on commit 17b9806

Please sign in to comment.