Skip to content

Commit efc18f6

Browse files
committed
wip
1 parent 37ca918 commit efc18f6

File tree

2 files changed

+411
-14
lines changed

2 files changed

+411
-14
lines changed

testing/matrix-sdk-integration-testing/src/helpers.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{
22
collections::HashMap,
33
ops::Deref,
44
option_env,
5+
path::{Path, PathBuf},
56
sync::{Arc, Mutex as StdMutex},
67
time::Duration,
78
};
@@ -21,9 +22,14 @@ use tokio::sync::Mutex;
2122

2223
static USERS: Lazy<Mutex<HashMap<String, (Client, TempDir)>>> = Lazy::new(Mutex::default);
2324

25+
enum SqlitePath {
26+
Random,
27+
Path(PathBuf),
28+
}
29+
2430
pub struct TestClientBuilder {
2531
username: String,
26-
use_sqlite: bool,
32+
use_sqlite_dir: Option<SqlitePath>,
2733
encryption_settings: EncryptionSettings,
2834
http_proxy: Option<String>,
2935
}
@@ -32,7 +38,7 @@ impl TestClientBuilder {
3238
pub fn new(username: impl Into<String>) -> Self {
3339
Self {
3440
username: username.into(),
35-
use_sqlite: false,
41+
use_sqlite_dir: None,
3642
encryption_settings: Default::default(),
3743
http_proxy: None,
3844
}
@@ -45,7 +51,16 @@ impl TestClientBuilder {
4551
}
4652

4753
pub fn use_sqlite(mut self) -> Self {
48-
self.use_sqlite = true;
54+
self.use_sqlite_dir = Some(SqlitePath::Random);
55+
self
56+
}
57+
58+
/// Create or re-use a Sqlite store (with no passphrase) in the supplied
59+
/// directory. Note: this path must remain valid throughout the use of
60+
/// the constructed Client, so if you created a TempDir you must hang on
61+
/// to a reference to it throughout the test.
62+
pub fn use_sqlite_dir(mut self, path: &Path) -> Self {
63+
self.use_sqlite_dir = Some(SqlitePath::Path(path.to_owned()));
4964
self
5065
}
5166

@@ -59,6 +74,42 @@ impl TestClientBuilder {
5974
self
6075
}
6176

77+
pub async fn duplicate(self, other: &Client) -> Result<Client> {
78+
let homeserver_url =
79+
option_env!("HOMESERVER_URL").unwrap_or("http://localhost:8228").to_owned();
80+
let sliding_sync_proxy_url =
81+
option_env!("SLIDING_SYNC_PROXY_URL").unwrap_or("http://localhost:8338").to_owned();
82+
83+
let mut client_builder = Client::builder()
84+
.user_agent("matrix-sdk-integration-tests")
85+
.homeserver_url(homeserver_url)
86+
.sliding_sync_proxy(sliding_sync_proxy_url)
87+
.with_encryption_settings(self.encryption_settings)
88+
.request_config(RequestConfig::short_retry());
89+
90+
if let Some(proxy) = self.http_proxy {
91+
client_builder = client_builder.proxy(proxy);
92+
}
93+
94+
let client = match self.use_sqlite_dir {
95+
Some(SqlitePath::Path(path_buf)) => {
96+
client_builder.sqlite_store(&path_buf, None).build().await?
97+
}
98+
_ => {
99+
panic!("You must call use_sqlite_dir for a duplicate client!");
100+
}
101+
};
102+
103+
client
104+
.restore_session(
105+
other.session().expect("Session must be logged in before we can duplicate it"),
106+
)
107+
.await
108+
.expect("Failed to restore session");
109+
110+
Ok(client)
111+
}
112+
62113
pub async fn build(self) -> Result<Client> {
63114
let mut users = USERS.lock().await;
64115
if let Some((client, _)) = users.get(&self.username) {
@@ -83,10 +134,14 @@ impl TestClientBuilder {
83134
client_builder = client_builder.proxy(proxy);
84135
}
85136

86-
let client = if self.use_sqlite {
87-
client_builder.sqlite_store(tmp_dir.path(), None).build().await?
88-
} else {
89-
client_builder.build().await?
137+
let client = match self.use_sqlite_dir {
138+
None => client_builder.build().await?,
139+
Some(SqlitePath::Random) => {
140+
client_builder.sqlite_store(tmp_dir.path(), None).build().await?
141+
}
142+
Some(SqlitePath::Path(path_buf)) => {
143+
client_builder.sqlite_store(&path_buf, None).build().await?
144+
}
90145
};
91146

92147
// safe to assume we have not registered this user yet, but ignore if we did

0 commit comments

Comments
 (0)