|
1 |
| -use futures::Future; |
| 1 | +use futures::{Future, IntoFuture}; |
2 | 2 | use futures_state_stream::StateStream;
|
3 | 3 | use std::error::Error as StdError;
|
4 |
| -use tokio_core::reactor::Core; |
| 4 | +use std::time::Duration; |
| 5 | +use tokio_core::reactor::{Core, Interval}; |
5 | 6 |
|
6 | 7 | use super::*;
|
7 | 8 | use error::{Error, ConnectError, SqlState};
|
@@ -331,3 +332,36 @@ fn enum_() {
|
331 | 332 |
|
332 | 333 | l.run(done).unwrap();
|
333 | 334 | }
|
| 335 | + |
| 336 | +#[test] |
| 337 | +fn cancel() { |
| 338 | + let mut l = Core::new().unwrap(); |
| 339 | + let handle = l.handle(); |
| 340 | + |
| 341 | + let done = Connection::connect("postgres://postgres@localhost", TlsMode::None, &handle) |
| 342 | + .then(move |c| { |
| 343 | + let c = c.unwrap(); |
| 344 | + let cancel_data = c.cancel_data(); |
| 345 | + let cancel = Interval::new(Duration::from_secs(1), &handle) |
| 346 | + .into_future() |
| 347 | + .then(move |r| { |
| 348 | + r.unwrap(); |
| 349 | + cancel_query("postgres://postgres@localhost", |
| 350 | + TlsMode::None, |
| 351 | + cancel_data, |
| 352 | + &handle) |
| 353 | + }) |
| 354 | + .then(Ok::<_, ()>); |
| 355 | + c.batch_execute("SELECT pg_sleep(10)") |
| 356 | + .then(Ok::<_, ()>) |
| 357 | + .join(cancel) |
| 358 | + }); |
| 359 | + |
| 360 | + let (select, cancel) = l.run(done).unwrap(); |
| 361 | + cancel.unwrap(); |
| 362 | + match select { |
| 363 | + Err(Error::Db(e, _)) => assert_eq!(e.code, SqlState::QueryCanceled), |
| 364 | + Err(e) => panic!("unexpected error {}", e), |
| 365 | + Ok(_) => panic!("unexpected success"), |
| 366 | + } |
| 367 | +} |
0 commit comments