Skip to content

Commit ccdf49f

Browse files
committed
Add tests and code clean up.
1 parent d4b72d8 commit ccdf49f

4 files changed

Lines changed: 117 additions & 21 deletions

File tree

Microsoft.Azure.Cosmos/src/DocumentClient.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,26 +1106,29 @@ private async Task<bool> GetInitializationTaskAsync(IStoreClientFactory storeCli
11061106

11071107
gatewayStoreModel.SetCaches(this.partitionKeyRangeCache, this.collectionCache);
11081108

1109-
if (this.ConnectionPolicy.ConnectionMode == ConnectionMode.Gateway && this.isThinClientEnabled)
1110-
{
1111-
ThinClientStoreModel thinClientStoreModel = new (
1112-
endpointManager: this.GlobalEndpointManager,
1113-
this.PartitionKeyRangeLocation,
1114-
this.sessionContainer,
1115-
(Cosmos.ConsistencyLevel)this.accountServiceConfiguration.DefaultConsistencyLevel,
1116-
this.eventSource,
1117-
this.serializerSettings,
1118-
this.httpClient,
1119-
this.ConnectionPolicy.UserAgentContainer,
1120-
isPartitionLevelFailoverEnabled: this.ConnectionPolicy.EnablePartitionLevelFailover || this.ConnectionPolicy.EnablePartitionLevelCircuitBreaker);
1121-
1122-
thinClientStoreModel.SetCaches(this.partitionKeyRangeCache, this.collectionCache);
1123-
1124-
this.StoreModel = thinClientStoreModel;
1125-
}
1126-
else if (this.ConnectionPolicy.ConnectionMode == ConnectionMode.Gateway)
1127-
{
1128-
this.StoreModel = this.GatewayStoreModel;
1109+
if (this.ConnectionPolicy.ConnectionMode == ConnectionMode.Gateway)
1110+
{
1111+
if (this.isThinClientEnabled)
1112+
{
1113+
ThinClientStoreModel thinClientStoreModel = new (
1114+
endpointManager: this.GlobalEndpointManager,
1115+
this.PartitionKeyRangeLocation,
1116+
this.sessionContainer,
1117+
(Cosmos.ConsistencyLevel)this.accountServiceConfiguration.DefaultConsistencyLevel,
1118+
this.eventSource,
1119+
this.serializerSettings,
1120+
this.httpClient,
1121+
this.ConnectionPolicy.UserAgentContainer,
1122+
isPartitionLevelFailoverEnabled: this.ConnectionPolicy.EnablePartitionLevelFailover || this.ConnectionPolicy.EnablePartitionLevelCircuitBreaker);
1123+
1124+
thinClientStoreModel.SetCaches(this.partitionKeyRangeCache, this.collectionCache);
1125+
1126+
this.StoreModel = thinClientStoreModel;
1127+
}
1128+
else
1129+
{
1130+
this.StoreModel = this.GatewayStoreModel;
1131+
}
11291132
}
11301133
else
11311134
{

Microsoft.Azure.Cosmos/src/Routing/GlobalPartitionEndpointManagerCore.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ internal sealed class GlobalPartitionEndpointManagerCore : GlobalPartitionEndpoi
5252
/// A readonly boolean flag used to determine if partition level failover is enabled.
5353
/// </summary>
5454
private readonly bool isPartitionLevelFailoverEnabled;
55+
5556
/// <summary>
5657
/// A readonly boolean flag used to determine if thinclient is enabled.
5758
/// </summary>

Microsoft.Azure.Cosmos/src/ThinClientStoreClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private async ValueTask<HttpRequestMessage> PrepareRequestForProxyAsync(
139139
ThinClientConstants.UserAgent,
140140
this.userAgentContainer.UserAgent);
141141

142-
Guid activityId = System.Diagnostics.Trace.CorrelationManager.ActivityId;
142+
Guid activityId = Trace.CorrelationManager.ActivityId;
143143
Debug.Assert(activityId != Guid.Empty);
144144
requestMessage.Headers.TryAddWithoutValidation(
145145
HttpConstants.HttpHeaders.ActivityId, activityId.ToString());

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemThinClientTests.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,98 @@ public async Task CreateItemsTest()
121121
{
122122
ItemResponse<TestObject> response = await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
123123
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
124+
string diagnostics = response.Diagnostics.ToString();
125+
Assert.IsTrue(diagnostics.Contains("ThinClientStoreModel"), "Diagnostics should contain 'ThinClientStoreModel'");
126+
}
127+
}
128+
129+
[TestMethod]
130+
[TestCategory("ThinClient")]
131+
public async Task CreateItemsTestWithThinClientFlagEnabledAndAccountDisabled()
132+
{
133+
Environment.SetEnvironmentVariable(ConfigurationManager.ThinClientModeEnabled, "True");
134+
this.connectionString = Environment.GetEnvironmentVariable("COSMOSDB_ACCOUNT_CONNECTION_STRING");
135+
136+
if (string.IsNullOrEmpty(this.connectionString))
137+
{
138+
Assert.Fail("Set environment variable COSMOSDB_ACCOUNT_CONNECTION_STRING to run the tests");
139+
}
140+
141+
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
142+
{
143+
PropertyNamingPolicy = null,
144+
PropertyNameCaseInsensitive = true,
145+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
146+
};
147+
this.cosmosSystemTextJsonSerializer = new MultiRegionSetupHelpers.CosmosSystemTextJsonSerializer(jsonSerializerOptions);
148+
149+
this.client = new CosmosClient(
150+
this.connectionString,
151+
new CosmosClientOptions()
152+
{
153+
ConnectionMode = ConnectionMode.Gateway,
154+
Serializer = this.cosmosSystemTextJsonSerializer,
155+
});
156+
157+
string uniqueDbName = "TestDb2_" + Guid.NewGuid().ToString();
158+
this.database = await this.client.CreateDatabaseIfNotExistsAsync(uniqueDbName);
159+
string uniqueContainerName = "TestContainer2_" + Guid.NewGuid().ToString();
160+
this.container = await this.database.CreateContainerIfNotExistsAsync(uniqueContainerName, "/pk");
161+
162+
string pk = "pk_create";
163+
IEnumerable<TestObject> items = this.GenerateItems(pk);
164+
165+
foreach (TestObject item in items)
166+
{
167+
ItemResponse<TestObject> response = await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
168+
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
169+
string diagnostics = response.Diagnostics.ToString();
170+
Assert.IsTrue(diagnostics.Contains("GatewayStoreModel"), "Diagnostics should contain 'GatewayStoreModel'");
171+
}
172+
}
173+
174+
[TestMethod]
175+
[TestCategory("ThinClient")]
176+
public async Task CreateItemsTestWithThinClientFlagDisabledAccountEnabled()
177+
{
178+
Environment.SetEnvironmentVariable(ConfigurationManager.ThinClientModeEnabled, "False");
179+
this.connectionString = Environment.GetEnvironmentVariable("COSMOSDB_THINCLIENT");
180+
181+
if (string.IsNullOrEmpty(this.connectionString))
182+
{
183+
Assert.Fail("Set environment variable COSMOSDB_THINCLIENT to run the tests");
184+
}
185+
186+
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
187+
{
188+
PropertyNamingPolicy = null,
189+
PropertyNameCaseInsensitive = true,
190+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
191+
};
192+
this.cosmosSystemTextJsonSerializer = new MultiRegionSetupHelpers.CosmosSystemTextJsonSerializer(jsonSerializerOptions);
193+
194+
this.client = new CosmosClient(
195+
this.connectionString,
196+
new CosmosClientOptions()
197+
{
198+
ConnectionMode = ConnectionMode.Gateway,
199+
Serializer = this.cosmosSystemTextJsonSerializer,
200+
});
201+
202+
string uniqueDbName = "TestDbTCDisabled_" + Guid.NewGuid().ToString();
203+
this.database = await this.client.CreateDatabaseIfNotExistsAsync(uniqueDbName);
204+
string uniqueContainerName = "TestContainerTCDisabled_" + Guid.NewGuid().ToString();
205+
this.container = await this.database.CreateContainerIfNotExistsAsync(uniqueContainerName, "/pk");
206+
207+
string pk = "pk_create";
208+
IEnumerable<TestObject> items = this.GenerateItems(pk);
209+
210+
foreach (TestObject item in items)
211+
{
212+
ItemResponse<TestObject> response = await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
213+
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
214+
string diagnostics = response.Diagnostics.ToString();
215+
Assert.IsTrue(diagnostics.Contains("GatewayStoreModel"), "Diagnostics should contain 'GatewayStoreModel'");
124216
}
125217
}
126218

0 commit comments

Comments
 (0)