Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Membership test #500

Merged
merged 11 commits into from Feb 7, 2022
28 changes: 14 additions & 14 deletions compatibility/README.md
Expand Up @@ -9,19 +9,19 @@ We have created a test project that can be used to test the compatibility betwee
## Adding a test case for your project

To add a test case for your project, please follow the following steps:
- [ ] Fork https://github.com/bincode-org/bincode
- [ ] create a new file `compatibility/src/<name>.rs`.
- [ ] Add a link to your project
- [ ] Add `Licence: MIT OR Apache-2.0` if you agree to distribute your code under this license
- [ ] Add a `mod <name>;` in the `lib.rs`. Make sure it's alphabetically ordered (check the ordering in your file system).
- [ ] Add your structs.
- [X] Fork https://github.com/bincode-org/bincode
VictorKoenders marked this conversation as resolved.
Show resolved Hide resolved
- [X] create a new file `compatibility/src/<name>.rs`.
- [X] Add a link to your project: https://github.com/ppamorim/openraft/tree/bincode-bug/example-raft-kv
- [X] Add `Licence: MIT OR Apache-2.0` if you agree to distribute your code under this license
- [X] Add a `mod <name>;` in the `lib.rs`. Make sure it's alphabetically ordered (check the ordering in your file system).
- [X] Add your structs.
- Adding references to libraries is not recommended. Libraries will not be implementing `bincode 2`'s encoding/decoding system.
- If you need references to libraries, consider adding a test case for that library, and then referencing that test.
- [ ] Make sure structs derive the following traits:
- [ ] `serde::Serialize` and `serde::Deserialize`, like normal
- [ ] `bincode_2::Encode` and `bincode_2::Decode`, for the bincode 2 encode/decode mechanic
- [ ] Because the crate is renamed to `bincode_2`, you also need to add `#[bincode(crate = "bincode_2")]`
- [ ] `Debug, PartialEq`
- [X] Make sure structs derive the following traits:
- [X] `serde::Serialize` and `serde::Deserialize`, like normal
- [X] `bincode_2::Encode` and `bincode_2::Decode`, for the bincode 2 encode/decode mechanic
- [X] Because the crate is renamed to `bincode_2`, you also need to add `#[bincode(crate = "bincode_2")]`
- [X] `Debug, PartialEq`

```rs
#[derive(Serialize, Deserialize, bincode_2::Encode, bincode_2::Decode, Debug, PartialEq)]
Expand All @@ -30,9 +30,9 @@ pub struct YourStruct {
}
```

- [ ] Use `rand` to be able to generate a random test case for your project.
- [ ] For strings there is a helper function in `crate`: `gen_string(rng: &mut impl Rng) -> String`
- [ ] Add the following code:
- [X] Use `rand` to be able to generate a random test case for your project.
- [X] For strings there is a helper function in `crate`: `gen_string(rng: &mut impl Rng) -> String`
- [X] Add the following code:

```rs
#[test]
Expand Down
1 change: 1 addition & 0 deletions compatibility/src/lib.rs
Expand Up @@ -5,6 +5,7 @@ use bincode_1::Options;

mod rand;
mod sway;
mod membership;

pub fn test_same_with_config<T, C, O>(t: &T, bincode_1_options: O, bincode_2_config: C)
where
Expand Down
48 changes: 48 additions & 0 deletions compatibility/src/membership.rs
@@ -0,0 +1,48 @@
use std::collections::BTreeSet;
VictorKoenders marked this conversation as resolved.
Show resolved Hide resolved
use rand::{prelude::ThreadRng, Rng};

type NodeId = u64;

#[derive(
bincode_2::Encode, bincode_2::Decode, serde::Serialize, serde::Deserialize, Debug, PartialEq,
)]
#[bincode(crate = "bincode_2")]
pub struct Membership {
/// learners set
learners: BTreeSet<NodeId>,

/// Multi configs.
configs: Vec<BTreeSet<NodeId>>,

/// Cache of all node ids.
all_members: BTreeSet<NodeId>,
}

#[test]
pub fn test() {
let mut rng = rand::thread_rng();
for _ in 0..1000 {
crate::test_same(Membership {
learners: random_btreeset(&mut rng, 1000),
configs: vec_random_btreeset(&mut rng,),
all_members: random_btreeset(&mut rng, 1000),
});
}
}

fn vec_random_btreeset(rng: &mut ThreadRng) -> Vec<BTreeSet<NodeId>> {
let mut vec = Vec::with_capacity(5);
for _ in 0..10 {
VictorKoenders marked this conversation as resolved.
Show resolved Hide resolved
vec.push(random_btreeset(rng, 100));
}
vec
}

fn random_btreeset(rng: &mut ThreadRng, count: u32) -> BTreeSet<NodeId> {
let mut set = BTreeSet::new();
for _ in 0..count {
VictorKoenders marked this conversation as resolved.
Show resolved Hide resolved
let v = rng.gen();
set.insert(v);
}
set
}