@@ -170,14 +170,13 @@ async fn postgres_cancel_token() {
170
170
#[ cfg( feature = "mysql" ) ]
171
171
#[ tokio:: test]
172
172
async fn mysql_cancel_token ( ) {
173
- use std:: time:: Duration ;
174
-
175
173
use diesel:: result:: { DatabaseErrorKind , Error } ;
174
+ use std:: time:: Duration ;
176
175
177
176
let ( sender, receiver) = tokio:: sync:: oneshot:: channel ( ) ;
178
177
179
- // execute a long-running query on a separate thread
180
- let task = tokio :: spawn ( async move {
178
+ // execute a long-running query in a separate future
179
+ let query_future = async move {
181
180
let conn = & mut connection ( ) . await ;
182
181
let token = conn. cancel_token ( ) ;
183
182
@@ -187,28 +186,30 @@ async fn mysql_cancel_token() {
187
186
. unwrap_or_else ( |_| panic ! ( "couldn't send token" ) ) ;
188
187
189
188
diesel:: dsl:: sql :: < diesel:: sql_types:: Integer > ( "SELECT SLEEP(5)" )
190
- . load :: < i32 > ( conn)
189
+ . get_result :: < i32 > ( conn)
191
190
. await
192
- } ) ;
193
-
194
- // wait for the cancellation token to be sent
195
- if let Ok ( token) = receiver. await {
196
- // give the query time to start before invoking the token
197
- tokio:: time:: sleep ( Duration :: from_millis ( 500 ) ) . await ;
198
- token. cancel_query ( ) . await . unwrap ( ) ;
199
- }
191
+ } ;
192
+ let cancel_future = async move {
193
+ // wait for the cancellation token to be sent
194
+ if let Ok ( token) = receiver. await {
195
+ // give the query time to start before invoking the token
196
+ tokio:: time:: sleep ( Duration :: from_millis ( 500 ) ) . await ;
197
+ token. cancel_query ( ) . await . unwrap ( ) ;
198
+ } else {
199
+ panic ! ( "Failed to receive cancel token" ) ;
200
+ }
201
+ } ;
202
+
203
+ let ( task, _) = tokio:: join!( query_future, cancel_future) ;
200
204
201
205
// make sure the query task resulted in a cancellation error or a return value of 1:
202
- match task. await . unwrap ( ) {
203
- Err ( e) => match e {
204
- Error :: DatabaseError ( DatabaseErrorKind :: Unknown , v)
205
- if v. message ( ) == "Query execution was interrupted" => { }
206
- _ => panic ! ( "unexpected error: {:?}" , e) ,
207
- } ,
208
- Ok ( r) => match r[ 0 ] {
209
- 1 => { }
210
- _ => panic ! ( "query completed successfully without cancellation" ) ,
211
- } ,
206
+ match task {
207
+ Err ( Error :: DatabaseError ( DatabaseErrorKind :: Unknown , v) )
208
+ if v. message ( ) == "Query execution was interrupted" => { }
209
+ Err ( e) => panic ! ( "unexpected error: {:?}" , e) ,
210
+ // mysql 8.4 returns 1 from a canceled sleep instead of an error
211
+ Ok ( 1 ) => { }
212
+ Ok ( _) => panic ! ( "query completed successfully without cancellation" ) ,
212
213
}
213
214
}
214
215
0 commit comments