Skip to content

Commit fee1997

Browse files
ananth7592Copilot
andcommitted
Routing: Adds env var wiring regression tests for LengthAware comparer
Adds LengthAwareRangeComparerEnvVarTests covering the end-to-end env-var propagation that this PR enables: AZURE_COSMOS_USE_LENGTH_AWARE_RANGE_COMPARATOR -> ConfigurationManager.IsLengthAwareRangeComparatorEnabled() -> CosmosClientOptions.UseLengthAwareRangeComparer (property initializer) -> ClientContextCore (useLengthAwareRangeComparer ctor arg) -> DocumentClient.UseLengthAwareRangeComparer -> PartitionKeyRangeCache.TryCombine consumer Ten unit tests, three layers: ConfigurationManager surface (3 tests): - env var unset returns the build-time default (true for GA/Preview, false for INTERNAL) - env var = ""false"" returns false in every build flavor - env var = ""true"" returns true in every build flavor CosmosClientOptions wiring (3 tests): - default UseLengthAwareRangeComparer respects env var = false - default UseLengthAwareRangeComparer respects env var = true - an explicit object-initializer setter overrides the env var End-to-end real CosmosClient construction (4 tests): - env var = false flows through ClientContextCore to CosmosClient.ClientOptions and to DocumentClient.UseLengthAwareRangeComparer (the value that PartitionKeyRangeCache.TryCombine consumes) - env var = true flows the same way - explicit CosmosClientOptions.UseLengthAwareRangeComparer = true beats env var = false through the real construction path - env var unset leaves CosmosClient.ClientOptions and DocumentClient on the build-time default Tests use Environment.SetEnvironmentVariable inside TestInitialize/TestCleanup with prior-value save/restore so they neither inherit pollution from earlier tests nor leak state to later tests. End-to-end tests use MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey against the local emulator endpoint so the CosmosClient constructor completes synchronously without any network call. Verified the tests have teeth: temporarily reverting the CosmosClientOptions.UseLengthAwareRangeComparer wiring back to the `#if !INTERNAL true #else false` literal makes the two key regression tests (`CosmosClientOptions_DefaultUseLengthAwareRangeComparer_RespectsEnvVarFalse` and `CosmosClient_UseLengthAwareRangeComparer_FlowsToDocumentClient_WhenEnvVarFalse`) fail with the expected assertion messages, while the other eight tests continue to pass (the hardcoded `true` happens to satisfy the `WhenEnvVarTrue`, `WhenEnvVarUnset`, and `ExplicitSetter` cases). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 244ca40 commit fee1997

1 file changed

Lines changed: 224 additions & 0 deletions

File tree

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
//------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
//------------------------------------------------------------
4+
5+
namespace Microsoft.Azure.Cosmos.Tests
6+
{
7+
using System;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
/// <summary>
11+
/// Verifies that the AZURE_COSMOS_USE_LENGTH_AWARE_RANGE_COMPARATOR environment
12+
/// variable controls the LengthAware range comparator end-to-end:
13+
///
14+
/// ENV var
15+
/// -> ConfigurationManager.IsLengthAwareRangeComparatorEnabled()
16+
/// -> CosmosClientOptions.UseLengthAwareRangeComparer (property initializer)
17+
/// -> ClientContextCore (useLengthAwareRangeComparer ctor arg)
18+
/// -> DocumentClient.UseLengthAwareRangeComparer (consumed by
19+
/// PartitionKeyRangeCache.TryCombine).
20+
///
21+
/// These tests are the regression bar for PR #5866 (Routing: Fixes LengthAware
22+
/// comparer not respecting env var in GA SDK). Before that fix, setting the env
23+
/// var to "false" had no effect on a real CosmosClient because
24+
/// CosmosClientOptions.UseLengthAwareRangeComparer was a hardcoded "true" literal
25+
/// gated only by the INTERNAL compile-time symbol.
26+
/// </summary>
27+
[TestClass]
28+
public class LengthAwareRangeComparerEnvVarTests
29+
{
30+
private const string EnvVarName = "AZURE_COSMOS_USE_LENGTH_AWARE_RANGE_COMPARATOR";
31+
private const string AccountEndpoint = "https://localhost:8081/";
32+
33+
// Default the runtime-build expects when the env var is unset.
34+
// The product code defaults to true in all non-INTERNAL builds; the test
35+
// assembly is built without the INTERNAL symbol, so we mirror that here.
36+
#if INTERNAL
37+
private const bool DefaultWhenUnset = false;
38+
#else
39+
private const bool DefaultWhenUnset = true;
40+
#endif
41+
42+
private string priorEnvVarValue;
43+
44+
[TestInitialize]
45+
public void TestInitialize()
46+
{
47+
this.priorEnvVarValue = Environment.GetEnvironmentVariable(EnvVarName);
48+
Environment.SetEnvironmentVariable(EnvVarName, null);
49+
}
50+
51+
[TestCleanup]
52+
public void TestCleanup()
53+
{
54+
Environment.SetEnvironmentVariable(EnvVarName, this.priorEnvVarValue);
55+
}
56+
57+
// ---------- ConfigurationManager surface ----------
58+
59+
[TestMethod]
60+
[Owner("amudumba")]
61+
public void IsLengthAwareRangeComparatorEnabled_WhenEnvVarUnset_ReturnsBuildDefault()
62+
{
63+
Assert.AreEqual(
64+
DefaultWhenUnset,
65+
ConfigurationManager.IsLengthAwareRangeComparatorEnabled(),
66+
"Unset env var should return the build-time default (true for GA/Preview, false for INTERNAL).");
67+
}
68+
69+
[TestMethod]
70+
[Owner("amudumba")]
71+
public void IsLengthAwareRangeComparatorEnabled_WhenEnvVarFalse_ReturnsFalse()
72+
{
73+
Environment.SetEnvironmentVariable(EnvVarName, "false");
74+
75+
Assert.IsFalse(
76+
ConfigurationManager.IsLengthAwareRangeComparatorEnabled(),
77+
"Env var=false must disable LengthAware comparator in every build flavor.");
78+
}
79+
80+
[TestMethod]
81+
[Owner("amudumba")]
82+
public void IsLengthAwareRangeComparatorEnabled_WhenEnvVarTrue_ReturnsTrue()
83+
{
84+
Environment.SetEnvironmentVariable(EnvVarName, "true");
85+
86+
Assert.IsTrue(
87+
ConfigurationManager.IsLengthAwareRangeComparatorEnabled(),
88+
"Env var=true must enable LengthAware comparator in every build flavor (including INTERNAL).");
89+
}
90+
91+
// ---------- CosmosClientOptions wiring ----------
92+
93+
[TestMethod]
94+
[Owner("amudumba")]
95+
public void CosmosClientOptions_DefaultUseLengthAwareRangeComparer_RespectsEnvVarFalse()
96+
{
97+
Environment.SetEnvironmentVariable(EnvVarName, "false");
98+
99+
CosmosClientOptions options = new CosmosClientOptions();
100+
101+
Assert.IsFalse(
102+
options.UseLengthAwareRangeComparer,
103+
"Env var=false must propagate to CosmosClientOptions.UseLengthAwareRangeComparer at construction time. "
104+
+ "Before PR #5866 this was a hardcoded 'true' literal and the env var was silently ignored.");
105+
}
106+
107+
[TestMethod]
108+
[Owner("amudumba")]
109+
public void CosmosClientOptions_DefaultUseLengthAwareRangeComparer_RespectsEnvVarTrue()
110+
{
111+
Environment.SetEnvironmentVariable(EnvVarName, "true");
112+
113+
CosmosClientOptions options = new CosmosClientOptions();
114+
115+
Assert.IsTrue(
116+
options.UseLengthAwareRangeComparer,
117+
"Env var=true must propagate to CosmosClientOptions.UseLengthAwareRangeComparer at construction time.");
118+
}
119+
120+
[TestMethod]
121+
[Owner("amudumba")]
122+
public void CosmosClientOptions_ExplicitSetter_OverridesEnvVar()
123+
{
124+
Environment.SetEnvironmentVariable(EnvVarName, "false");
125+
126+
CosmosClientOptions options = new CosmosClientOptions
127+
{
128+
UseLengthAwareRangeComparer = true,
129+
};
130+
131+
Assert.IsTrue(
132+
options.UseLengthAwareRangeComparer,
133+
"An explicit setter assignment must override the env-var-sourced default. "
134+
+ "The property initializer runs first; user-supplied object-initializer values take precedence.");
135+
}
136+
137+
// ---------- End-to-end: real CosmosClient construction ----------
138+
139+
[TestMethod]
140+
[Owner("amudumba")]
141+
public void CosmosClient_UseLengthAwareRangeComparer_FlowsToDocumentClient_WhenEnvVarFalse()
142+
{
143+
Environment.SetEnvironmentVariable(EnvVarName, "false");
144+
145+
using CosmosClient cosmosClient = new CosmosClient(
146+
AccountEndpoint,
147+
MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey);
148+
149+
Assert.IsFalse(
150+
cosmosClient.ClientOptions.UseLengthAwareRangeComparer,
151+
"Env var=false must reach CosmosClient.ClientOptions on a real (non-mock) construction path.");
152+
153+
Assert.IsFalse(
154+
cosmosClient.DocumentClient.UseLengthAwareRangeComparer,
155+
"Env var=false must propagate through ClientContextCore into the underlying DocumentClient — "
156+
+ "this is the value that PartitionKeyRangeCache.TryCombine consumes.");
157+
}
158+
159+
[TestMethod]
160+
[Owner("amudumba")]
161+
public void CosmosClient_UseLengthAwareRangeComparer_FlowsToDocumentClient_WhenEnvVarTrue()
162+
{
163+
Environment.SetEnvironmentVariable(EnvVarName, "true");
164+
165+
using CosmosClient cosmosClient = new CosmosClient(
166+
AccountEndpoint,
167+
MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey);
168+
169+
Assert.IsTrue(
170+
cosmosClient.ClientOptions.UseLengthAwareRangeComparer,
171+
"Env var=true must reach CosmosClient.ClientOptions on a real (non-mock) construction path.");
172+
173+
Assert.IsTrue(
174+
cosmosClient.DocumentClient.UseLengthAwareRangeComparer,
175+
"Env var=true must propagate through ClientContextCore into the underlying DocumentClient.");
176+
}
177+
178+
[TestMethod]
179+
[Owner("amudumba")]
180+
public void CosmosClient_UseLengthAwareRangeComparer_ExplicitOptionOverridesEnvVar()
181+
{
182+
// Env says "off" but caller explicitly opts back in via options.
183+
Environment.SetEnvironmentVariable(EnvVarName, "false");
184+
185+
CosmosClientOptions options = new CosmosClientOptions
186+
{
187+
UseLengthAwareRangeComparer = true,
188+
};
189+
190+
using CosmosClient cosmosClient = new CosmosClient(
191+
AccountEndpoint,
192+
MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey,
193+
options);
194+
195+
Assert.IsTrue(
196+
cosmosClient.ClientOptions.UseLengthAwareRangeComparer,
197+
"Explicit CosmosClientOptions.UseLengthAwareRangeComparer must beat the env var on the real construction path.");
198+
199+
Assert.IsTrue(
200+
cosmosClient.DocumentClient.UseLengthAwareRangeComparer,
201+
"Explicit option must also flow through to DocumentClient.");
202+
}
203+
204+
[TestMethod]
205+
[Owner("amudumba")]
206+
public void CosmosClient_UseLengthAwareRangeComparer_DefaultsToBuildDefault_WhenEnvVarUnset()
207+
{
208+
// env var already cleared by TestInitialize
209+
using CosmosClient cosmosClient = new CosmosClient(
210+
AccountEndpoint,
211+
MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey);
212+
213+
Assert.AreEqual(
214+
DefaultWhenUnset,
215+
cosmosClient.ClientOptions.UseLengthAwareRangeComparer,
216+
"Unset env var should leave the real CosmosClient on the build-time default.");
217+
218+
Assert.AreEqual(
219+
DefaultWhenUnset,
220+
cosmosClient.DocumentClient.UseLengthAwareRangeComparer,
221+
"Unset env var should leave DocumentClient on the build-time default.");
222+
}
223+
}
224+
}

0 commit comments

Comments
 (0)