Skip to content

Commit

Permalink
Merge pull request #595 from S7evinK/s7evink/cloneable-goose-user
Browse files Browse the repository at this point in the history
Make GooseUser and GooseUserData cloneable
  • Loading branch information
jeremyandrews authored Jun 13, 2024
2 parents b6a0566 + 097122a commit 7c1e584
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/goose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ use downcast_rs::{impl_downcast, Downcast};
use regex::Regex;
use reqwest::{header, Client, ClientBuilder, Method, RequestBuilder, Response};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -822,8 +823,28 @@ pub trait GooseUserData: Downcast + Send + Sync + 'static {}
impl_downcast!(GooseUserData);
impl<T: Send + Sync + 'static> GooseUserData for T {}

trait CloneGooseUserData {
fn clone_goose_user_data(&self) -> Box<dyn GooseUserData>;
}

impl<T> CloneGooseUserData for T
where
T: GooseUserData + Clone + 'static,
{
fn clone_goose_user_data(&self) -> Box<dyn GooseUserData> {
Box::new(self.clone())
}
}

impl Clone for Box<dyn GooseUserData> {
fn clone(&self) -> Self {
self.clone_goose_user_data()
}
}

/// An individual user state, repeatedly running all [`Transaction`](./struct.Transaction.html)s
/// in a specific [`Scenario`](./struct.Scenario.html).
#[derive(Debug)]
pub struct GooseUser {
/// The Instant when this `GooseUser` client started.
pub started: Instant,
Expand Down Expand Up @@ -870,6 +891,43 @@ pub struct GooseUser {
/// [`GooseUserData`] trait.
session_data: Option<Box<dyn GooseUserData>>,
}

impl Clone for GooseUser {
fn clone(&self) -> Self {
Self {
started: self.started,
iterations: self.iterations,
scenarios_index: self.scenarios_index,
scenario_name: self.scenario_name.clone(),
transaction_index: self.transaction_index.clone(),
transaction_name: self.transaction_name.clone(),
client: self.client.clone(),
base_url: self.base_url.clone(),
config: self.config.clone(),
logger: self.logger.clone(),
throttle: self.throttle.clone(),
is_throttled: self.is_throttled,
metrics_channel: self.metrics_channel.clone(),
shutdown_channel: self.shutdown_channel.clone(),
weighted_users_index: self.weighted_users_index,
load_test_hash: self.load_test_hash,
request_cadence: self.request_cadence.clone(),
slept: self.slept,
session_data: if self.session_data.is_some() {
Option::from(self.session_data.clone_goose_user_data())
} else {
None
},
}
}
}

impl Debug for dyn GooseUserData {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
write!(fmt, "GooseUserData")
}
}

impl GooseUser {
/// Create a new user state.
pub fn new(
Expand Down Expand Up @@ -3351,7 +3409,7 @@ mod tests {

#[test]
fn test_get_mut_session_data() {
#[derive(Debug)]
#[derive(Debug, Clone)]
struct CustomSessionData {
data: String,
}
Expand Down

0 comments on commit 7c1e584

Please sign in to comment.