Skip to content

Commit e129555

Browse files
committed
Task 42205: Cleanup orphaned XEvents sessions
- Using an xUnit Collection Fixture to run the cleanup.
1 parent 30e50cf commit e129555

1 file changed

Lines changed: 46 additions & 31 deletions

File tree

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

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,59 @@
1313

1414
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
1515
{
16-
public class XEventsTracingTest
16+
// XEvent sessions may become orphaned on the Azure SQL Server, which leads to poor performance
17+
// (query timeouts, deadlocks, etc) over time. This fixture is instantiated once per test run
18+
// and drops these orphaned sessions as part of every run to help mitigate this issue.
19+
public class XEventCleaner
1720
{
18-
private readonly string _testName;
19-
20-
public XEventsTracingTest(ITestOutputHelper outputHelper)
21+
public XEventCleaner()
2122
{
22-
_testName = DataTestUtility.CurrentTestName(outputHelper);
23-
}
23+
if (DataTestUtility.AreConnStringsSetup() &&
24+
DataTestUtility.IsNotAzureSynapse() &&
25+
DataTestUtility.IsNotManagedInstance())
26+
{
27+
using SqlConnection connection = new(DataTestUtility.TCPConnectionString);
28+
connection.Open();
2429

25-
// XEvent sessions may become orphaned on the Azure SQL Server, which leads to poor
26-
// performance (query timeouts, deadlocks, etc) over time. This test drops these orphaned
27-
// sessions as part of every run to help mitigate this issue.
28-
[ConditionalFact(
29-
typeof(DataTestUtility),
30-
nameof(DataTestUtility.AreConnStringsSetup),
31-
nameof(DataTestUtility.IsNotAzureSynapse),
32-
nameof(DataTestUtility.IsNotManagedInstance))]
33-
public void CleanupOrphanedXEventSessions()
34-
{
35-
using SqlConnection connection = new(DataTestUtility.TCPConnectionString);
36-
connection.Open();
30+
using SqlCommand command = new(
31+
"""
32+
DECLARE @sql NVARCHAR(MAX) = N'';
33+
34+
-- Identify orphaned event sessions and generate DROP commands.
35+
SELECT @sql += N'DROP EVENT SESSION [' + s.name + N'] ON DATABASE;' + CHAR(13) + CHAR(10)
36+
FROM sys.database_event_sessions s
37+
LEFT JOIN sys.dm_xe_database_sessions t ON s.name = t.name
38+
WHERE t.name IS NULL;
3739
38-
using SqlCommand command = new(
39-
"""
40-
DECLARE @sql NVARCHAR(MAX) = N'';
40+
-- Execute the generated commands
41+
EXEC sys.sp_executesql @sql;
42+
""",
43+
connection);
44+
45+
int rowsAffected = command.ExecuteNonQuery();
46+
47+
if (rowsAffected > 0)
48+
{
49+
Console.WriteLine($"Dropped {rowsAffected} orphaned XEvent sessions.");
50+
}
51+
}
52+
}
53+
}
4154

42-
-- Identify orphaned event sessions and generate DROP commands.
43-
SELECT @sql += N'DROP EVENT SESSION [' + s.name + N'] ON DATABASE;' + CHAR(13) + CHAR(10)
44-
FROM sys.database_event_sessions s
45-
LEFT JOIN sys.dm_xe_database_sessions t ON s.name = t.name
46-
WHERE t.name IS NULL;
55+
// This empty class is required by xUnit to tie the cleaner to the test classes.
56+
[CollectionDefinition("XEventCleaner")]
57+
public class XEventCleanerCollection : ICollectionFixture<XEventCleaner>
58+
{
59+
}
4760

48-
-- Execute the generated commands
49-
EXEC sys.sp_executesql @sql;
50-
""",
51-
connection);
61+
[Collection("XEventCleaner")]
62+
public class XEventsTracingTest
63+
{
64+
private readonly string _testName;
5265

53-
command.ExecuteNonQuery();
66+
public XEventsTracingTest(ITestOutputHelper outputHelper)
67+
{
68+
_testName = DataTestUtility.CurrentTestName(outputHelper);
5469
}
5570

5671
[Trait("Category", "flaky")]

0 commit comments

Comments
 (0)