test: flaky integration tests when GetRandomMovie() returns a soft-deleted movie

未关闭 适合新手
#600 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp
领域
testing

调研方向

从两个指定的 ServiceApplicationFactory.cs 文件中的 GetRandomMovie() 开始,比较它们各自如何选择有效电影。在两种实现中都排除软删除的条目,然后运行受影响的客户端和服务器集成测试;当随机选择无法返回软删除的电影且这些测试通过时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Describe the bug

Several integration tests are intermittently flaky because GetRandomMovie() can return a movie that an earlier test in the same class has already soft-deleted and never restored.

Observed failure, from PR #599 (Build and Test Library, run 42093778343). The change in that PR is unrelated, and the identical commit passed on a fork run:

CommunityToolkit.Datasync.Client.Test.Service.Integration_Tests.Read_SoftDeleted_NotDeleted_Returns200 [FAIL]
  CommunityToolkit.Datasync.Client.DatasyncHttpException : Gone
    at ServiceResponse.ThrowIfNotSuccessful(...)
    at DatasyncServiceClient`1.GetAsync(String id, ...)
    at Integration_Tests.Read_SoftDeleted_NotDeleted_Returns200() line 152

Root cause

  1. Each test class that uses the in-memory test server creates one ServiceApplicationFactory via IClassFixture<ServiceApplicationFactory>. All tests in the class share one in-memory movie repository.
  2. GetRandomMovie() picks a random movie that satisfies IsValid(...) (title, year and duration). It does not exclude movies where Deleted == true.
  3. Several tests soft-delete a random movie, either with SoftDelete(existingMovie) or through DELETE api/in-memory/softmovies/{id}. They never restore it.
  4. If a later test in the same class randomly picks one of those movies and calls the softmovies endpoint, the server correctly returns 410 Gone (or the test's other expectations break), and the test fails.

The failure rate is low and depends on test ordering. A few leaked soft-deleted rows sit in the set of seeded valid movies, so it is probably in the low single-digit percent range per run. That is why it only appears occasionally in CI.

I could not reproduce it locally. 15 consecutive runs of Service.Integration_Tests all passed.

Affected tests

Any test that calls GetRandomMovie() and expects a non-deleted movie, in a class that also soft-deletes movies. The soft-deleting tests also mutate Deleted state themselves.

tests/CommunityToolkit.Datasync.Client.Test

  • Service/Integration_Tests.cs soft-deletes in Create_SoftDeleted_Returns409, Read_SoftDeleted_Deleted_Returns404, Read_SoftDeleted_Deleted_WithIncludeDeleted_Returns200, Delete_NotSoftDeleted_Returns204 and Delete_SoftDeletedId_Returns410. All 16 GetRandomMovie() call sites in that file are exposed.

tests/CommunityToolkit.Datasync.Server.Test has the same GetRandomMovie() implementation in Helpers/ServiceApplicationFactory.cs, and the same soft-delete pattern:

  • Service/Read_Tests.cs: Read_SoftDeleted_Deleted_Returns410 and Read_SoftDeleted_Deleted_WithIncludeDeleted_Returns200. Read_SoftDeleted_NotDeleted_Returns200 has the same signature as the observed failure.
  • Service/Delete_Tests.cs: Delete_NotSoftDeleted_Returns204 and Delete_SoftDeletedId_Returns410.
  • Service/Replace_Tests.cs: three soft-delete tests.
  • Service/Create_Tests.cs: one soft-delete test.

Suggested fix

Smallest fix: exclude soft-deleted movies in both GetRandomMovie() implementations:

repository.GetEntities().Where(x => IsValid(x) && !x.Deleted).ToList();
  • tests/CommunityToolkit.Datasync.Client.Test/Helpers/ServiceApplicationFactory.cs
  • tests/CommunityToolkit.Datasync.Server.Test/Helpers/ServiceApplicationFactory.cs

Tests that need a deleted movie already call SoftDelete(...) explicitly, so they are unaffected.

A more robust option is to also restore or reset the repository per test, as ResetInMemoryMovies() already does for the offline push tests. That removes the cross-test state sharing entirely, at some cost in test runtime.

Additional context

Delete_* tests that hard-delete a movie remove it from the repository, so they don't cause this problem. Only soft-deleted rows that stay in the repository do.

主要语言
C#
星标
184
派生
48
平均合并
21 分钟
30 天内合并 PR
7

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CommunityToolkit/Datasync 的其他 Issue

查看 CommunityToolkit/Datasync 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。