Skip to content

Commit a16c0a8

Browse files
CSHARP-2658: Specify effect of client-side errors on in-progress transactions.
1 parent 434ffb3 commit a16c0a8

File tree

6 files changed

+238
-3
lines changed

6 files changed

+238
-3
lines changed

src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingCommandMessageWireProtocol.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ public TCommandResult Execute(IConnection connection, CancellationToken cancella
102102
}
103103
finally
104104
{
105-
MessageWasProbablySent(message);
105+
if (message.WasSent)
106+
{
107+
MessageWasProbablySent(message);
108+
}
106109
}
107110

108111
if (message.WrappedMessage.ResponseExpected)
@@ -141,7 +144,10 @@ public async Task<TCommandResult> ExecuteAsync(IConnection connection, Cancellat
141144
}
142145
finally
143146
{
144-
MessageWasProbablySent(message);
147+
if (message.WasSent)
148+
{
149+
MessageWasProbablySent(message);
150+
}
145151
}
146152

147153
if (message.WrappedMessage.ResponseExpected)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Copyright 2019-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Threading;
19+
using System.Threading.Tasks;
20+
using FluentAssertions;
21+
using MongoDB.Bson;
22+
using MongoDB.Driver.Core.Bindings;
23+
24+
namespace MongoDB.Driver.Tests.JsonDrivenTests
25+
{
26+
public class JsonDrivenAssertSessionTransactionStateTest : JsonDrivenTestRunnerTest
27+
{
28+
private CoreTransactionState _state;
29+
30+
public JsonDrivenAssertSessionTransactionStateTest(IJsonDrivenTestRunner testRunner, Dictionary<string, object> objectMap)
31+
: base(testRunner, objectMap)
32+
{
33+
34+
}
35+
36+
// public methods
37+
public override void Act(CancellationToken cancellationToken)
38+
{
39+
// do nothing
40+
}
41+
42+
public override Task ActAsync(CancellationToken cancellationToken)
43+
{
44+
// do nothing
45+
return Task.FromResult(true);
46+
}
47+
48+
public override void Assert()
49+
{
50+
CoreSession.CurrentTransaction.State.Should().Be(_state);
51+
}
52+
53+
// protected methods
54+
protected override void SetArgument(string name, BsonValue value)
55+
{
56+
switch (name)
57+
{
58+
case "state":
59+
_state = MapTransactionState(value.ToString());
60+
break;
61+
default:
62+
base.SetArgument(name, value);
63+
break;
64+
}
65+
}
66+
67+
// private methods
68+
private CoreTransactionState MapTransactionState(string value)
69+
{
70+
switch (value)
71+
{
72+
case "in_progress": return CoreTransactionState.InProgress;
73+
default:
74+
return (CoreTransactionState) Enum.Parse(typeof(CoreTransactionState), value, true);
75+
}
76+
}
77+
}
78+
}

tests/MongoDB.Driver.Tests/JsonDrivenTests/JsonDrivenInsertOneTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public JsonDrivenInsertOneTest(IMongoCollection<BsonDocument> collection, Dictio
3737
// public methods
3838
public override void Arrange(BsonDocument document)
3939
{
40-
JsonDrivenHelper.EnsureAllFieldsAreValid(document, "name", "object", "collectionOptions", "arguments", "result");
40+
JsonDrivenHelper.EnsureAllFieldsAreValid(document, "name", "object", "collectionOptions", "arguments", "result", "error");
4141
base.Arrange(document);
4242
}
4343

tests/MongoDB.Driver.Tests/JsonDrivenTests/JsonDrivenTestFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public JsonDrivenTest CreateTest(string receiver, string name)
7070
case "assertSessionPinned": return new JsonDrivenAssertSessionPinnedTest(_testRunner, _objectMap);
7171
case "assertSessionUnpinned": return new JsonDrivenAssertSessionUnpinnedTest(_testRunner, _objectMap);
7272
case "assertSameLsidOnLastTwoCommands": return new JsonDrivenAssertSameLsidOnLastTwoCommandsTest(_testRunner, _eventCapturer, _objectMap);
73+
case "assertSessionTransactionState": return new JsonDrivenAssertSessionTransactionStateTest(_testRunner, _objectMap);
7374
default: throw new FormatException($"Invalid method name: \"{name}\".");
7475
}
7576

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
runOn:
2+
-
3+
minServerVersion: "4.0"
4+
topology: ["replicaset"]
5+
-
6+
minServerVersion: "4.1.8"
7+
topology: ["sharded"]
8+
9+
database_name: &database_name "transaction-tests"
10+
collection_name: &collection_name "test"
11+
12+
data: []
13+
tests:
14+
- description: Client side error in command starting transaction
15+
16+
operations:
17+
- name: startTransaction
18+
object: session0
19+
- name: insertOne
20+
object: collection
21+
arguments:
22+
session: session0
23+
document:
24+
_id: {.: .}
25+
error: true
26+
- name: assertSessionTransactionState
27+
object: testRunner
28+
arguments:
29+
session: session0
30+
state: starting
31+
32+
- description: Client side error when transaction is in progress
33+
34+
operations:
35+
- name: startTransaction
36+
object: session0
37+
- name: insertOne
38+
object: collection
39+
arguments:
40+
session: session0
41+
document:
42+
_id: 4
43+
result:
44+
insertedId: 4
45+
- name: insertOne
46+
object: collection
47+
arguments:
48+
session: session0
49+
document:
50+
_id: {.: .}
51+
error: true
52+
- name: assertSessionTransactionState
53+
object: testRunner
54+
arguments:
55+
session: session0
56+
state: in_progress

0 commit comments

Comments
 (0)