Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implementing Eq only for unit tests
  • Loading branch information
BradleyOlson64 committed Mar 10, 2023
commit 3fcb639ab2cca3e5ed2115f41cfbe90f23668e6a
29 changes: 17 additions & 12 deletions node/core/dispute-coordinator/src/participation/queues/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,26 @@ impl ParticipationRequest {
let Self { candidate_hash, candidate_receipt, .. } = self;
(candidate_hash, candidate_receipt)
}
// For tests we want to check whether requests are equal, but the
// request_timer field of ParticipationRequest doesn't implement
// eq. This helper checks whether all other fields are equal,
// which is sufficient.
#[cfg(test)]
pub fn functionally_equal(&self, other: ParticipationRequest) -> bool {
if &self.candidate_receipt == other.candidate_receipt() &&
&self.candidate_hash == other.candidate_hash() &&
}

// We want to compare participation requests in unit tests, so we
// only implement Eq for tests.
#[cfg(test)]
impl PartialEq for ParticipationRequest {
fn eq(&self, other: &Self) -> bool {
let ParticipationRequest {
candidate_receipt,
candidate_hash,
session: _session,
_request_timer,
} = self;
candidate_receipt == other.candidate_receipt() &&
candidate_hash == other.candidate_hash() &&
self.session == other.session()
{
return true
}
false
}
}
#[cfg(test)]
impl Eq for ParticipationRequest {}

impl Queues {
/// Create new `Queues`.
Expand Down
21 changes: 10 additions & 11 deletions node/core/dispute-coordinator/src/participation/queues/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ fn ordering_works_as_expected() {
);

// Prioritized queue is ordered correctly
assert!(queue.dequeue().unwrap().functionally_equal(req_prio));
assert!(queue.dequeue().unwrap().functionally_equal(req_prio_2));
assert_eq!(queue.dequeue(), Some(req_prio));
assert_eq!(queue.dequeue(), Some(req_prio_2));
// So is the best-effort
assert!(queue.dequeue().unwrap().functionally_equal(req1));
assert!(queue.dequeue().unwrap().functionally_equal(req3));
assert!(queue.dequeue().unwrap().functionally_equal(req5_unknown_parent));
assert_eq!(queue.dequeue(), Some(req1));
assert_eq!(queue.dequeue(), Some(req3));
assert_eq!(queue.dequeue(), Some(req5_unknown_parent));

assert_matches!(queue.dequeue(), None);
}
Expand All @@ -127,7 +127,7 @@ fn candidate_is_only_dequeued_once() {
let req_prio = make_participation_request(Hash::repeat_byte(0x02));
let req_best_effort_then_prio = make_participation_request(Hash::repeat_byte(0x03));
let req_prio_then_best_effort = make_participation_request(Hash::repeat_byte(0x04));

queue
.queue_with_comparator(
make_dummy_comparator(&req1, None),
Expand Down Expand Up @@ -158,7 +158,6 @@ fn candidate_is_only_dequeued_once() {
req_prio.clone(),
)
.unwrap();

// Insert first as best effort:
queue
.queue_with_comparator(
Expand All @@ -177,7 +176,7 @@ fn candidate_is_only_dequeued_once() {
.unwrap();

// Make space in prio:
assert!(queue.dequeue().unwrap().functionally_equal(req_prio));
assert_eq!(queue.dequeue(), Some(req_prio));

// Insert first as prio:
queue
Expand All @@ -196,8 +195,8 @@ fn candidate_is_only_dequeued_once() {
)
.unwrap();

assert!(queue.dequeue().unwrap().functionally_equal(req_best_effort_then_prio));
assert!(queue.dequeue().unwrap().functionally_equal(req_prio_then_best_effort));
assert!(queue.dequeue().unwrap().functionally_equal(req1));
assert_eq!(queue.dequeue(), Some(req_best_effort_then_prio));
assert_eq!(queue.dequeue(), Some(req_prio_then_best_effort));
assert_eq!(queue.dequeue(), Some(req1));
assert_matches!(queue.dequeue(), None);
}