Skip to content

Commit 6dda93d

Browse files
authored
RUST-35 add integration tests for write concerns on some methods (#364)
* RUST-35 add integration tests for write concerns on some methods * Update tests to use EventClient
1 parent 7158ad6 commit 6dda93d

File tree

2 files changed

+238
-11
lines changed

2 files changed

+238
-11
lines changed

src/concern/test.rs

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ use crate::{
77
options::{
88
Acknowledgment,
99
FindOneOptions,
10+
InsertManyOptions,
1011
InsertOneOptions,
1112
ReadConcern,
1213
TransactionOptions,
14+
UpdateOptions,
1315
WriteConcern,
1416
},
1517
test::{EventClient, TestClient, LOCK},
18+
Collection,
1619
};
1720

1821
#[test]
@@ -210,3 +213,223 @@ async fn assert_event_contains_read_concern(client: &EventClient) {
210213
"snapshot"
211214
);
212215
}
216+
217+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
218+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
219+
#[function_name::named]
220+
async fn command_contains_write_concern_insert_one() {
221+
let _guard = LOCK.run_concurrently().await;
222+
let client = EventClient::new().await;
223+
let coll: Collection = client.database("test").collection(function_name!());
224+
225+
coll.drop(None).await.unwrap();
226+
coll.insert_one(
227+
doc! { "foo": "bar" },
228+
InsertOneOptions::builder()
229+
.write_concern(
230+
WriteConcern::builder()
231+
.w(Acknowledgment::Nodes(1))
232+
.journal(true)
233+
.build(),
234+
)
235+
.build(),
236+
)
237+
.await
238+
.unwrap();
239+
coll.insert_one(
240+
doc! { "foo": "bar" },
241+
InsertOneOptions::builder()
242+
.write_concern(
243+
WriteConcern::builder()
244+
.w(Acknowledgment::Nodes(1))
245+
.journal(false)
246+
.build(),
247+
)
248+
.build(),
249+
)
250+
.await
251+
.unwrap();
252+
253+
assert_eq!(
254+
command_write_concerns(&client, "insert"),
255+
vec![
256+
doc! {
257+
"w": 1,
258+
"j": true,
259+
},
260+
doc! {
261+
"w": 1,
262+
"j": false,
263+
},
264+
]
265+
);
266+
}
267+
268+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
269+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
270+
#[function_name::named]
271+
async fn command_contains_write_concern_insert_many() {
272+
let _guard = LOCK.run_concurrently().await;
273+
let client = EventClient::new().await;
274+
let coll: Collection = client.database("test").collection(function_name!());
275+
276+
coll.drop(None).await.unwrap();
277+
coll.insert_many(
278+
&[doc! { "foo": "bar" }],
279+
InsertManyOptions::builder()
280+
.write_concern(
281+
WriteConcern::builder()
282+
.w(Acknowledgment::Nodes(1))
283+
.journal(true)
284+
.build(),
285+
)
286+
.build(),
287+
)
288+
.await
289+
.unwrap();
290+
coll.insert_many(
291+
&[doc! { "foo": "bar" }],
292+
InsertManyOptions::builder()
293+
.write_concern(
294+
WriteConcern::builder()
295+
.w(Acknowledgment::Nodes(1))
296+
.journal(false)
297+
.build(),
298+
)
299+
.build(),
300+
)
301+
.await
302+
.unwrap();
303+
304+
assert_eq!(
305+
command_write_concerns(&client, "insert"),
306+
vec![
307+
doc! {
308+
"w": 1,
309+
"j": true,
310+
},
311+
doc! {
312+
"w": 1,
313+
"j": false,
314+
},
315+
]
316+
);
317+
}
318+
319+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
320+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
321+
#[function_name::named]
322+
async fn command_contains_write_concern_update_one() {
323+
let _guard = LOCK.run_concurrently().await;
324+
let client = EventClient::new().await;
325+
let coll: Collection = client.database("test").collection(function_name!());
326+
327+
coll.drop(None).await.unwrap();
328+
coll.insert_one(doc! { "foo": "bar" }, None).await.unwrap();
329+
coll.update_one(
330+
doc! { "foo": "bar" },
331+
doc! { "$set": { "foo": "baz" } },
332+
UpdateOptions::builder()
333+
.write_concern(
334+
WriteConcern::builder()
335+
.w(Acknowledgment::Nodes(1))
336+
.journal(true)
337+
.build(),
338+
)
339+
.build(),
340+
)
341+
.await
342+
.unwrap();
343+
coll.update_one(
344+
doc! { "foo": "baz" },
345+
doc! { "$set": { "foo": "quux" } },
346+
UpdateOptions::builder()
347+
.write_concern(
348+
WriteConcern::builder()
349+
.w(Acknowledgment::Nodes(1))
350+
.journal(false)
351+
.build(),
352+
)
353+
.build(),
354+
)
355+
.await
356+
.unwrap();
357+
358+
assert_eq!(
359+
command_write_concerns(&client, "update"),
360+
vec![
361+
doc! {
362+
"w": 1,
363+
"j": true,
364+
},
365+
doc! {
366+
"w": 1,
367+
"j": false,
368+
},
369+
]
370+
);
371+
}
372+
373+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
374+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
375+
#[function_name::named]
376+
async fn command_contains_write_concern_update_many() {
377+
let _guard = LOCK.run_concurrently().await;
378+
let client = EventClient::new().await;
379+
let coll: Collection = client.database("test").collection(function_name!());
380+
381+
coll.drop(None).await.unwrap();
382+
coll.insert_many(&[doc! { "foo": "bar" }, doc! { "foo": "bar" }], None)
383+
.await
384+
.unwrap();
385+
coll.update_many(
386+
doc! { "foo": "bar" },
387+
doc! { "$set": { "foo": "baz" } },
388+
UpdateOptions::builder()
389+
.write_concern(
390+
WriteConcern::builder()
391+
.w(Acknowledgment::Nodes(1))
392+
.journal(true)
393+
.build(),
394+
)
395+
.build(),
396+
)
397+
.await
398+
.unwrap();
399+
coll.update_many(
400+
doc! { "foo": "baz" },
401+
doc! { "$set": { "foo": "quux" } },
402+
UpdateOptions::builder()
403+
.write_concern(
404+
WriteConcern::builder()
405+
.w(Acknowledgment::Nodes(1))
406+
.journal(false)
407+
.build(),
408+
)
409+
.build(),
410+
)
411+
.await
412+
.unwrap();
413+
414+
assert_eq!(
415+
command_write_concerns(&client, "update"),
416+
vec![
417+
doc! {
418+
"w": 1,
419+
"j": true,
420+
},
421+
doc! {
422+
"w": 1,
423+
"j": false,
424+
},
425+
]
426+
);
427+
}
428+
429+
fn command_write_concerns(client: &EventClient, key: &str) -> Vec<Document> {
430+
client
431+
.get_command_started_events(&[key])
432+
.into_iter()
433+
.map(|d| d.command.get_document("writeConcern").unwrap().clone())
434+
.collect()
435+
}

src/srv.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ impl SrvResolver {
3636
pub(crate) async fn new(config: Option<ResolverConfig>) -> Result<Self> {
3737
let resolver = AsyncResolver::new(config).await?;
3838

39-
Ok(Self {
40-
resolver,
41-
})
39+
Ok(Self { resolver })
4240
}
4341

4442
pub(crate) async fn resolve_client_options(
@@ -47,7 +45,10 @@ impl SrvResolver {
4745
) -> Result<ResolvedConfig> {
4846
let lookup_result = self.get_srv_hosts(hostname).await?;
4947
let mut config = ResolvedConfig {
50-
hosts: lookup_result.hosts.into_iter().collect::<Result<Vec<ServerAddress>>>()?,
48+
hosts: lookup_result
49+
.hosts
50+
.into_iter()
51+
.collect::<Result<Vec<ServerAddress>>>()?,
5152
min_ttl: lookup_result.min_ttl,
5253
auth_source: None,
5354
replica_set: None,
@@ -58,10 +59,7 @@ impl SrvResolver {
5859
Ok(config)
5960
}
6061

61-
pub(crate) async fn get_srv_hosts(
62-
&self,
63-
original_hostname: &str,
64-
) -> Result<LookupHosts> {
62+
pub(crate) async fn get_srv_hosts(&self, original_hostname: &str) -> Result<LookupHosts> {
6563
let hostname_parts: Vec<_> = original_hostname.split('.').collect();
6664

6765
if hostname_parts.len() < 3 {
@@ -86,7 +84,10 @@ impl SrvResolver {
8684

8785
let hostname = srv.target().to_utf8();
8886
let port = Some(srv.port());
89-
let mut address = ServerAddress::Tcp { host: hostname, port };
87+
let mut address = ServerAddress::Tcp {
88+
host: hostname,
89+
port,
90+
};
9091

9192
let domain_name = &hostname_parts[1..];
9293

@@ -106,7 +107,7 @@ impl SrvResolver {
106107
domain_name.join(".")
107108
),
108109
}
109-
.into()));
110+
.into()));
110111
}
111112

112113
// The spec tests list the seeds without the trailing '.', so we remove it by
@@ -127,7 +128,10 @@ impl SrvResolver {
127128
.into());
128129
}
129130

130-
Ok(LookupHosts { hosts: srv_addresses, min_ttl: Duration::from_secs(min_ttl.into()) })
131+
Ok(LookupHosts {
132+
hosts: srv_addresses,
133+
min_ttl: Duration::from_secs(min_ttl.into()),
134+
})
131135
}
132136

133137
async fn get_txt_options(

0 commit comments

Comments
 (0)