Skip to content

Commit 3569cc2

Browse files
RUST-768 Test that versioned API options are passed to transaction-continuing commands (#342)
1 parent 372dc99 commit 3569cc2

File tree

5 files changed

+77
-20
lines changed

5 files changed

+77
-20
lines changed

src/sync/test.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,10 @@ fn typed_collection() {
187187
#[test]
188188
#[function_name::named]
189189
fn transactions() {
190-
let (should_skip, should_create_collection) = RUNTIME.block_on(async {
190+
let should_skip = RUNTIME.block_on(async {
191191
let test_client = AsyncTestClient::new().await;
192192
// TODO RUST-122: Unskip this test on sharded clusters
193-
let should_skip = !test_client.is_replica_set() || test_client.server_version_lt(4, 0);
194-
let should_create_collection = test_client.server_version_lt(4, 4);
195-
(should_skip, should_create_collection)
193+
!test_client.is_replica_set() || test_client.server_version_lt(4, 0)
196194
});
197195
if should_skip {
198196
return;
@@ -205,12 +203,10 @@ fn transactions() {
205203
.expect("session creation should succeed");
206204
let coll = init_db_and_typed_coll(&client, function_name!(), function_name!());
207205

208-
if should_create_collection {
209-
client
210-
.database(function_name!())
211-
.create_collection(function_name!(), None)
212-
.expect("create collection should succeed");
213-
}
206+
client
207+
.database(function_name!())
208+
.create_collection(function_name!(), None)
209+
.expect("create collection should succeed");
214210

215211
session
216212
.start_transaction(None)

src/test/spec/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod sessions;
1616
mod transactions;
1717
mod unified_runner;
1818
mod v2_runner;
19+
mod versioned_api;
1920

2021
use std::{
2122
convert::TryFrom,

src/test/spec/transactions.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ use serde::{Deserialize, Serialize};
22
use tokio::sync::{RwLockReadGuard, RwLockWriteGuard};
33

44
use crate::{
5-
bson::{serde_helpers::serialize_u64_as_i32, Document},
5+
bson::{doc, serde_helpers::serialize_u64_as_i32, Document},
66
client::session::TransactionState,
77
test::{run_spec_test, TestClient, LOCK},
88
};
99

1010
use super::run_v2_test;
1111

12-
use crate::bson::doc;
13-
1412
#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))]
1513
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
1614
async fn run() {

src/test/spec/unified_runner/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,3 @@ async fn test_examples() {
255255
let _guard: RwLockWriteGuard<_> = LOCK.run_exclusively().await;
256256
run_spec_test(&["unified-runner-examples"], run_unified_format_test).await;
257257
}
258-
259-
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
260-
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
261-
async fn test_versioned_api() {
262-
let _guard: RwLockWriteGuard<_> = LOCK.run_exclusively().await;
263-
run_spec_test(&["versioned-api"], run_unified_format_test).await;
264-
}

src/test/spec/versioned_api.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use tokio::sync::{RwLockReadGuard, RwLockWriteGuard};
2+
3+
use crate::{
4+
bson::doc,
5+
options::{ServerApi, ServerApiVersion},
6+
test::{run_spec_test, EventClient, TestClient, CLIENT_OPTIONS, LOCK},
7+
};
8+
9+
use super::run_unified_format_test;
10+
11+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
12+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
13+
async fn run() {
14+
let _guard: RwLockWriteGuard<_> = LOCK.run_exclusively().await;
15+
// TODO RUST-725 Unskip these tests on 5.0
16+
if TestClient::new().await.server_version_gte(5, 0) {
17+
return;
18+
}
19+
run_spec_test(&["versioned-api"], run_unified_format_test).await;
20+
}
21+
22+
// TODO RUST-817 Remove this test in favor of transaction-handling.json versioned API spec test when
23+
// transactions are implemented in the unified runner
24+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
25+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
26+
#[function_name::named]
27+
async fn transaction_handling() {
28+
let _guard: RwLockReadGuard<_> = LOCK.run_concurrently().await;
29+
30+
let version = ServerApi::builder()
31+
.version(ServerApiVersion::Version1)
32+
.build();
33+
34+
let mut options = CLIENT_OPTIONS.clone();
35+
options.server_api = Some(version);
36+
let client = EventClient::with_options(options).await;
37+
if !client.is_replica_set() || client.server_version_lt(5, 0) {
38+
return;
39+
}
40+
41+
let mut session = client.start_session(None).await.unwrap();
42+
session.start_transaction(None).await.unwrap();
43+
44+
let coll = client
45+
.database(function_name!())
46+
.collection(function_name!());
47+
48+
coll.insert_one_with_session(doc! { "x": 1 }, None, &mut session)
49+
.await
50+
.unwrap();
51+
session.commit_transaction().await.unwrap();
52+
session.commit_transaction().await.unwrap();
53+
54+
session.start_transaction(None).await.unwrap();
55+
56+
coll.insert_one_with_session(doc! { "y": 2 }, None, &mut session)
57+
.await
58+
.unwrap();
59+
session.abort_transaction().await.unwrap();
60+
session
61+
.abort_transaction()
62+
.await
63+
.expect_err("aborting twice should fail");
64+
65+
let events = client.get_all_command_started_events();
66+
for event in events {
67+
assert!(event.command.contains_key("apiVersion"));
68+
}
69+
}

0 commit comments

Comments
 (0)