From f430299b1519cd4d9c766536c758bb3b79885b64 Mon Sep 17 00:00:00 2001 From: Nalu Tripician <27316859+NaluTripician@users.noreply.github.com> Date: Mon, 22 Jul 2024 10:11:21 -0700 Subject: [PATCH] bounded stalenness test with fix --- .../FaultInjection/src/FaultInjection.csproj | 1 - .../tests/FaultInjectionTests.csproj | 1 - .../src/RMResources.Designer.cs | 2 +- .../src/direct/QuorumReader.cs | 9 ++- .../CosmosItemTests.cs | 74 +++++++++++++++++++ ...icrosoft.Azure.Cosmos.EmulatorTests.csproj | 4 +- 6 files changed, 86 insertions(+), 5 deletions(-) diff --git a/Microsoft.Azure.Cosmos.Samples/Tools/FaultInjection/src/FaultInjection.csproj b/Microsoft.Azure.Cosmos.Samples/Tools/FaultInjection/src/FaultInjection.csproj index 9d40a016f8..dfd8f8693d 100644 --- a/Microsoft.Azure.Cosmos.Samples/Tools/FaultInjection/src/FaultInjection.csproj +++ b/Microsoft.Azure.Cosmos.Samples/Tools/FaultInjection/src/FaultInjection.csproj @@ -26,7 +26,6 @@ - diff --git a/Microsoft.Azure.Cosmos.Samples/Tools/FaultInjection/tests/FaultInjectionTests.csproj b/Microsoft.Azure.Cosmos.Samples/Tools/FaultInjection/tests/FaultInjectionTests.csproj index f52fc7cdfd..6489fc45e1 100644 --- a/Microsoft.Azure.Cosmos.Samples/Tools/FaultInjection/tests/FaultInjectionTests.csproj +++ b/Microsoft.Azure.Cosmos.Samples/Tools/FaultInjection/tests/FaultInjectionTests.csproj @@ -17,7 +17,6 @@ - diff --git a/Microsoft.Azure.Cosmos/src/RMResources.Designer.cs b/Microsoft.Azure.Cosmos/src/RMResources.Designer.cs index 5629d0cc8a..ba6cbc77d9 100644 --- a/Microsoft.Azure.Cosmos/src/RMResources.Designer.cs +++ b/Microsoft.Azure.Cosmos/src/RMResources.Designer.cs @@ -46,7 +46,7 @@ internal RMResources() if (object.ReferenceEquals(resourceMan, null)) { #if COSMOSCLIENT - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.Azure.Documents.RMResources", typeof(RMResources).Assembly); + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.Azure.Cosmos.RMResources", typeof(RMResources).Assembly); #else global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.Azure.Documents.RMResources", typeof(RMResources).GetAssembly()); #endif diff --git a/Microsoft.Azure.Cosmos/src/direct/QuorumReader.cs b/Microsoft.Azure.Cosmos/src/direct/QuorumReader.cs index 2a96228e29..e5e3aa3772 100644 --- a/Microsoft.Azure.Cosmos/src/direct/QuorumReader.cs +++ b/Microsoft.Azure.Cosmos/src/direct/QuorumReader.cs @@ -795,7 +795,14 @@ private bool IsQuorumMet( selectedResponse = validReadResponses.Where(s => (s.Target.LSN == maxLsn) && (s.Target.StatusCode < StatusCodes.StartingErrorCode)).FirstOrDefault(); if (selectedResponse == null) { - selectedResponse = validReadResponses.First(s => s.Target.LSN == maxLsn); + try + { + selectedResponse = validReadResponses.First(s => s.Target.LSN == maxLsn); + } + catch + { + selectedResponse = validReadResponses.First(); + } } readLsn = selectedResponse.Target.ItemLSN == -1 ? diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs index 1463321ee7..9d99949168 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs @@ -33,6 +33,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests using System.Reflection; using System.Text.RegularExpressions; using Microsoft.Azure.Cosmos.Diagnostics; + using Microsoft.Azure.Cosmos.FaultInjection; [TestClass] public class CosmosItemTests : BaseCosmosClientHelper @@ -3320,6 +3321,79 @@ await CosmosItemTests.GivenItemAsyncWhenMissingMemberHandlingIsErrorThenExpectsC cancellationToken: cancellationToken)); } + [TestMethod] + public async Task TooManyReadTest() + { + string connectionString = ConfigurationManager.GetEnvironmentVariable("COSMOSDB_MULTI_REGION", null); + + if (string.IsNullOrEmpty(connectionString)) + { + Assert.Fail("Set environment variable COSMOSDB_MULTI_REGION to run the tests"); + } + + FaultInjectionCondition readConditon = new FaultInjectionConditionBuilder() + .WithOperationType(FaultInjectionOperationType.ReadItem) + .Build(); + IFaultInjectionResult tooManyRequestsResult = FaultInjectionResultBuilder + .GetResultBuilder(FaultInjectionServerErrorType.TooManyRequests) + .Build(); + FaultInjectionRule rule = new FaultInjectionRuleBuilder( + id: "tooMany", + condition: readConditon, + result: tooManyRequestsResult) + .WithDuration(TimeSpan.FromMinutes(90)) + .Build(); + + List rules = new List() { rule }; + FaultInjector faultInjector = new FaultInjector(rules); + + rule.Disable(); + + + CosmosClientOptions clientOptions = new CosmosClientOptions() + { + ConnectionMode = ConnectionMode.Direct, + ConsistencyLevel = Cosmos.ConsistencyLevel.Strong, + }; + + CosmosClient faultInjectionClient = new CosmosClient( + connectionString: connectionString, + clientOptions: faultInjector.GetFaultInjectionClientOptions(clientOptions)); + + Cosmos.Database db = await faultInjectionClient.CreateDatabaseIfNotExistsAsync("LoadTest"); + Container container = await db.CreateContainerIfNotExistsAsync("LoadContainer", "/pk"); + + dynamic item = new + { + id = "testId", + pk = "pk", + }; + + await container.CreateItemAsync(item); + + rule.Enable(); + + try + { + ItemResponse ir = await container.ReadItemAsync( + "testId", + new Cosmos.PartitionKey("pk"), + new ItemRequestOptions() + { + ExcludeRegions = new List() { "West US" } + }); + + + Assert.AreEqual(HttpStatusCode.OK, ir.StatusCode); + } + finally + { + await db.DeleteAsync(); + faultInjectionClient.Dispose(); + } + + } + private static async Task GivenItemStreamAsyncWhenMissingMemberHandlingIsErrorThenExpectsCosmosExceptionTestAsync( Func> itemStreamAsync) { diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Microsoft.Azure.Cosmos.EmulatorTests.csproj b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Microsoft.Azure.Cosmos.EmulatorTests.csproj index 928e057967..b99af2f55c 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Microsoft.Azure.Cosmos.EmulatorTests.csproj +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Microsoft.Azure.Cosmos.EmulatorTests.csproj @@ -19,7 +19,6 @@ - @@ -361,6 +360,9 @@ PreserveNewest + + + true