Skip to content

Commit

Permalink
fixup!: refactor conflict tests
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <[email protected]>
  • Loading branch information
bsbds committed Apr 26, 2024
1 parent 4de7af9 commit 0ab29e9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
14 changes: 14 additions & 0 deletions crates/curp/src/server/conflict/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ impl<C> PartialEq for CommandEntry<C> {

impl<C> Eq for CommandEntry<C> {}

impl<C> PartialOrd for CommandEntry<C> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<C> Ord for CommandEntry<C> {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.id.cmp(&other.id)
}
}

impl<C> From<CommandEntry<C>> for PoolEntry<C> {
fn from(entry: CommandEntry<C>) -> Self {
PoolEntry {
Expand Down
64 changes: 53 additions & 11 deletions crates/xline/src/conflict/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ fn kv_sp_operations_are_ok() {
assert!(sp.insert_if_not_conflict(entry2.clone()).is_none());
assert!(sp.insert_if_not_conflict(entry3.clone()).is_some());
assert!(sp.insert_if_not_conflict(entry4.clone()).is_none());
assert_eq!(sp.all().len(), 3);
compare_commands(
sp.all(),
vec![entry1.clone(), entry2.clone(), entry4.clone()],
);
assert_eq!(sp.len(), 3);
sp.remove(entry1.clone());
assert!(sp.insert_if_not_conflict(entry3.clone()).is_some());
Expand Down Expand Up @@ -56,10 +59,25 @@ fn kv_ucp_operations_are_ok() {
assert!(!ucp.insert(entry3.clone()));
assert!(ucp.insert(entry4.clone()));
assert!(ucp.insert(entry5.clone()));
assert_eq!(ucp.all().len(), 5);
compare_commands(
ucp.all(),
vec![
entry1.clone(),
entry2.clone(),
entry3.clone(),
entry4.clone(),
entry5.clone(),
],
);
assert_eq!(ucp.len(), 5);
assert_eq!(ucp.all_conflict(&entry1).len(), 3);
assert_eq!(ucp.all_conflict(&entry6).len(), 2);
compare_commands(
ucp.all_conflict(&entry1),
vec![entry1.clone(), entry2.clone(), entry4.clone()],
);
compare_commands(
ucp.all_conflict(&entry6),
vec![entry4.clone(), entry5.clone()],
);
ucp.remove(entry4.clone());
ucp.remove(entry5.clone());
assert!(!ucp.insert(entry6.clone()));
Expand All @@ -81,7 +99,7 @@ fn lease_sp_operations_are_ok() {
assert!(sp.insert_if_not_conflict(entry2.clone()).is_none());
assert!(sp.insert_if_not_conflict(entry3.clone()).is_some());
assert!(sp.insert_if_not_conflict(entry4.clone()).is_some());
assert_eq!(sp.all().len(), 2);
compare_commands(sp.all(), vec![entry1.clone(), entry2.clone()]);
assert_eq!(sp.len(), 2);
sp.remove(entry1);
sp.remove(entry2);
Expand All @@ -106,10 +124,25 @@ fn lease_ucp_operations_are_ok() {
assert!(!ucp.insert(entry3.clone()));
assert!(ucp.insert(entry4.clone()));
assert!(ucp.insert(entry5.clone()));
assert_eq!(ucp.all().len(), 5);
compare_commands(
ucp.all(),
vec![
entry1.clone(),
entry2.clone(),
entry3.clone(),
entry4.clone(),
entry5.clone(),
],
);
assert_eq!(ucp.len(), 5);
assert_eq!(ucp.all_conflict(&entry1).len(), 3);
assert_eq!(ucp.all_conflict(&entry3).len(), 2);
compare_commands(
ucp.all_conflict(&entry1),
vec![entry1.clone(), entry2.clone(), entry4.clone()],
);
compare_commands(
ucp.all_conflict(&entry3),
vec![entry3.clone(), entry5.clone()],
);
ucp.remove(entry3.clone());
ucp.remove(entry5.clone());
assert!(!ucp.insert(entry5.clone()));
Expand All @@ -127,7 +160,7 @@ fn exclusive_sp_operations_are_ok() {
assert!(sp.insert_if_not_conflict(entry1.clone()).is_some());
assert!(sp.insert_if_not_conflict(entry1.clone()).is_some());
assert!(sp.insert_if_not_conflict(entry2.clone()).is_some());
assert_eq!(sp.all().len(), 1);
compare_commands(sp.all(), vec![entry1.clone()]);
assert_eq!(sp.len(), 1);
sp.remove(entry1);
assert!(sp.insert_if_not_conflict(entry2).is_some());
Expand All @@ -144,8 +177,11 @@ fn exclusive_ucp_operations_are_ok() {
let entry2 = gen.gen_role_add();
assert!(ucp.insert(entry1.clone()));
assert!(ucp.insert(entry2.clone()));
assert_eq!(ucp.all().len(), 2);
assert_eq!(ucp.all_conflict(&entry1).len(), 2);
compare_commands(ucp.all(), vec![entry1.clone(), entry2.clone()]);
compare_commands(
ucp.all_conflict(&entry1),
vec![entry1.clone(), entry2.clone()],
);
assert_eq!(ucp.len(), 2);
ucp.remove(entry1.clone());
ucp.remove(entry2.clone());
Expand All @@ -155,6 +191,12 @@ fn exclusive_ucp_operations_are_ok() {
assert_eq!(ucp.len(), 0);
}

fn compare_commands(mut a: Vec<CommandEntry<Command>>, mut b: Vec<CommandEntry<Command>>) {
a.sort_unstable();
b.sort_unstable();
assert_eq!(a, b);
}

#[derive(Default)]
struct EntryGenerator {
id: u64,
Expand Down
3 changes: 1 addition & 2 deletions crates/xlineapi/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ impl From<KeyRange> for PbKeyRange {
}

/// Command to run consensus protocol
#[cfg_attr(test, derive(PartialEq))]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Command {
/// Request data
request: RequestWrapper,
Expand Down

0 comments on commit 0ab29e9

Please sign in to comment.