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

Aberta Para iniciantes
#600 1 comentário 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
78/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
csharp
Domínio
testing

Direção de pesquisa

Comece por GetRandomMovie() nos dois arquivos ServiceApplicationFactory.cs mencionados e compare como cada um seleciona filmes válidos. Exclua entradas excluídas logicamente em ambas as implementações e, em seguida, execute os testes de integração afetados do cliente e do servidor; a tarefa estará concluída quando a seleção aleatória não puder retornar um filme excluído logicamente e esses testes passarem.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.

Linguagem predominante
C#
Estrelas
184
Forks
48
Merge médio
21min
PRs com merge (30d)
7

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de CommunityToolkit/Datasync

Todas as issues de CommunityToolkit/Datasync

Issues semelhantes

Mais issues de C#

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.