|
3 | 3 |
|
4 | 4 | use couchbase_lite::*;
|
5 | 5 | use serde::{Deserialize, Serialize};
|
6 |
| -use std::{path::Path, str}; |
| 6 | +use std::{env, path::Path, str, thread, time::Duration}; |
7 | 7 | use tempfile::{tempdir, TempDir};
|
8 | 8 | use tokio::runtime;
|
9 | 9 |
|
10 |
| -// See https://github.com/Dushistov/couchbase-lite-rust/issues/54 |
11 |
| -#[test] |
| 10 | +/// See https://github.com/Dushistov/couchbase-lite-rust/issues/54 |
12 | 11 | #[ignore]
|
| 12 | +#[test] |
13 | 13 | fn test_double_replicator_restart() {
|
14 |
| - let (url, auth, tmp_dir) = init_env(); |
| 14 | + let Some((url, auth, tmp_dir)) = init_test_env() else { |
| 15 | + println!("SKIPING test_double_replicator_restart, env variables not setted"); |
| 16 | + return; |
| 17 | + }; |
15 | 18 |
|
16 | 19 | let runtime = runtime::Builder::new_current_thread()
|
17 | 20 | .enable_io()
|
@@ -66,48 +69,51 @@ fn test_double_replicator_restart() {
|
66 | 69 | println!("pushing {pushing}, docs {docs:?}");
|
67 | 70 | },
|
68 | 71 | );
|
69 |
| - let mut repl = Replicator::new(&db, url, params).unwrap(); |
| 72 | + let mut repl = Replicator::new(&db, &url, params).unwrap(); |
70 | 73 | repl.start(false).unwrap();
|
71 | 74 | repl
|
72 | 75 | };
|
73 | 76 |
|
74 | 77 | let (stop_tx, stop_rx) = tokio::sync::oneshot::channel();
|
75 | 78 |
|
76 | 79 | let thread_join_handle = {
|
77 |
| - std::thread::spawn(move || { |
| 80 | + thread::spawn(move || { |
78 | 81 | runtime.block_on(async {
|
79 | 82 | rx.recv().await.unwrap();
|
80 | 83 | println!("got async event that replicator was idle");
|
81 | 84 | rx.recv().await.unwrap();
|
82 | 85 | let _: () = stop_rx.await.unwrap();
|
83 | 86 | println!("get value from stop_rx, waiting last messages processing");
|
84 |
| - tokio::time::sleep(std::time::Duration::from_secs(1)).await; |
| 87 | + tokio::time::sleep(Duration::from_secs(1)).await; |
85 | 88 | });
|
86 | 89 | })
|
87 | 90 | };
|
88 | 91 | sync_rx.recv().unwrap();
|
89 | 92 | println!("got SYNC event that replicator was idle");
|
90 | 93 | for _ in 0..10 {
|
91 |
| - repl = repl.restart(&db, url, &auth, false).unwrap(); |
| 94 | + repl = repl.restart(&db, &url, &auth, false).unwrap(); |
92 | 95 | }
|
93 | 96 | println!("multi restart done");
|
94 |
| - std::thread::sleep(std::time::Duration::from_secs(2)); |
| 97 | + thread::sleep(Duration::from_secs(3)); |
95 | 98 | repl.stop();
|
96 | 99 | stop_tx.send(()).unwrap();
|
97 | 100 | thread_join_handle.join().unwrap();
|
98 |
| - |
99 | 101 | println!("tokio done");
|
100 | 102 | }
|
101 | 103 |
|
102 |
| -// See https://github.com/Dushistov/couchbase-lite-rust/issues/94 |
| 104 | +/// See https://github.com/Dushistov/couchbase-lite-rust/issues/94 |
103 | 105 | #[ignore]
|
104 | 106 | #[test]
|
105 | 107 | fn test_wrong_sync_packets_order() {
|
106 |
| - let (url, auth, tmp_dir) = init_env(); |
| 108 | + let Some((url, auth, tmp_dir)) = init_test_env() else { |
| 109 | + println!("SKIPING test_wrong_sync_packets_order, env variables not setted"); |
| 110 | + return; |
| 111 | + }; |
| 112 | + |
107 | 113 | let runtime = runtime::Runtime::new().unwrap();
|
108 | 114 | Database::init_socket_impl(runtime.handle().clone());
|
109 | 115 |
|
110 |
| - start_repl_and_save_documents(tmp_dir.path(), "a", 10_000, url, auth).unwrap(); |
| 116 | + start_repl_and_save_documents(tmp_dir.path(), "a", 10_000, &url, auth).unwrap(); |
111 | 117 | }
|
112 | 118 |
|
113 | 119 | #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
|
@@ -201,16 +207,35 @@ fn start_repl_and_save_documents(
|
201 | 207 | ReplicatorState::Busy(_) => was_busy = true,
|
202 | 208 | }
|
203 | 209 | }
|
204 |
| - std::thread::sleep(std::time::Duration::from_secs(2)); |
| 210 | + thread::sleep(Duration::from_secs(2)); |
205 | 211 | repl.stop();
|
206 | 212 | Ok(())
|
207 | 213 | }
|
208 | 214 |
|
209 |
| -fn init_env() -> (&'static str, ReplicatorAuthentication, TempDir) { |
| 215 | +fn get_env_var(name: &str) -> Result<String, String> { |
| 216 | + match env::var(name) { |
| 217 | + Err(err) => Err(format!("Can not get {name}: {err}")), |
| 218 | + Ok(val) if val.trim().is_empty() => Err(format!("{name} is setted but empty")), |
| 219 | + Ok(val) => Ok(val), |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +fn init_test_env() -> Option<(String, ReplicatorAuthentication, TempDir)> { |
210 | 224 | let _ = env_logger::try_init();
|
211 | 225 | let tmp_dir = tempdir().expect("Can not create tmp directory");
|
212 | 226 | println!("we create tempdir at {:?}", tmp_dir.path());
|
213 |
| - let url = "ws://127.0.0.1:4984/demo/"; |
214 |
| - let auth = ReplicatorAuthentication::None; |
215 |
| - (url, auth, tmp_dir) |
| 227 | + let Ok(url) = get_env_var("SG_URL") else { |
| 228 | + return None; |
| 229 | + }; |
| 230 | + let auth = if env::var("SG_USER").is_ok() { |
| 231 | + let username = get_env_var("SG_USER").unwrap(); |
| 232 | + let password = get_env_var("SG_PASS").unwrap(); |
| 233 | + ReplicatorAuthentication::Basic { username, password } |
| 234 | + } else if env::var("SG_TOKEN").is_ok() { |
| 235 | + let token = get_env_var("SG_TOKEN").unwrap(); |
| 236 | + ReplicatorAuthentication::SessionToken(token) |
| 237 | + } else { |
| 238 | + ReplicatorAuthentication::None |
| 239 | + }; |
| 240 | + Some((url, auth, tmp_dir)) |
216 | 241 | }
|
0 commit comments