Fix some flaky offchain HTTP tests (#6038)

* http: Use assert_eq in tests for better debuggability

* http: Use matches! macro instead of if let ...

* http: Simplify some bits and pieces

* http: Don't answer immediately in HTTP test server

Sometimes it can happen that we receive the response immediately
when testing the HTTP api due to kernel scheduling. Because of it, we
add a marginal 10ms async-friendly delay to minimize the risk.

* http: Use the same async runtime when testing HTTP API/worker

* http: Return a future Delay only for non-zero Duration

This allows to short-circuit in the response_wait logic and only
send/not wait for response.
This commit is contained in:
Igor Matuszewski
2020-05-22 13:14:26 +02:00
committed by GitHub
parent c6e23b615f
commit 66fe846d48
2 changed files with 31 additions and 34 deletions
@@ -51,12 +51,14 @@ pub fn timestamp_from_now(timestamp: Timestamp) -> Duration {
pub fn deadline_to_future(
deadline: Option<Timestamp>,
) -> futures::future::MaybeDone<impl futures::Future> {
use futures::future;
use futures::future::{self, Either};
future::maybe_done(match deadline {
Some(deadline) => future::Either::Left(
futures_timer::Delay::new(timestamp_from_now(deadline))
),
None => future::Either::Right(future::pending())
future::maybe_done(match deadline.map(timestamp_from_now) {
None => Either::Left(future::pending()),
// Only apply delay if we need to wait a non-zero duration
Some(duration) if duration <= Duration::from_secs(0) =>
Either::Right(Either::Left(future::ready(()))),
Some(duration) =>
Either::Right(Either::Right(futures_timer::Delay::new(duration))),
})
}