@@ -167,6 +167,51 @@ async fn postgres_cancel_token() {
167167 }
168168}
169169
170+ #[ cfg( feature = "mysql" ) ]
171+ #[ tokio:: test]
172+ async fn mysql_cancel_token ( ) {
173+ use std:: time:: Duration ;
174+
175+ use diesel:: result:: { DatabaseErrorKind , Error } ;
176+
177+ let ( sender, receiver) = tokio:: sync:: oneshot:: channel ( ) ;
178+
179+ // execute a long-running query on a separate thread
180+ let task = tokio:: spawn ( async move {
181+ let conn = & mut connection ( ) . await ;
182+ let token = conn. cancel_token ( ) ;
183+
184+ // send the token back to the main thread via a oneshot channel
185+ sender
186+ . send ( token)
187+ . unwrap_or_else ( |_| panic ! ( "couldn't send token" ) ) ;
188+
189+ diesel:: dsl:: sql :: < diesel:: sql_types:: Integer > ( "SELECT SLEEP(5)" )
190+ . load :: < i32 > ( conn)
191+ . 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+ }
200+
201+ // 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 ! ( "" ) ,
211+ } ,
212+ }
213+ }
214+
170215#[ cfg( feature = "postgres" ) ]
171216async fn setup ( connection : & mut TestConnection ) {
172217 diesel:: sql_query (
0 commit comments