Skip to content

Fix failing Cosmos tests - #38808

Open
AndriySvyryd wants to merge 1 commit into
mainfrom
FlakyCosmos
Open

Fix failing Cosmos tests#38808
AndriySvyryd wants to merge 1 commit into
mainfrom
FlakyCosmos

Conversation

@AndriySvyryd

@AndriySvyryd AndriySvyryd commented Aug 14, 2026

Copy link
Copy Markdown
Member
  • Some of the flakiness is caused by CosmosClient caching container RIDs internally, making the next query after a container is recreated to fail. The workaround is to clean the databases by deleting the documents without recreating the containers.
  • Cleanup service providers proactively in tests to release resources held by CosmosClient faster
  • Update asserts in some tests due to new Cosmos behavior in negative cases
  • Skip tests consistently failing on Linux Cosmos emulator
  • Create triggers inside an ExecutionStrategy

@AndriySvyryd
AndriySvyryd requested review from cincuranet and a lite review from Copilot August 14, 2026 05:08
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner August 14, 2026 05:08
@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) August 14, 2026 05:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to reduce test flakiness across environments by (1) skipping known-problematic Cosmos emulator scenarios on Linux and (2) improving test fixture cleanup by disposing the fixture’s service provider. It also adjusts a relational UDF test to run under non-RELEASE builds.

Changes:

  • Dispose SharedStoreFixtureBase’s ServiceProvider during fixture teardown.
  • Skip oversized-request Cosmos tests on the Linux Cosmos emulator via ConditionalFact.
  • Update a relational UDF test to include non-RELEASE behavior (but the current expectation likely breaks Debug builds).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
test/EFCore.Specification.Tests/SharedStoreFixtureBase.cs Adds service provider disposal during async fixture teardown.
test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs Changes conditional compilation for a nullable-return UDF test to run in non-RELEASE builds.
test/EFCore.Cosmos.FunctionalTests/CosmosTransactionalBatchTest.cs Skips oversized payload tests on Linux emulator where behavior differs (timeouts).

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/EFCore.Specification.Tests/SharedStoreFixtureBase.cs
Copilot AI review requested due to automatic review settings August 14, 2026 20:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Suppressed comments (2)

test/EFCore.Cosmos.FunctionalTests/CosmosConcurrencyTest.cs:135

  • Same issue as above: without specifying an internal service provider, this path will create/cache another root provider that isn't deterministically disposed. Reuse the fixture's provider so cleanup happens via SharedStoreFixtureBase.DisposeAsync().
            .Options;

test/EFCore.Cosmos.FunctionalTests/CosmosConcurrencyTest.cs:71

  • After removing the explicitly managed ServiceProvider, these options will cause EF to build/cache an internal root service provider (via ServiceProviderCache) which isn't deterministically disposed during test cleanup. Since the fixture already owns and disposes a provider, reuse it here to avoid leaking providers/resources across the test run.

This issue also appears on line 135 of the same file.

            .Options;

Copilot AI review requested due to automatic review settings August 14, 2026 22:26
@AndriySvyryd AndriySvyryd changed the title Skip failing tests on Linux Cosmos emulator Fix Cosmos tests Aug 14, 2026
@AndriySvyryd AndriySvyryd changed the title Fix Cosmos tests Fix failing Cosmos tests Aug 14, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Suppressed comments (2)

test/EFCore.Cosmos.FunctionalTests/CosmosConcurrencyTest.cs:126

  • The locally created serviceProvider is never used (it's not passed via UseInternalServiceProvider or otherwise referenced), so this allocates and disposes a provider for no effect. This adds unnecessary overhead to the test and makes the intent unclear.
        await using var serviceProvider = new ServiceCollection()
            .AddEntityFrameworkCosmos()
            .BuildServiceProvider();

test/EFCore.Cosmos.FunctionalTests/TestUtilities/CosmosTestStore.cs:412

  • DeleteDocumentsAsync queries SELECT VALUE c FROM c, which downloads entire documents (including any large payload properties) and then buffers all (id, partitionKey) pairs in memory before deleting. For shared stores this can significantly slow down cleanup and increase memory usage. Consider projecting only the needed fields (id + partition key store names) and deleting as you iterate pages.
            var documents = new List<(string Id, PartitionKey PartitionKey)>();
            using var iterator = container.GetItemQueryIterator<JsonElement>("SELECT VALUE c FROM c");

            while (iterator.HasMoreResults)
            {

Cleanup service providers proactively in tests
Copilot AI review requested due to automatic review settings August 15, 2026 00:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Suppressed comments (2)

test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs:1077

  • The non-RELEASE branch expects UnreachableException, but the translation pipeline throws InvalidOperationException for nullable-value-type translations regardless of build configuration (see RelationalMethodCallTranslatorProvider throwing InvalidOperationException for nullable value types). This will make the test fail in non-RELEASE builds.
#if RELEASE
        var exception = Assert.Throws<InvalidOperationException>(
            () => context.Customers.Where(c => c.Id == UDFSqlContext.NullableValueReturnType()).ToList());

        Assert.Equal(
            RelationalStrings.DbFunctionNullableValueReturnType(
                context.Model.FindDbFunction(typeof(UDFSqlContext).GetMethod(nameof(UDFSqlContext.NullableValueReturnType))!)!.ModelName,
                "int?"),
            exception.Message);
#else
        Assert.Throws<UnreachableException>(
            () => context.Customers.Where(c => c.Id == UDFSqlContext.NullableValueReturnType()).ToList());
#endif

test/EFCore.Cosmos.FunctionalTests/CosmosConcurrencyTest.cs:140

  • A ServiceProvider is created and disposed, but it isn’t actually used by EF for internal services since it isn’t passed into the options. If the intent is to proactively dispose the internal provider/CosmosClient resources, pass it via UseInternalServiceProvider; otherwise remove the ServiceProvider creation.
        await using var serviceProvider = new ServiceCollection()
            .AddEntityFrameworkCosmos()
            .BuildServiceProvider();
        var options = Fixture.TestStore.AddProviderOptions(
                Fixture.AddOptions(
                    new DbContextOptionsBuilder()
                        .UseCosmos(o =>
                        {
                            if (contentResponseOnWriteEnabled != null)
                            {
#pragma warning disable CS0618 // Type or member is obsolete
                                o.ContentResponseOnWriteEnabled(contentResponseOnWriteEnabled.Value);
#pragma warning restore CS0618 // Type or member is obsolete
                            }
                        })))
            .EnableServiceProviderCaching(false)
            .Options;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants