|
| 1 | +mod utils; |
| 2 | + |
| 3 | +use std::path::Path; |
| 4 | +use couchbase_lite::*; |
| 5 | +use utils::*; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + let mut db = Database::open( |
| 9 | + "test1", |
| 10 | + Some(DatabaseConfiguration { |
| 11 | + directory: Path::new("./"), |
| 12 | + #[cfg(feature = "enterprise")] |
| 13 | + encryption_key: None, |
| 14 | + }), |
| 15 | + ) |
| 16 | + .unwrap(); |
| 17 | + |
| 18 | + add_or_update_user("great_name", vec!["channel1".into()]); |
| 19 | + let session_token = get_session("great_name"); |
| 20 | + println!("Sync gateway session token: {session_token}"); |
| 21 | + |
| 22 | + let mut repl = |
| 23 | + setup_replicator(db.clone(), session_token).add_document_listener(Box::new(doc_listener)); |
| 24 | + |
| 25 | + repl.start(false); |
| 26 | + |
| 27 | + std::thread::sleep(std::time::Duration::from_secs(3)); |
| 28 | + |
| 29 | + // Auto-purge test scenario from support ticket https://support.couchbase.com/hc/en-us/requests/70596?page=1 |
| 30 | + // Testing if documents pushed to inaccessible channels get auto-purged |
| 31 | + create_doc(&mut db, "doc1", "channel1"); |
| 32 | + create_doc(&mut db, "doc2", "channel2"); |
| 33 | + |
| 34 | + std::thread::sleep(std::time::Duration::from_secs(10)); |
| 35 | + assert!(get_doc(&db, "doc1").is_ok()); |
| 36 | + assert!(get_doc(&db, "doc2").is_ok()); // This looks buggy |
| 37 | + |
| 38 | + change_channel(&mut db, "doc1", "channel2"); |
| 39 | + |
| 40 | + std::thread::sleep(std::time::Duration::from_secs(10)); |
| 41 | + assert!(get_doc(&db, "doc1").is_err()); |
| 42 | + |
| 43 | + repl.stop(None); |
| 44 | +} |
| 45 | + |
| 46 | +fn create_doc(db: &mut Database, id: &str, channel: &str) { |
| 47 | + let mut doc = Document::new_with_id(id); |
| 48 | + doc.set_properties_as_json( |
| 49 | + &serde_json::json!({ |
| 50 | + "channels": channel, |
| 51 | + }) |
| 52 | + .to_string(), |
| 53 | + ) |
| 54 | + .unwrap(); |
| 55 | + db.save_document(&mut doc).unwrap(); |
| 56 | + |
| 57 | + println!( |
| 58 | + "Created doc {id} with content: {}", |
| 59 | + doc.properties_as_json() |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +fn get_doc(db: &Database, id: &str) -> Result<Document> { |
| 64 | + db.get_document(id) |
| 65 | +} |
| 66 | + |
| 67 | +fn change_channel(db: &mut Database, id: &str, channel: &str) { |
| 68 | + let mut doc = get_doc(db, id).unwrap(); |
| 69 | + let mut prop = doc.mutable_properties(); |
| 70 | + prop.at("channels").put_string(channel); |
| 71 | + let _ = db.save_document(&mut doc); |
| 72 | + println!( |
| 73 | + "Changed doc {id} with content: {}", |
| 74 | + doc.properties_as_json() |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +fn setup_replicator(db: Database, session_token: String) -> Replicator { |
| 79 | + let repl_conf = ReplicatorConfiguration { |
| 80 | + database: Some(db.clone()), |
| 81 | + endpoint: Endpoint::new_with_url(SYNC_GW_URL).unwrap(), |
| 82 | + replicator_type: ReplicatorType::PushAndPull, |
| 83 | + continuous: true, |
| 84 | + disable_auto_purge: false, |
| 85 | + max_attempts: 3, |
| 86 | + max_attempt_wait_time: 1, |
| 87 | + heartbeat: 60, |
| 88 | + authenticator: None, |
| 89 | + proxy: None, |
| 90 | + headers: vec![( |
| 91 | + "Cookie".to_string(), |
| 92 | + format!("SyncGatewaySession={session_token}"), |
| 93 | + )] |
| 94 | + .into_iter() |
| 95 | + .collect(), |
| 96 | + pinned_server_certificate: None, |
| 97 | + trusted_root_certificates: None, |
| 98 | + channels: MutableArray::default(), |
| 99 | + document_ids: MutableArray::default(), |
| 100 | + collections: None, |
| 101 | + accept_parent_domain_cookies: false, |
| 102 | + #[cfg(feature = "enterprise")] |
| 103 | + accept_only_self_signed_server_certificate: false, |
| 104 | + }; |
| 105 | + let repl_context = ReplicationConfigurationContext::default(); |
| 106 | + Replicator::new(repl_conf, Box::new(repl_context)).unwrap() |
| 107 | +} |
| 108 | + |
| 109 | +fn doc_listener(direction: Direction, documents: Vec<ReplicatedDocument>) { |
| 110 | + println!("=== Document(s) replicated ==="); |
| 111 | + println!("Direction: {direction:?}"); |
| 112 | + for document in documents { |
| 113 | + println!("Document: {document:?}"); |
| 114 | + } |
| 115 | + println!("==="); |
| 116 | +} |
0 commit comments