Skip to content

Commit 5a9a929

Browse files
Tests: Adds request URI diagnostics to InitTaskThreadSafe
Captures the absolute URI of every non-metadata HTTP request observed by the mock handler and surfaces the list in the assertion message when httpCallCount is unexpected. Addresses Fabian's review feedback: lets us distinguish an AsyncCache race (duplicate I/O for the same URI) from two different HTTP requests legitimately issued by the client (e.g. an extra collection cache resolve). The previous assertion only exposed a count, making it hard to diagnose. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4c6d266 commit 5a9a929

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

  • Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public async Task InitTaskThreadSafe()
8484
{
8585
int httpCallCount = 0;
8686
int metadataCallCount = 0;
87+
ConcurrentBag<string> httpRequestUris = new ConcurrentBag<string>();
8788
bool delayCallBack = true;
8889

8990
var isInitializedField = typeof(VmMetadataApiHandler).GetField("isInitialized",
@@ -100,15 +101,17 @@ public async Task InitTaskThreadSafe()
100101
{
101102
RequestCallBack = async (request, cancellToken) =>
102103
{
103-
if(request.RequestUri.AbsoluteUri == VmMetadataApiHandler.vmMetadataEndpointUrl.AbsoluteUri)
104+
string requestUri = request.RequestUri?.AbsoluteUri;
105+
if (requestUri == VmMetadataApiHandler.vmMetadataEndpointUrl.AbsoluteUri)
104106
{
105107
Interlocked.Increment(ref metadataCallCount);
106-
}
108+
}
107109
else
108110
{
109111
Interlocked.Increment(ref httpCallCount);
112+
httpRequestUris.Add(requestUri ?? "<null>");
110113
}
111-
114+
112115
while (delayCallBack)
113116
{
114117
await Task.Delay(TimeSpan.FromMilliseconds(100));
@@ -149,13 +152,19 @@ public async Task InitTaskThreadSafe()
149152
await Task.WhenAll(tasks);
150153

151154
Assert.AreEqual(1, metadataCallCount, "Only one call for VM Metadata call with be made");
152-
Assert.AreEqual(1, httpCallCount, "Only the first task should do the http call. All other should wait on the first task");
155+
Assert.AreEqual(
156+
1,
157+
httpCallCount,
158+
$"Only the first task should do the http call. All other should wait on the first task. Observed URIs: [{string.Join(", ", httpRequestUris)}]");
153159

154160
// Reset counters and retry the client to verify a new http call is done for new requests
155161
tasks.Clear();
156162
delayCallBack = true;
157163
this.TaskStartedCount = 0;
158164
httpCallCount = 0;
165+
while (httpRequestUris.TryTake(out _))
166+
{
167+
}
159168
}
160169
}
161170

0 commit comments

Comments
 (0)