From 6835161f2d4cadec865f467a0d1a2a3d94432797 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Wed, 3 Dec 2025 20:06:21 +0000 Subject: [PATCH] .NET: Fix flaky vector store integration tests - race condition on indexing (#2586) * Initial plan * Add vector store indexing wait helpers to fix flaky tests Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> * Use proper VectorStoreStatus struct comparison in Azure tests Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> * Remove unused System.ClientModel import Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> * Remove excessive comments and unnecessary status variable per code review Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> --- .../AzureAIAgentsPersistentCreateTests.cs | 42 ++++++++++++++++++ .../OpenAIAssistantClientExtensionsTests.cs | 43 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/dotnet/tests/AzureAIAgentsPersistent.IntegrationTests/AzureAIAgentsPersistentCreateTests.cs b/dotnet/tests/AzureAIAgentsPersistent.IntegrationTests/AzureAIAgentsPersistentCreateTests.cs index 17fad1581f..a10cc11d79 100644 --- a/dotnet/tests/AzureAIAgentsPersistent.IntegrationTests/AzureAIAgentsPersistentCreateTests.cs +++ b/dotnet/tests/AzureAIAgentsPersistent.IntegrationTests/AzureAIAgentsPersistentCreateTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. using System; +using System.Diagnostics; using System.IO; using System.Threading.Tasks; using AgentConformance.IntegrationTests.Support; @@ -108,6 +109,9 @@ public class AzureAIAgentsPersistentCreateTests ); var vectorStoreMetadata = await this._persistentAgentsClient.VectorStores.CreateVectorStoreAsync([uploadedAgentFile.Id], name: "WordCodeLookup_VectorStore"); + // Wait for vector store indexing to complete before using it + await this.WaitForVectorStoreReadyAsync(this._persistentAgentsClient, vectorStoreMetadata.Value.Id); + // Act. var agent = createMechanism switch { @@ -292,4 +296,42 @@ public class AzureAIAgentsPersistentCreateTests await this._persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id); } } + + /// + /// Waits for a vector store to complete indexing by polling its status. + /// + /// The persistent agents client. + /// The ID of the vector store. + /// Maximum time to wait in seconds (default: 30). + /// A task that completes when the vector store is ready or throws on timeout/failure. + private async Task WaitForVectorStoreReadyAsync( + PersistentAgentsClient client, + string vectorStoreId, + int maxWaitSeconds = 30) + { + Stopwatch sw = Stopwatch.StartNew(); + while (sw.Elapsed.TotalSeconds < maxWaitSeconds) + { + PersistentAgentsVectorStore vectorStore = await client.VectorStores.GetVectorStoreAsync(vectorStoreId); + + if (vectorStore.Status == VectorStoreStatus.Completed) + { + if (vectorStore.FileCounts.Failed > 0) + { + throw new InvalidOperationException("Vector store indexing failed for some files"); + } + + return; + } + + if (vectorStore.Status == VectorStoreStatus.Expired) + { + throw new InvalidOperationException("Vector store has expired"); + } + + await Task.Delay(1000); + } + + throw new TimeoutException($"Vector store did not complete indexing within {maxWaitSeconds}s"); + } } diff --git a/dotnet/tests/OpenAIAssistant.IntegrationTests/OpenAIAssistantClientExtensionsTests.cs b/dotnet/tests/OpenAIAssistant.IntegrationTests/OpenAIAssistantClientExtensionsTests.cs index d187d85b78..f1373bd98b 100644 --- a/dotnet/tests/OpenAIAssistant.IntegrationTests/OpenAIAssistantClientExtensionsTests.cs +++ b/dotnet/tests/OpenAIAssistant.IntegrationTests/OpenAIAssistantClientExtensionsTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. using System; +using System.Diagnostics; using System.IO; using System.Threading.Tasks; using AgentConformance.IntegrationTests.Support; @@ -173,6 +174,9 @@ public class OpenAIAssistantClientExtensionsTests }); string vectorStoreId = vectorStoreCreate.Value.Id; + // Wait for vector store indexing to complete before using it + await WaitForVectorStoreReadyAsync(vectorStoreClient, vectorStoreId); + var fileSearchTool = new HostedFileSearchTool() { Inputs = [new HostedVectorStoreContent(vectorStoreId)] }; var agent = createMechanism switch @@ -219,4 +223,43 @@ public class OpenAIAssistantClientExtensionsTests File.Delete(searchFilePath); } } + + /// + /// Waits for a vector store to complete indexing by polling its status. + /// + /// The vector store client. + /// The ID of the vector store. + /// Maximum time to wait in seconds (default: 30). + /// A task that completes when the vector store is ready or throws on timeout/failure. + private static async Task WaitForVectorStoreReadyAsync( + VectorStoreClient client, + string vectorStoreId, + int maxWaitSeconds = 30) + { + Stopwatch sw = Stopwatch.StartNew(); + while (sw.Elapsed.TotalSeconds < maxWaitSeconds) + { + VectorStore vectorStore = await client.GetVectorStoreAsync(vectorStoreId); + VectorStoreStatus status = vectorStore.Status; + + if (status == VectorStoreStatus.Completed) + { + if (vectorStore.FileCounts.Failed > 0) + { + throw new InvalidOperationException("Vector store indexing failed for some files"); + } + + return; + } + + if (status == VectorStoreStatus.Expired) + { + throw new InvalidOperationException("Vector store has expired"); + } + + await Task.Delay(1000); + } + + throw new TimeoutException($"Vector store did not complete indexing within {maxWaitSeconds}s"); + } }