Skip to content

Commit

Permalink
Introduce random_str() helper function
Browse files Browse the repository at this point in the history
We already have a random_buf() function that created a buffer of random
bytes and yet we defined similar logic for creating a random string
in-line. Factor out a dedicated function, random_str(), for this
purpose.
  • Loading branch information
d-e-s-o committed Dec 29, 2024
1 parent 9789ec6 commit df1ad58
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,13 @@ mod tests {
vec
}

fn random_str() -> String {
let len = (0..32).choose(&mut thread_rng()).unwrap();
let mut string = String::new();
string.extend((0..len).map(|_| thread_rng().gen::<char>()));
string
}

for _ in 0..50000 {
let message = match (0..5).choose(&mut thread_rng()).unwrap() {
0 => WebSocketMessage::Pong(random_buf()),
Expand All @@ -766,11 +773,7 @@ mod tests {
// the connection.
i => {
if i & 0x1 == 0 {
let len = (0..32).choose(&mut thread_rng()).unwrap();
let mut string = String::new();
string.extend((0..len).map(|_| thread_rng().gen::<char>()));

WebSocketMessage::Text(string)
WebSocketMessage::Text(random_str())
} else {
WebSocketMessage::Binary(random_buf())
}
Expand Down

0 comments on commit df1ad58

Please sign in to comment.