Add some simple proptests using the Arbitray trait on Requests and Responses, gated to test only
This commit is contained in:
parent
6168cb51d7
commit
dd042cf4d8
|
@ -17,6 +17,8 @@ hex = "0.4"
|
||||||
# which we don't use, so disable it to drop the dependencies.
|
# which we don't use, so disable it to drop the dependencies.
|
||||||
indexmap = { version = "1.2", default-features = false }
|
indexmap = { version = "1.2", default-features = false }
|
||||||
pin-project = "0.4"
|
pin-project = "0.4"
|
||||||
|
proptest = "0.9"
|
||||||
|
proptest-derive = "0.1.0"
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
serde = { version = "1", features = ["serde_derive"] }
|
serde = { version = "1", features = ["serde_derive"] }
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Seeds for failure cases proptest has generated in the past. It is
|
||||||
|
# automatically read and these particular cases re-run before any
|
||||||
|
# novel cases are generated.
|
||||||
|
#
|
||||||
|
# It is recommended to check this file in to source control so that
|
||||||
|
# everyone who runs the test benefits from these saved cases.
|
||||||
|
cc fb728d0d2a16976de785ed8525cc74c4d3f7826f8840ebe5a4fee6b5ab0edad8 # shrinks to req = GetMempool
|
||||||
|
cc 3ed95f3a436a15e33b27f4bf1105629860b8e6a895165eb83befcb625e951b1d # shrinks to req = GetPeers
|
|
@ -6,6 +6,9 @@
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use proptest_derive::Arbitrary;
|
||||||
|
|
||||||
use zebra_chain::transaction::Transaction;
|
use zebra_chain::transaction::Transaction;
|
||||||
|
|
||||||
use crate::meta_addr::MetaAddr;
|
use crate::meta_addr::MetaAddr;
|
||||||
|
@ -14,15 +17,18 @@ use super::types::Nonce;
|
||||||
|
|
||||||
/// A network request, represented in internal format.
|
/// A network request, represented in internal format.
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
#[cfg_attr(test, derive(Arbitrary))]
|
||||||
pub enum Request {
|
pub enum Request {
|
||||||
/// Requests additional peers from the server.
|
/// Requests additional peers from the server.
|
||||||
GetPeers,
|
GetPeers,
|
||||||
/// Advertises peers to the remote server.
|
/// Advertises peers to the remote server.
|
||||||
|
#[cfg_attr(test, proptest(skip))]
|
||||||
PushPeers(Vec<MetaAddr>),
|
PushPeers(Vec<MetaAddr>),
|
||||||
/// Heartbeats triggered on peer connection start.
|
/// Heartbeats triggered on peer connection start.
|
||||||
// This is included as a bit of a hack, it should only be used
|
// This is included as a bit of a hack, it should only be used
|
||||||
// internally for connection management. You should not expect to
|
// internally for connection management. You should not expect to
|
||||||
// be firing or handling `Ping` requests or `Pong` responses.
|
// be firing or handling `Ping` requests or `Pong` responses.
|
||||||
|
#[cfg_attr(test, proptest(skip))]
|
||||||
Ping(Nonce),
|
Ping(Nonce),
|
||||||
/// Requests the transactions the remote server has verified but
|
/// Requests the transactions the remote server has verified but
|
||||||
/// not yet confirmed.
|
/// not yet confirmed.
|
||||||
|
@ -31,14 +37,17 @@ pub enum Request {
|
||||||
|
|
||||||
/// A response to a network request, represented in internal format.
|
/// A response to a network request, represented in internal format.
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
#[cfg_attr(test, derive(Arbitrary))]
|
||||||
pub enum Response {
|
pub enum Response {
|
||||||
/// Generic success.
|
/// Generic success.
|
||||||
Ok,
|
Ok,
|
||||||
/// Generic error.
|
/// Generic error.
|
||||||
Error,
|
Error,
|
||||||
/// A list of peers, used to respond to `GetPeers`.
|
/// A list of peers, used to respond to `GetPeers`.
|
||||||
|
#[cfg_attr(test, proptest(skip))]
|
||||||
Peers(Vec<MetaAddr>),
|
Peers(Vec<MetaAddr>),
|
||||||
/// A list of transactions, such as in response to `GetMempool`.
|
/// A list of transactions, such as in response to `GetMempool`.
|
||||||
|
#[cfg_attr(test, proptest(skip))]
|
||||||
Transactions(Vec<Transaction>),
|
Transactions(Vec<Transaction>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,3 +59,23 @@ where
|
||||||
Self::Error
|
Self::Error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use proptest::prelude::*;
|
||||||
|
|
||||||
|
use super::{Request, Response};
|
||||||
|
|
||||||
|
proptest! {
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn proptest_request(req in any::<Request>()) {
|
||||||
|
println!("{:?}", req);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn proptest_response(res in any::<Response>()) {
|
||||||
|
println!("{:?}", res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue