Skip to content

Commit

Permalink
Merge pull request #14 from NicolasKlenert/master
Browse files Browse the repository at this point in the history
Fix skip bug of consumer
  • Loading branch information
klingtnet authored Aug 23, 2022
2 parents 947172b + f390df9 commit 8bac3c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl<T: Clone + Copy> RbConsumer<T> for Consumer<T> {
let count = cmp::min(cnt, self.inspector.count());
let prev_read_pos = self.inspector.read_pos.load(Ordering::Relaxed);
self.inspector.read_pos.store(
(prev_read_pos + count) % self.inspector.capacity(),
(prev_read_pos + count) % self.inspector.size,
Ordering::Relaxed,
);
Ok(count)
Expand Down
28 changes: 28 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,31 @@ fn get_with_wrapping_2() {
assert_eq!(d[0], 2);
assert_eq!(d[1], 3);
}

#[test]
fn read_equals_get_and_skip() {
let rb = SpscRb::new(2);
let (consumer, producer) = (rb.consumer(), rb.producer());
let mut a = [1, 2];
assert_eq!(producer.write(&a).unwrap(), 2);
assert_eq!(rb.count(), 2);
assert_eq!(rb.slots_free(), 0);
let mut b = [0, 0, 0, 0];
assert_eq!(consumer.read(&mut b).unwrap(), 2);
assert_eq!(b, [1, 2, 0, 0]);
assert_eq!(rb.count(), 0);
assert_eq!(rb.slots_free(), 2);

// the same should be the case for get and skip
a[0] = 3;
a[1] = 4;
assert_eq!(producer.write(&a).unwrap(), 2);
assert_eq!(rb.count(), 2);
assert_eq!(consumer.get(&mut b).unwrap(), 2);
assert_eq!(b, [3, 4, 0, 0]);
assert_eq!(rb.count(), 2);
assert_eq!(rb.slots_free(), 0);
assert_eq!(consumer.skip(2).unwrap(), 2);
assert_eq!(rb.count(), 0);
assert_eq!(rb.slots_free(), 2);
}

0 comments on commit 8bac3c2

Please sign in to comment.