Skip to content

Commit b4f84b8

Browse files
authored
CDRIVER-3215 Assert transaction state does not change on client-side error (#763)
1 parent 68fd0b9 commit b4f84b8

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

src/libmongoc/tests/json-test-operations.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,53 @@ assert_event_count (json_test_ctx_t *ctx, const bson_t *operation)
22552255
}
22562256
}
22572257

2258+
static void
2259+
assert_session_transaction_state (json_test_ctx_t *ctx, const bson_t *operation)
2260+
{
2261+
const char *expected_state;
2262+
const mongoc_client_session_t *session;
2263+
mongoc_transaction_state_t state;
2264+
char *state_strings[] = {
2265+
"none", "starting", "in progress", "committed", "aborted"};
2266+
2267+
expected_state = bson_lookup_utf8 (operation, "arguments.state");
2268+
session = session_from_name (
2269+
ctx, bson_lookup_utf8 (operation, "arguments.session"));
2270+
state = mongoc_client_session_get_transaction_state (session);
2271+
2272+
if (!strcmp (expected_state, "none")) {
2273+
if (state != MONGOC_TRANSACTION_NONE) {
2274+
test_error ("expected session transaction state none, but have %s",
2275+
state_strings[state]);
2276+
}
2277+
} else if (!strcmp (expected_state, "starting")) {
2278+
if (state != MONGOC_TRANSACTION_STARTING) {
2279+
test_error ("expected session transaction state starting, but have %s",
2280+
state_strings[state]);
2281+
}
2282+
} else if (!strcmp (expected_state, "in_progress")) {
2283+
if (state != MONGOC_TRANSACTION_IN_PROGRESS) {
2284+
test_error (
2285+
"expected session transaction state in progress, but have %s",
2286+
state_strings[state]);
2287+
}
2288+
} else if (!strcmp (expected_state, "committed")) {
2289+
if (state != MONGOC_TRANSACTION_COMMITTED) {
2290+
test_error (
2291+
"expected session transaction state committed, but have %s",
2292+
state_strings[state]);
2293+
}
2294+
} else if (!strcmp (expected_state, "aborted")) {
2295+
if (state != MONGOC_TRANSACTION_ABORTED) {
2296+
test_error ("expected session transaction state aborted, but have %s",
2297+
state_strings[state]);
2298+
}
2299+
} else {
2300+
test_error ("unrecognized state %s for assertSessionTransactionState",
2301+
expected_state);
2302+
}
2303+
}
2304+
22582305
static bool
22592306
op_error (const bson_t *operation)
22602307
{
@@ -2549,6 +2596,8 @@ json_test_operation (json_test_ctx_t *ctx,
25492596
wt = thread_from_name (ctx,
25502597
bson_lookup_utf8 (operation, "arguments.name"));
25512598
run_on_thread (wt, &op);
2599+
} else if (!strcmp (op_name, "assertSessionTransactionState")) {
2600+
assert_session_transaction_state (ctx, operation);
25522601
} else {
25532602
test_error ("unrecognized testRunner operation name %s", op_name);
25542603
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"runOn": [
3+
{
4+
"minServerVersion": "4.0",
5+
"topology": [
6+
"replicaset"
7+
]
8+
},
9+
{
10+
"minServerVersion": "4.1.8",
11+
"topology": [
12+
"sharded"
13+
]
14+
}
15+
],
16+
"database_name": "transaction-tests",
17+
"collection_name": "test",
18+
"data": [],
19+
"tests": [
20+
{
21+
"description": "Client side error in command starting transaction",
22+
"operations": [
23+
{
24+
"name": "startTransaction",
25+
"object": "session0"
26+
},
27+
{
28+
"name": "insertOne",
29+
"object": "collection",
30+
"arguments": {
31+
"session": "session0",
32+
"document": {
33+
"_id": {
34+
".": "."
35+
}
36+
}
37+
},
38+
"error": true
39+
},
40+
{
41+
"name": "assertSessionTransactionState",
42+
"object": "testRunner",
43+
"arguments": {
44+
"session": "session0",
45+
"state": "starting"
46+
}
47+
}
48+
]
49+
},
50+
{
51+
"description": "Client side error when transaction is in progress",
52+
"operations": [
53+
{
54+
"name": "startTransaction",
55+
"object": "session0"
56+
},
57+
{
58+
"name": "insertOne",
59+
"object": "collection",
60+
"arguments": {
61+
"session": "session0",
62+
"document": {
63+
"_id": 4
64+
}
65+
},
66+
"result": {
67+
"insertedId": 4
68+
}
69+
},
70+
{
71+
"name": "insertOne",
72+
"object": "collection",
73+
"arguments": {
74+
"session": "session0",
75+
"document": {
76+
"_id": {
77+
".": "."
78+
}
79+
}
80+
},
81+
"error": true
82+
},
83+
{
84+
"name": "assertSessionTransactionState",
85+
"object": "testRunner",
86+
"arguments": {
87+
"session": "session0",
88+
"state": "in_progress"
89+
}
90+
}
91+
]
92+
}
93+
]
94+
}
95+

0 commit comments

Comments
 (0)