chore(relay): fix flaky proptest (#4157)

This turned out to be a user error in how I was using proptest.

Related: https://github.com/proptest-rs/proptest/issues/72.
Resolves: #3965.
This commit is contained in:
Thomas Eizinger
2024-03-16 12:09:39 +11:00
committed by GitHub
parent 194e8ae579
commit de6bbbc10d
2 changed files with 13 additions and 9 deletions

View File

@@ -1,6 +1,5 @@
use crate::Binding;
use proptest::arbitrary::any;
use proptest::collection::vec;
use proptest::strategy::Just;
use proptest::strategy::Strategy;
use proptest::string::string_regex;
@@ -30,11 +29,16 @@ pub fn channel_number() -> impl Strategy<Value = ChannelNumber> {
(ChannelNumber::MIN..ChannelNumber::MAX).prop_map(|n| ChannelNumber::new(n).unwrap())
}
pub fn channel_payload() -> impl Strategy<Value = (Vec<u8>, usize)> {
vec(any::<u8>(), 0..(u16::MAX as usize)).prop_flat_map(|vec| {
let len = vec.len();
(Just(vec), 0..len)
})
pub fn channel_payload() -> impl Strategy<Value = (Vec<u8>, u16)> {
any::<Vec<u8>>()
.prop_filter("payload does not fit into u16", |vec| {
vec.len() <= u16::MAX as usize
})
.prop_map(|vec| {
let len = vec.len();
(vec, len as u16)
})
}
pub fn username_salt() -> impl Strategy<Value = String> {

View File

@@ -97,12 +97,12 @@ mod tests {
#[test_strategy::proptest]
fn channel_data_decoding(
#[strategy(crate::proptest::channel_number())] channel: ChannelNumber,
#[strategy(crate::proptest::channel_payload())] payload: (Vec<u8>, usize),
#[strategy(crate::proptest::channel_payload())] payload: (Vec<u8>, u16),
) {
let encoded = to_bytes(channel.value(), payload.1 as u16, &payload.0);
let encoded = to_bytes(channel.value(), payload.1, &payload.0);
let parsed = ChannelData::parse(&encoded).unwrap();
assert_eq!(channel.value(), parsed.channel);
assert_eq!(&payload.0[..payload.1], parsed.data)
assert_eq!(&payload.0[..(payload.1 as usize)], parsed.data)
}
}