Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GooseUser and GooseUserData cloneable #595

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading