Skip to content

Commit dc75c40

Browse files
authored
Tests | Account for MAX_DISPATCH_LATENCY in XEvents tests (#3456)
* Account for MAX_DISPATCH_LATENCY in XEvents tests XEvents are asynchronous, and are written to the target within MAX_DISPATCH_LATENCY seconds. We therefore need to wait that long before querying the target. * Filter XEvent session by client_connection_id
1 parent 257e326 commit dc75c40

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,8 @@ protected virtual void OnMatchingEventWritten(EventWrittenEventArgs eventData)
10921092

10931093
public readonly ref struct XEventScope // : IDisposable
10941094
{
1095+
private const int MaxXEventsLatencyS = 5;
1096+
10951097
private readonly SqlConnection _connection;
10961098
private readonly bool _useDatabaseSession;
10971099

@@ -1126,6 +1128,8 @@ INNER JOIN sys.dm_xe_sessions AS xe
11261128

11271129
using (SqlCommand command = new SqlCommand(xEventQuery, _connection))
11281130
{
1131+
Thread.Sleep(MaxXEventsLatencyS * 1000);
1132+
11291133
if (_connection.State == ConnectionState.Closed)
11301134
{
11311135
_connection.Open();
@@ -1146,9 +1150,9 @@ private void SetupXEvent(string eventSpecification, string targetSpecification)
11461150
{eventSpecification}
11471151
{targetSpecification}
11481152
WITH (
1149-
MAX_MEMORY=4096 KB,
1153+
MAX_MEMORY=16 MB,
11501154
EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,
1151-
MAX_DISPATCH_LATENCY=30 SECONDS,
1155+
MAX_DISPATCH_LATENCY={MaxXEventsLatencyS} SECONDS,
11521156
MAX_EVENT_SIZE=0 KB,
11531157
MEMORY_PARTITION_MODE=NONE,
11541158
TRACK_CAUSALITY=ON,

src/Microsoft.Data.SqlClient/tests/ManualTests/TracingTests/XEventsTracingTest.cs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,35 @@ public void XEventActivityIDConsistentWithTracing(string query, System.Data.Comm
2121
// where it can be recorded in an XEvent session. This is documented at:
2222
// https://learn.microsoft.com/en-us/sql/relational-databases/native-client/features/accessing-diagnostic-information-in-the-extended-events-log
2323

24-
using (SqlConnection xEventManagementConnection = new SqlConnection(DataTestUtility.TCPConnectionString))
25-
using (DataTestUtility.XEventScope xEventSession = new DataTestUtility.XEventScope(xEventManagementConnection,
26-
@"ADD EVENT SQL_STATEMENT_STARTING (ACTION (client_connection_id)),
27-
ADD EVENT RPC_STARTING (ACTION (client_connection_id))",
28-
"ADD TARGET ring_buffer"))
29-
{
30-
Guid connectionId;
31-
HashSet<string> ids;
24+
using SqlConnection activityConnection = new(DataTestUtility.TCPConnectionString);
25+
activityConnection.Open();
3226

33-
using (DataTestUtility.MDSEventListener TraceListener = new())
34-
using (SqlConnection connection = new(DataTestUtility.TCPConnectionString))
35-
{
36-
connection.Open();
37-
connectionId = connection.ClientConnectionId;
27+
Guid connectionId = activityConnection.ClientConnectionId;
28+
HashSet<string> ids;
3829

39-
using SqlCommand command = new(query, connection) { CommandType = commandType };
40-
using SqlDataReader reader = command.ExecuteReader();
41-
while (reader.Read())
42-
{
43-
// Flush data
44-
}
30+
using SqlConnection xEventManagementConnection = new(DataTestUtility.TCPConnectionString);
31+
using DataTestUtility.XEventScope xEventSession = new(xEventManagementConnection,
32+
$@"ADD EVENT SQL_STATEMENT_STARTING (ACTION (client_connection_id) WHERE (client_connection_id='{connectionId}')),
33+
ADD EVENT RPC_STARTING (ACTION (client_connection_id) WHERE (client_connection_id='{connectionId}'))",
34+
"ADD TARGET ring_buffer");
4535

46-
ids = TraceListener.ActivityIDs;
36+
using (DataTestUtility.MDSEventListener TraceListener = new())
37+
{
38+
using SqlCommand command = new(query, activityConnection) { CommandType = commandType };
39+
using SqlDataReader reader = command.ExecuteReader();
40+
while (reader.Read())
41+
{
42+
// Flush data
4743
}
4844

49-
XmlDocument eventList = xEventSession.GetEvents();
50-
// Get the associated activity ID from the XEvent session. We expect to see the same ID in the trace as well.
51-
string activityId = GetCommandActivityId(query, xEvent, connectionId, eventList);
52-
53-
Assert.Contains(activityId, ids);
45+
ids = TraceListener.ActivityIDs;
5446
}
47+
48+
XmlDocument eventList = xEventSession.GetEvents();
49+
// Get the associated activity ID from the XEvent session. We expect to see the same ID in the trace as well.
50+
string activityId = GetCommandActivityId(query, xEvent, connectionId, eventList);
51+
52+
Assert.Contains(activityId, ids);
5553
}
5654

5755
private static string GetCommandActivityId(string commandText, string eventName, Guid connectionId, XmlDocument xEvents)

0 commit comments

Comments
 (0)