Replace block_on and expect_ready with FutureExt::now_or_never

Both futures::executor::block_on and our own expect_ready were being
used to resolve futures that were, in practice, known to be
non-blocking. FutureExt::now_or_never handles that case more lightly
than block_on and more uniformly than expect_ready.

This lets us drop the dependency on the full 'futures' crate down to
just futures_util, which should help with compile time.
This commit is contained in:
Jordan Rose
2021-07-02 17:55:30 -07:00
parent d923dd38c9
commit e7a0f4d424
20 changed files with 222 additions and 216 deletions

View File

@@ -5,7 +5,7 @@
mod support;
use futures::executor::block_on;
use futures_util::FutureExt;
use libsignal_protocol::*;
use rand::rngs::OsRng;
use std::convert::TryFrom;
@@ -14,7 +14,7 @@ use support::*;
#[test]
#[allow(clippy::eval_order_dependence)]
fn test_basic_prekey_v3() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -250,14 +250,16 @@ fn test_basic_prekey_v3() -> Result<(), SignalProtocolError> {
.is_err());
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
#[ignore = "slow to run locally"]
#[allow(clippy::eval_order_dependence)]
fn chain_jump_over_limit() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -337,14 +339,16 @@ fn chain_jump_over_limit() -> Result<(), SignalProtocolError> {
.await
.is_err());
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
#[ignore = "slow to run locally"]
#[allow(clippy::eval_order_dependence)]
fn chain_jump_over_limit_with_self() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let a1_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -432,13 +436,15 @@ fn chain_jump_over_limit_with_self() -> Result<(), SignalProtocolError> {
);
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
#[allow(clippy::eval_order_dependence)]
fn test_bad_signed_pre_key_signature() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let bob_address = ProtocolAddress::new("+14151111112".to_owned(), 1);
let mut alice_store = support::test_in_memory_protocol_store()?;
@@ -509,7 +515,9 @@ fn test_bad_signed_pre_key_signature() -> Result<(), SignalProtocolError> {
.await?;
Ok(())
})
}
.now_or_never()
.expect("sync")
}
// testRepeatBundleMessageV2 cannot be represented
@@ -517,7 +525,7 @@ fn test_bad_signed_pre_key_signature() -> Result<(), SignalProtocolError> {
#[test]
#[allow(clippy::eval_order_dependence)]
fn repeat_bundle_message_v3() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
let bob_address = ProtocolAddress::new("+14151111112".to_owned(), 1);
@@ -643,13 +651,15 @@ fn repeat_bundle_message_v3() -> Result<(), SignalProtocolError> {
);
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
#[allow(clippy::eval_order_dependence)]
fn bad_message_bundle() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -764,13 +774,15 @@ fn bad_message_bundle() -> Result<(), SignalProtocolError> {
));
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
#[allow(clippy::eval_order_dependence)]
fn optional_one_time_prekey() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
let bob_address = ProtocolAddress::new("+14151111112".to_owned(), 1);
@@ -852,7 +864,9 @@ fn optional_one_time_prekey() -> Result<(), SignalProtocolError> {
);
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
@@ -864,7 +878,7 @@ fn basic_session_v3() -> Result<(), SignalProtocolError> {
#[test]
fn message_key_limits() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let (alice_session_record, bob_session_record) = initialize_sessions_v3()?;
let alice_address = ProtocolAddress::new("+14159999999".to_owned(), 1);
@@ -916,7 +930,9 @@ fn message_key_limits() -> Result<(), SignalProtocolError> {
SignalProtocolError::DuplicatedMessage(2300, 5)
));
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[allow(clippy::needless_range_loop)]
@@ -924,7 +940,7 @@ fn run_session_interaction(
alice_session: SessionRecord,
bob_session: SessionRecord,
) -> Result<(), SignalProtocolError> {
block_on(async {
async {
use rand::seq::SliceRandom;
let alice_address = ProtocolAddress::new("+14159999999".to_owned(), 1);
@@ -1015,7 +1031,9 @@ fn run_session_interaction(
}
Ok(())
})
}
.now_or_never()
.expect("sync")
}
async fn run_interaction(
@@ -1128,7 +1146,7 @@ async fn is_session_id_equal(
#[test]
fn basic_simultaneous_initiate() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -1261,12 +1279,14 @@ fn basic_simultaneous_initiate() -> Result<(), SignalProtocolError> {
assert!(is_session_id_equal(&bob_store, &bob_address, &alice_store, &alice_address).await?);
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
fn simultaneous_initiate_with_lossage() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -1381,12 +1401,14 @@ fn simultaneous_initiate_with_lossage() -> Result<(), SignalProtocolError> {
assert!(is_session_id_equal(&bob_store, &bob_address, &alice_store, &alice_address).await?);
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
fn simultaneous_initiate_lost_message() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -1510,12 +1532,14 @@ fn simultaneous_initiate_lost_message() -> Result<(), SignalProtocolError> {
assert!(is_session_id_equal(&bob_store, &bob_address, &alice_store, &alice_address).await?);
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
fn simultaneous_initiate_repeated_messages() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -1710,12 +1734,14 @@ fn simultaneous_initiate_repeated_messages() -> Result<(), SignalProtocolError>
assert!(is_session_id_equal(&bob_store, &bob_address, &alice_store, &alice_address).await?);
Ok(())
})
}
.now_or_never()
.expect("sync")
}
#[test]
fn simultaneous_initiate_lost_message_repeated_messages() -> Result<(), SignalProtocolError> {
block_on(async {
async {
let mut csprng = OsRng;
let alice_address = ProtocolAddress::new("+14151111111".to_owned(), 1);
@@ -1958,5 +1984,7 @@ fn simultaneous_initiate_lost_message_repeated_messages() -> Result<(), SignalPr
assert!(is_session_id_equal(&bob_store, &bob_address, &alice_store, &alice_address).await?);
Ok(())
})
}
.now_or_never()
.expect("sync")
}