diff --git a/Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs b/Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs index 6b4910bc82..3f4cc279e4 100644 --- a/Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs +++ b/Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs @@ -1136,11 +1136,12 @@ private void ValidateLimitToEndpointSettings() } private void ValidatePartitionLevelFailoverSettings() - { - if (this.EnablePartitionLevelFailover - && (this.ApplicationPreferredRegions == null || this.ApplicationPreferredRegions.Count == 0)) - { - throw new ArgumentException($"{nameof(this.ApplicationPreferredRegions)} is required when {nameof(this.EnablePartitionLevelFailover)} is enabled."); + { + if (this.EnablePartitionLevelFailover + && string.IsNullOrEmpty(this.ApplicationRegion) + && (this.ApplicationPreferredRegions is null || this.ApplicationPreferredRegions.Count == 0)) + { + throw new ArgumentException($"{nameof(this.ApplicationPreferredRegions)} or {nameof(this.ApplicationRegion)} is required when {nameof(this.EnablePartitionLevelFailover)} is enabled."); } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientOptionsUnitTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientOptionsUnitTests.cs index 7561bbcf50..47c006e70e 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientOptionsUnitTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientOptionsUnitTests.cs @@ -285,7 +285,7 @@ public void CosmosClientOptions_WhenPartitionLevelFailoverEnabledAndPreferredReg ArgumentException exception = Assert.ThrowsException(() => cosmosClientBuilder.Build()); Assert.AreEqual( - expected: "ApplicationPreferredRegions is required when EnablePartitionLevelFailover is enabled.", + expected: "ApplicationPreferredRegions or ApplicationRegion is required when EnablePartitionLevelFailover is enabled.", actual: exception.Message); } finally @@ -1134,6 +1134,61 @@ public void TestServerCertificatesValidationWithDisableSSLFlagTrue(string connSt #nullable disable } + [TestMethod] + public void PPAFClientApplicationRegionCreationTest() + { + CosmosClientOptions cosmosClientOptions = new CosmosClientOptions + { + ApplicationRegion = Regions.WestUS2, + EnablePartitionLevelFailover = true + }; + + CosmosClient cosmosClient = new CosmosClient(ConnectionString, cosmosClientOptions); + Assert.AreEqual(Regions.WestUS2, cosmosClient.ClientOptions.ApplicationRegion); + Assert.IsTrue(cosmosClient.ClientOptions.EnablePartitionLevelFailover); + } + + [TestMethod] + public void PPAFClientApplicationPreferredRegionCreationTest() + { + CosmosClientOptions cosmosClientOptions = new CosmosClientOptions + { + ApplicationPreferredRegions = new List { Regions.WestUS2, Regions.EastUS2 }, + EnablePartitionLevelFailover = true + }; + + CosmosClient cosmosClient = new CosmosClient(ConnectionString, cosmosClientOptions); + Assert.AreEqual(Regions.WestUS2, cosmosClient.ClientOptions.ApplicationPreferredRegions[0]); + Assert.AreEqual(Regions.EastUS2, cosmosClient.ClientOptions.ApplicationPreferredRegions[1]); + Assert.IsTrue(cosmosClient.ClientOptions.EnablePartitionLevelFailover); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void PPAFClientAppRegionAndAppPreferredRegionTest() + { + CosmosClientOptions cosmosClientOptions = new CosmosClientOptions + { + EnablePartitionLevelFailover = true, + ApplicationPreferredRegions = new List { Regions.WestUS2, Regions.EastUS2 }, + ApplicationRegion = Regions.AustraliaCentral + }; + + _ = new CosmosClient(ConnectionString, cosmosClientOptions); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void PPAFClientNoRegionsTest() + { + CosmosClientOptions cosmosClientOptions = new CosmosClientOptions + { + EnablePartitionLevelFailover = true + }; + + _ = new CosmosClient(ConnectionString, cosmosClientOptions); + } + private class TestWebProxy : IWebProxy { public ICredentials Credentials { get; set; } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/PartitionKeyRangeFailoverTests/GlobalPartitionEndpointManagerTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/PartitionKeyRangeFailoverTests/GlobalPartitionEndpointManagerTests.cs index 60b6cb399f..f1d483888a 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/PartitionKeyRangeFailoverTests/GlobalPartitionEndpointManagerTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/PartitionKeyRangeFailoverTests/GlobalPartitionEndpointManagerTests.cs @@ -253,7 +253,7 @@ public void CreateItemAsync_WithNoPreferredRegionsAndServiceUnavailable_ShouldTh cosmosClientOptions)); Assert.AreEqual( - expected: "ApplicationPreferredRegions is required when EnablePartitionLevelFailover is enabled.", + expected: "ApplicationPreferredRegions or ApplicationRegion is required when EnablePartitionLevelFailover is enabled.", actual: exception.Message); }