Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Flaky Test: Fixes Client Telemetry Flaky tests #4760

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
using System.Linq;
using Cosmos.Util;
using Microsoft.Azure.Cosmos.Telemetry.Models;
using System.Threading;

public abstract class ClientTelemetryTestsBase : BaseCosmosClientHelper
{
Expand All @@ -48,6 +49,8 @@ public abstract class ClientTelemetryTestsBase : BaseCosmosClientHelper
protected HttpClientHandlerHelper httpHandler;
protected HttpClientHandlerHelper httpHandlerForNonAzureInstance;

protected ManualResetEventSlim eventSlim;

private bool isClientTelemetryAPICallFailed = false;

public static void ClassInitialize(TestContext _)
Expand All @@ -63,6 +66,8 @@ public static void ClassCleanup()

public virtual void TestInitialize()
{
this.eventSlim = new ManualResetEventSlim(false);

this.actualInfo = new List<ClientTelemetryProperties>();

this.httpHandler = new HttpClientHandlerHelper
Expand All @@ -80,11 +85,13 @@ public virtual void TestInitialize()
}
return this.HttpHandlerRequestCallbackChecks(request);
},
ResponseIntercepter = (response) =>
ResponseIntercepter = (response, request) =>
{
if (response.RequestMessage != null && response.RequestMessage.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);

this.eventSlim.Set();
}

return Task.FromResult(response);
Expand All @@ -94,6 +101,8 @@ public virtual void TestInitialize()
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
this.isClientTelemetryAPICallFailed = true;

this.eventSlim.Set();
}
}
};
Expand All @@ -120,11 +129,13 @@ public virtual void TestInitialize()

return this.HttpHandlerRequestCallbackChecks(request);
},
ResponseIntercepter = (response) =>
ResponseIntercepter = (response, request) =>
{
if (response.RequestMessage != null && response.RequestMessage.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);

this.eventSlim.Set();
}
return Task.FromResult(response);
},
Expand All @@ -133,6 +144,8 @@ public virtual void TestInitialize()
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
this.isClientTelemetryAPICallFailed = true;

this.eventSlim.Set();
}
}
};
Expand Down Expand Up @@ -164,6 +177,8 @@ public virtual async Task Cleanup()
await base.TestCleanup();

Assert.IsFalse(this.isClientTelemetryAPICallFailed, $"Call to client telemetry service endpoint (i.e. {telemetryServiceEndpoint}) failed");

this.eventSlim.Reset();
}

public virtual async Task PointSuccessOperationsTest(ConnectionMode mode, bool isAzureInstance)
Expand Down Expand Up @@ -209,7 +224,7 @@ public virtual async Task PointSuccessOperationsTest(ConnectionMode mode, bool i
{ Documents.OperationType.Delete.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 12,
this.WaitAndAssert(expectedOperationCount: 12,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
isAzureInstance: isAzureInstance);
}
Expand Down Expand Up @@ -240,7 +255,7 @@ await container.ReadItemAsync<JObject>(
{ Documents.OperationType.Read.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.Eventual,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedCacheSource: null,
Expand Down Expand Up @@ -273,7 +288,7 @@ await container.ReadItemStreamAsync(
{ Documents.OperationType.Read.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.ConsistentPrefix,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedCacheSource: null,
Expand Down Expand Up @@ -322,7 +337,7 @@ await container
{ Documents.OperationType.Delete.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 12,
this.WaitAndAssert(expectedOperationCount: 12,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedCacheSource: null);
}
Expand Down Expand Up @@ -352,7 +367,7 @@ public virtual async Task BatchOperationsTest(ConnectionMode mode)
{ Documents.OperationType.Batch.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.Eventual,
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}
Expand All @@ -378,7 +393,7 @@ public virtual async Task SingleOperationMultipleTimesTest(ConnectionMode mode)
{ Documents.OperationType.Create.ToString(), 1}
};

await this.WaitAndAssert(
this.WaitAndAssert(
expectedOperationCount: 4,// 2 (read, requestLatency + requestCharge) + 2 (create, requestLatency + requestCharge)
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}
Expand Down Expand Up @@ -433,7 +448,7 @@ public virtual async Task QueryOperationSinglePartitionTest(ConnectionMode mode)
{ Documents.OperationType.Create.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 4,
this.WaitAndAssert(expectedOperationCount: 4,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.ConsistentPrefix);
}
Expand Down Expand Up @@ -492,7 +507,7 @@ public virtual async Task QueryMultiPageSinglePartitionOperationTest(ConnectionM
{ Documents.OperationType.Create.ToString(), 2}
};

await this.WaitAndAssert(
this.WaitAndAssert(
expectedOperationCount: 4,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.ConsistentPrefix);
Expand Down Expand Up @@ -540,7 +555,7 @@ await ToDoActivity.CreateRandomItems(
{ Documents.OperationType.Create.ToString(), 10}
};

await this.WaitAndAssert(
this.WaitAndAssert(
expectedOperationCount: 4,
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}
Expand Down Expand Up @@ -591,7 +606,7 @@ await ToDoActivity.CreateRandomItems(
{ Documents.OperationType.Create.ToString(), 10}
};

await this.WaitAndAssert(
this.WaitAndAssert(
expectedOperationCount: 4,
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}
Expand Down Expand Up @@ -629,7 +644,7 @@ public virtual async Task QueryOperationInvalidContinuationTokenTest(ConnectionM
{ Documents.OperationType.Create.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}

Expand Down Expand Up @@ -665,6 +680,16 @@ public virtual async Task CreateItemWithSubStatusCodeTest(ConnectionMode mode)
return this.HttpHandlerRequestCallbackChecks(request);
};

httpHandler.ResponseIntercepter = (response, request) =>
{
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
this.eventSlim.Set();
}

return Task.FromResult(response);
};

// Replacing originally initialized cosmos Builder with this one with new handler
this.cosmosClientBuilder = this.cosmosClientBuilder
.WithClientTelemetryOptions(new CosmosClientTelemetryOptions()
Expand Down Expand Up @@ -692,7 +717,7 @@ public virtual async Task CreateItemWithSubStatusCodeTest(ConnectionMode mode)
{ Documents.OperationType.Create.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedSubstatuscode: 999999,
isExpectedNetworkTelemetry: false);
Expand All @@ -706,7 +731,7 @@ await this.WaitAndAssert(expectedOperationCount: 2,
/// <param name="expectedConsistencyLevel"> Expected Consistency level of the operation recorded by telemetry</param>
/// <param name="expectedOperationRecordCountMap"> Expected number of requests recorded for each operation </param>
/// <returns></returns>
private async Task WaitAndAssert(
private void WaitAndAssert(
int expectedOperationCount = 0,
Microsoft.Azure.Cosmos.ConsistencyLevel? expectedConsistencyLevel = null,
IDictionary<string, long> expectedOperationRecordCountMap = null,
Expand All @@ -724,7 +749,7 @@ private async Task WaitAndAssert(
HashSet<CacheRefreshInfo> cacheRefreshInfoSet = new HashSet<CacheRefreshInfo>();
do
{
await Task.Delay(TimeSpan.FromMilliseconds(1500)); // wait at least for 1 round of telemetry
this.eventSlim.Wait();

HashSet<OperationInfo> actualOperationSet = new HashSet<OperationInfo>();
HashSet<RequestInfo> requestInfoSet = new HashSet<RequestInfo>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private async Task<Container> CreateContainer(bool isLocalQuorumConsistency)
{
HttpClientHandlerHelper httpHandler = new HttpClientHandlerHelper
{
ResponseIntercepter = async (response) =>
ResponseIntercepter = async (response, _) =>
{
string responseString = await response.Content.ReadAsStringAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public HttpClientHandlerHelper() : base(new HttpClientHandler())

public Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> RequestCallBack { get; set; }

public Func<HttpResponseMessage, Task<HttpResponseMessage>> ResponseIntercepter { get; set; }
public Func<HttpResponseMessage, HttpRequestMessage, Task<HttpResponseMessage>> ResponseIntercepter { get; set; }

public Action<HttpRequestMessage, Exception> ExceptionIntercepter { get; set; }

Expand All @@ -34,7 +34,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
if (this.ResponseIntercepter != null)
{
httpResponse = await this.ResponseIntercepter(httpResponse);
httpResponse = await this.ResponseIntercepter(httpResponse, request);
}
return httpResponse;
}
Expand All @@ -58,7 +58,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

if (this.ResponseIntercepter != null)
{
httpResponse = await this.ResponseIntercepter(httpResponse);
httpResponse = await this.ResponseIntercepter(httpResponse, request);
}

return httpResponse;
Expand Down
Loading