Skip to content

Commit

Permalink
fix: use lower order bytes from counter (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai authored Jun 10, 2024
1 parent 533e122 commit d2283f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,26 @@ impl Factory {
Pxid::from_parts(prefix, time, self.machine_id, self.process_id, counter)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn factory_never_repeats() {
const TRYOUTS: usize = 1000;

let mut specimen: Vec<Pxid> = Vec::with_capacity(TRYOUTS);
let factory = Factory::new().unwrap();

for _ in 0..TRYOUTS {
let id = factory.new_id("test").unwrap();
specimen.push(id);
}

for id in specimen.iter() {
let count = specimen.iter().filter(|&x| x == id).count();
assert_eq!(count, 1);
}
}
}
2 changes: 1 addition & 1 deletion src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl Pxid {
bytes[12..=13].copy_from_slice(&process_id.to_be_bytes());

// 3 bytes of increment counter (big endian)
bytes[14..].copy_from_slice(&counter.to_be_bytes()[0..=1]);
bytes[14..].copy_from_slice(&counter.to_be_bytes()[2..4]);

Ok(Self(bytes))
}
Expand Down

0 comments on commit d2283f7

Please sign in to comment.