threads3: Improve the test

This commit is contained in:
mo8it 2024-08-20 13:35:07 +02:00
parent 631f44331e
commit d141a73493
2 changed files with 10 additions and 18 deletions

View file

@ -1,7 +1,6 @@
use std::{sync::mpsc, thread, time::Duration}; use std::{sync::mpsc, thread, time::Duration};
struct Queue { struct Queue {
length: u32,
first_half: Vec<u32>, first_half: Vec<u32>,
second_half: Vec<u32>, second_half: Vec<u32>,
} }
@ -9,7 +8,6 @@ struct Queue {
impl Queue { impl Queue {
fn new() -> Self { fn new() -> Self {
Self { Self {
length: 10,
first_half: vec![1, 2, 3, 4, 5], first_half: vec![1, 2, 3, 4, 5],
second_half: vec![6, 7, 8, 9, 10], second_half: vec![6, 7, 8, 9, 10],
} }
@ -48,17 +46,15 @@ mod tests {
fn threads3() { fn threads3() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let queue = Queue::new(); let queue = Queue::new();
let queue_length = queue.length;
send_tx(queue, tx); send_tx(queue, tx);
let mut total_received: u32 = 0; let mut received = Vec::with_capacity(10);
for received in rx { for value in rx {
println!("Got: {received}"); received.push(value);
total_received += 1;
} }
println!("Number of received values: {total_received}"); received.sort();
assert_eq!(total_received, queue_length); assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
} }
} }

View file

@ -1,7 +1,6 @@
use std::{sync::mpsc, thread, time::Duration}; use std::{sync::mpsc, thread, time::Duration};
struct Queue { struct Queue {
length: u32,
first_half: Vec<u32>, first_half: Vec<u32>,
second_half: Vec<u32>, second_half: Vec<u32>,
} }
@ -9,7 +8,6 @@ struct Queue {
impl Queue { impl Queue {
fn new() -> Self { fn new() -> Self {
Self { Self {
length: 10,
first_half: vec![1, 2, 3, 4, 5], first_half: vec![1, 2, 3, 4, 5],
second_half: vec![6, 7, 8, 9, 10], second_half: vec![6, 7, 8, 9, 10],
} }
@ -50,17 +48,15 @@ mod tests {
fn threads3() { fn threads3() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let queue = Queue::new(); let queue = Queue::new();
let queue_length = queue.length;
send_tx(queue, tx); send_tx(queue, tx);
let mut total_received: u32 = 0; let mut received = Vec::with_capacity(10);
for received in rx { for value in rx {
println!("Got: {received}"); received.push(value);
total_received += 1;
} }
println!("Number of received values: {total_received}"); received.sort();
assert_eq!(total_received, queue_length); assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
} }
} }