|
13 | 13 |
|
14 | 14 | namespace Microsoft.Data.SqlClient.ManualTesting.Tests |
15 | 15 | { |
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 |
17 | 20 | { |
18 | | - private readonly string _testName; |
19 | | - |
20 | | - public XEventsTracingTest(ITestOutputHelper outputHelper) |
| 21 | + public XEventCleaner() |
21 | 22 | { |
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(); |
24 | 29 |
|
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; |
37 | 39 |
|
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 | + } |
41 | 54 |
|
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 | + } |
47 | 60 |
|
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; |
52 | 65 |
|
53 | | - command.ExecuteNonQuery(); |
| 66 | + public XEventsTracingTest(ITestOutputHelper outputHelper) |
| 67 | + { |
| 68 | + _testName = DataTestUtility.CurrentTestName(outputHelper); |
54 | 69 | } |
55 | 70 |
|
56 | 71 | [Trait("Category", "flaky")] |
|
0 commit comments