From 17f82e914d8f3b9e9526fb21261fcab08b3ebda0 Mon Sep 17 00:00:00 2001 From: Georg von Kries Date: Thu, 23 May 2024 15:16:20 +0200 Subject: [PATCH 1/3] Fix flaky test. --- .../Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs b/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs index 350362a26de..498496f346a 100644 --- a/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs +++ b/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs @@ -12,6 +12,14 @@ public async Task ShouldListBlogPostWhenCallingAQuery() using var context = new BlogContext(); await context.InitializeAsync(); + // The RecentBlogPosts query sorts the content items by CreatedUtc. Since + // the CreatedUtc property is managed automatically and cannot be set when + // adding a content item, adding this delay ensures that the second post is + // always newer. + // The delay was chosen to be higher than the clock resolution in most + // operating systems. + await Task.Delay(20); + var blogPostContentItemId = await context .CreateContentItem("BlogPost", builder => { From ad70147bae42ec8dee11f909cb3b20c95ab0d62c Mon Sep 17 00:00:00 2001 From: Georg von Kries Date: Fri, 24 May 2024 09:42:40 +0200 Subject: [PATCH 2/3] Don't use a timeout. --- .../Queries/RecentBlogPostsQueryTests.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs b/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs index 498496f346a..8263739314e 100644 --- a/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs +++ b/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs @@ -12,14 +12,6 @@ public async Task ShouldListBlogPostWhenCallingAQuery() using var context = new BlogContext(); await context.InitializeAsync(); - // The RecentBlogPosts query sorts the content items by CreatedUtc. Since - // the CreatedUtc property is managed automatically and cannot be set when - // adding a content item, adding this delay ensures that the second post is - // always newer. - // The delay was chosen to be higher than the clock resolution in most - // operating systems. - await Task.Delay(20); - var blogPostContentItemId = await context .CreateContentItem("BlogPost", builder => { @@ -43,11 +35,17 @@ public async Task ShouldListBlogPostWhenCallingAQuery() .WithField("displayText"); }); - var nodes = result["data"]["recentBlogPosts"]; + var jsonArray = result["data"]?["recentBlogPosts"]?.AsArray(); + + Assert.NotNull(jsonArray); + Assert.Equal(2, jsonArray.Count); - Assert.Equal(2, nodes.AsArray().Count); - Assert.Equal("Some sorta blogpost in a Query!", nodes[0]["displayText"].ToString()); - Assert.Equal("Man must explore, and this is exploration at its greatest", nodes[1]["displayText"].ToString()); + // The RecentBlogPosts query sorts the content items by CreatedUtc. If the + // test is executing too fast, both blog entries may have the same CreatedUtc + // value and ordering becomes random. Because of this, we do not assert the order + // of the result. + Assert.Contains("Some sorta blogpost in a Query!", jsonArray.Select(node => node["displayText"]?.ToString())); + Assert.Contains("Man must explore, and this is exploration at its greatest", jsonArray.Select(node => node["displayText"]?.ToString())); } } } From 748050f13d7f3079dd9863bbd26c3ce698942696 Mon Sep 17 00:00:00 2001 From: Georg von Kries Date: Sat, 25 May 2024 16:16:44 +0200 Subject: [PATCH 3/3] React to code review. --- .../Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs b/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs index 8263739314e..79a0033299b 100644 --- a/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs +++ b/test/OrchardCore.Tests/Apis/GraphQL/Queries/RecentBlogPostsQueryTests.cs @@ -44,8 +44,10 @@ public async Task ShouldListBlogPostWhenCallingAQuery() // test is executing too fast, both blog entries may have the same CreatedUtc // value and ordering becomes random. Because of this, we do not assert the order // of the result. - Assert.Contains("Some sorta blogpost in a Query!", jsonArray.Select(node => node["displayText"]?.ToString())); - Assert.Contains("Man must explore, and this is exploration at its greatest", jsonArray.Select(node => node["displayText"]?.ToString())); + var displayTexts = jsonArray.Select(node => node["displayText"]?.ToString()); + + Assert.Contains("Some sorta blogpost in a Query!", displayTexts); + Assert.Contains("Man must explore, and this is exploration at its greatest", displayTexts); } } }