Skip to content

Commit 8e4cef1

Browse files
committed
make Configuration::to_string() deterministic
tests assume that the order of elements is deterministic. Without this you can be unlucky and get a tests failure like: ``` assertion failed: `(left == right)`' left: `"voters=(2 3 1)&&(1) autoleave\n1: StateProbe match=0 next=0\n2: StateProbe match=0 next=1\n3: StateProbe match=0 next=1\n"` right: `"voters=(1 2 3)&&(1) autoleave\n1: StateProbe match=0 next=0\n2: StateProbe match=0 next=1\n3: StateProbe match=0 next=1\n"` Differences (-left|+right): -voters=(2 3 1)&&(1) autoleave +voters=(1 2 3)&&(1) autoleave 1: StateProbe match=0 next=0 2: StateProbe match=0 next=1 3: StateProbe match=0 next=1 ``` Signed-off-by: Guillaume Girol <guillaume.girol@rubycat.eu>
1 parent 7d60c11 commit 8e4cef1

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

src/tracker.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,37 +92,39 @@ pub struct Configuration {
9292
#[cfg(test)]
9393
impl std::fmt::Display for Configuration {
9494
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95+
fn iter_to_sorted_string(iter: impl Iterator<Item = impl ToString + Ord>) -> String {
96+
iter.sorted()
97+
.map(|x| x.to_string())
98+
.collect::<Vec<String>>()
99+
.join(" ")
100+
}
95101
use itertools::Itertools;
96102
if self.voters.outgoing.is_empty() {
97-
write!(f, "voters={}", self.voters.incoming)?
103+
write!(
104+
f,
105+
"voters=({})",
106+
iter_to_sorted_string(self.voters.incoming.iter())
107+
)?
98108
} else {
99109
write!(
100110
f,
101-
"voters={}&&{}",
102-
self.voters.incoming, self.voters.outgoing
111+
"voters=({})&&({})",
112+
iter_to_sorted_string(self.voters.incoming.iter()),
113+
iter_to_sorted_string(self.voters.outgoing.iter())
103114
)?
104115
}
105116
if !self.learners.is_empty() {
106117
write!(
107118
f,
108119
" learners=({})",
109-
self.learners
110-
.iter()
111-
.sorted_by(|&a, &b| a.cmp(b))
112-
.map(|x| x.to_string())
113-
.collect::<Vec<String>>()
114-
.join(" ")
120+
iter_to_sorted_string(self.learners.iter())
115121
)?
116122
}
117123
if !self.learners_next.is_empty() {
118124
write!(
119125
f,
120126
" learners_next=({})",
121-
self.learners_next
122-
.iter()
123-
.map(|x| x.to_string())
124-
.collect::<Vec<String>>()
125-
.join(" ")
127+
iter_to_sorted_string(self.learners_next.iter())
126128
)?
127129
}
128130
if self.auto_leave {

0 commit comments

Comments
 (0)