@@ -62,19 +62,10 @@ public void CommandTimeoutWithSleepSync()
6262 {
6363 cmd . CommandTimeout = 2 ;
6464 var sw = Stopwatch . StartNew ( ) ;
65- try
66- {
67- using var reader = cmd . ExecuteReader ( ) ;
68-
69- // shouldn't get here
70- Assert . True ( false ) ;
71- }
72- catch ( MySqlException ex )
73- {
74- sw . Stop ( ) ;
75- Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
76- TestUtilities . AssertDuration ( sw , cmd . CommandTimeout * 1000 - 100 , 500 ) ;
77- }
65+ var ex = Assert . Throws < MySqlException > ( cmd . ExecuteReader ) ;
66+ sw . Stop ( ) ;
67+ Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
68+ TestUtilities . AssertDuration ( sw , cmd . CommandTimeout * 1000 - 100 , 500 ) ;
7869 }
7970
8071 Assert . Equal ( ConnectionState . Closed , m_connection . State ) ;
@@ -87,19 +78,10 @@ public async Task CommandTimeoutWithSleepAsync()
8778 {
8879 cmd . CommandTimeout = 2 ;
8980 var sw = Stopwatch . StartNew ( ) ;
90- try
91- {
92- using var reader = await cmd . ExecuteReaderAsync ( ) ;
93-
94- // shouldn't get here
95- Assert . True ( false ) ;
96- }
97- catch ( MySqlException ex )
98- {
99- sw . Stop ( ) ;
100- Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
101- TestUtilities . AssertDuration ( sw , cmd . CommandTimeout * 1000 - 100 , 700 ) ;
102- }
81+ var exception = await Assert . ThrowsAsync < MySqlException > ( cmd . ExecuteReaderAsync ) ;
82+ sw . Stop ( ) ;
83+ Assert . Contains ( c_timeoutMessage , exception . Message , StringComparison . OrdinalIgnoreCase ) ;
84+ TestUtilities . AssertDuration ( sw , cmd . CommandTimeout * 1000 - 100 , 700 ) ;
10385 }
10486
10587 Assert . Equal ( ConnectionState . Closed , m_connection . State ) ;
@@ -129,19 +111,10 @@ create procedure sleep_sproc(IN seconds INT)
129111 cmd . CommandTimeout = 2 ;
130112
131113 var sw = Stopwatch . StartNew ( ) ;
132- try
133- {
134- using var reader = cmd . ExecuteReader ( ) ;
135-
136- // shouldn't get here
137- Assert . True ( false ) ;
138- }
139- catch ( MySqlException ex )
140- {
141- sw . Stop ( ) ;
142- Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
143- TestUtilities . AssertDuration ( sw , ( ( int ) cmd . CommandTimeout ) * 1000 - 100 , 500 ) ;
144- }
114+ var ex = Assert . Throws < MySqlException > ( cmd . ExecuteReader ) ;
115+ sw . Stop ( ) ;
116+ Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
117+ TestUtilities . AssertDuration ( sw , ( ( int ) cmd . CommandTimeout ) * 1000 - 100 , 500 ) ;
145118 }
146119
147120 [ SkippableFact ( ServerFeatures . Timeout , Baseline = "https://bugs.mysql.com/bug.php?id=87307" ) ]
@@ -270,19 +243,10 @@ public void TransactionCommandTimeoutWithSleepSync()
270243 {
271244 cmd . CommandTimeout = 2 ;
272245 var sw = Stopwatch . StartNew ( ) ;
273- try
274- {
275- using var reader = cmd . ExecuteReader ( ) ;
276-
277- // shouldn't get here
278- Assert . True ( false ) ;
279- }
280- catch ( MySqlException ex )
281- {
282- sw . Stop ( ) ;
283- Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
284- TestUtilities . AssertDuration ( sw , cmd . CommandTimeout * 1000 - 100 , 500 ) ;
285- }
246+ var ex = Assert . Throws < MySqlException > ( cmd . ExecuteReader ) ;
247+ sw . Stop ( ) ;
248+ Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
249+ TestUtilities . AssertDuration ( sw , cmd . CommandTimeout * 1000 - 100 , 500 ) ;
286250 }
287251
288252 Assert . Equal ( ConnectionState . Closed , m_connection . State ) ;
@@ -296,18 +260,9 @@ public async Task TransactionCommandTimeoutWithSleepAsync()
296260 {
297261 cmd . CommandTimeout = 2 ;
298262 var sw = Stopwatch . StartNew ( ) ;
299- try
300- {
301- using var reader = await cmd . ExecuteReaderAsync ( ) ;
302-
303- // shouldn't get here
304- Assert . True ( false ) ;
305- }
306- catch ( MySqlException ex )
307- {
308- sw . Stop ( ) ;
309- Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
310- }
263+ var ex = await Assert . ThrowsAsync < MySqlException > ( cmd . ExecuteReaderAsync ) ;
264+ sw . Stop ( ) ;
265+ Assert . Contains ( c_timeoutMessage , ex . Message , StringComparison . OrdinalIgnoreCase ) ;
311266 }
312267
313268 Assert . Equal ( ConnectionState . Closed , m_connection . State ) ;
0 commit comments