This repository was archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathmod.rs
132 lines (116 loc) · 4.04 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
mod batch_size;
mod bulk;
mod coll;
mod connstring;
mod crud_spec;
mod db;
mod cursor;
mod error;
mod gridfs;
mod handshake;
mod wire_protocol;
use bson;
use mongodb::{Client, ThreadedClient};
use mongodb::db::ThreadedDatabase;
use std::thread;
#[test]
fn is_master() {
let client = Client::connect("localhost", 27017).unwrap();
let res = client.is_master().expect("Failed to execute is_master.");
assert!(res);
}
#[test]
fn database_names() {
let client = Client::connect("localhost", 27017).unwrap();
client
.drop_database("test-client-mod-database_names")
.expect("Failed to drop database");
client
.drop_database("test-client-mod-database_names_2")
.expect("Failed to drop database");
let base_results = client.database_names().expect(
"Failed to execute database_names.",
);
assert!(base_results.contains(&"admin".to_owned()));
assert!(base_results.contains(&"local".to_owned()));
assert!(!base_results.contains(
&"test-client-mod-database_names".to_owned(),
));
assert!(!base_results.contains(
&"test-client-mod-database_names_2".to_owned(),
));
// Build dbs
let db1 = client.db("test-client-mod-database_names");
let db2 = client.db("test-client-mod-database_names_2");
db1.collection("test1")
.insert_one(bson::Document::new(), None)
.expect("Failed to insert placeholder document into collection");
db2.collection("test2")
.insert_one(bson::Document::new(), None)
.expect("Failed to insert placeholder document into collection");
// Check new dbs
let results = client.database_names().expect(
"Failed to execute database_names.",
);
assert!(results.contains(&"admin".to_owned()));
assert!(results.contains(&"local".to_owned()));
assert!(results.contains(
&"test-client-mod-database_names".to_owned(),
));
assert!(results.contains(
&"test-client-mod-database_names_2".to_owned(),
));
}
#[test]
fn is_sync() {
let client = Client::connect("localhost", 27017).unwrap();
let client1 = client.clone();
let client2 = client.clone();
client.drop_database("test-client-mod-is_sync").expect(
"failed to drop database",
);
client.drop_database("test-client-mod-is_sync_2").expect(
"failed to drop database",
);
let base_results = client.database_names().expect(
"Failed to execute database_names.",
);
assert!(base_results.contains(&"admin".to_owned()));
assert!(base_results.contains(&"local".to_owned()));
assert!(!base_results.contains(
&"test-client-mod-is_sync".to_owned(),
));
assert!(!base_results.contains(
&"test-client-mod-is_sync_2".to_owned(),
));
let child1 = thread::spawn(move || {
let db = client1.db("test-client-mod-is_sync");
db.collection("test1")
.insert_one(bson::Document::new(), None)
.expect("Failed to insert placeholder document into collection");
let results = client1.database_names().expect(
"Failed to execute database_names.",
);
assert!(results.contains(&"test-client-mod-is_sync".to_owned()));
});
let child2 = thread::spawn(move || {
let db = client2.db("test-client-mod-is_sync_2");
db.collection("test2")
.insert_one(bson::Document::new(), None)
.expect("Failed to insert placeholder document into collection");
let results = client2.database_names().expect(
"Failed to execute database_names.",
);
assert!(results.contains(&"test-client-mod-is_sync_2".to_owned()));
});
let _ = child1.join();
let _ = child2.join();
// Check new dbs
let results = client.database_names().expect(
"Failed to execute database_names.",
);
assert!(results.contains(&"admin".to_owned()));
assert!(results.contains(&"local".to_owned()));
assert!(results.contains(&"test-client-mod-is_sync".to_owned()));
assert!(results.contains(&"test-client-mod-is_sync_2".to_owned()));
}