Skip to content

Commit c25ee4f

Browse files
committed
Fix embedded transaction tests
1 parent 0e13211 commit c25ee4f

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

driver-core/src/test/functional/com/mongodb/JsonTestServerVersionChecker.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.List;
2424

25+
import static com.mongodb.ClusterFixture.getServerVersion;
2526
import static com.mongodb.ClusterFixture.getVersionList;
2627
import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet;
2728
import static com.mongodb.ClusterFixture.isSharded;
@@ -31,15 +32,16 @@
3132

3233
public final class JsonTestServerVersionChecker {
3334
private static final List<String> TOPOLOGY_TYPES = asList("sharded", "replicaset", "single");
34-
private static ServerVersion serverVersion;
3535

3636
public static boolean skipTest(final BsonDocument testDocument, final BsonDocument testDefinition) {
37-
return !(canRunTest(testDocument) && canRunTest(testDefinition));
37+
return skipTest(testDocument, testDefinition, getServerVersion());
3838
}
3939

40-
private static boolean canRunTest(final BsonDocument document) {
41-
ServerVersion serverVersion = getServerVersion();
40+
public static boolean skipTest(final BsonDocument testDocument, final BsonDocument testDefinition, final ServerVersion serverVersion) {
41+
return !(canRunTest(testDocument, serverVersion) && canRunTest(testDefinition, serverVersion));
42+
}
4243

44+
private static boolean canRunTest(final BsonDocument document, final ServerVersion serverVersion) {
4345
if (document.containsKey("minServerVersion")
4446
&& serverVersion.compareTo(getServerVersionForField("minServerVersion", document)) < 0) {
4547
return false;
@@ -54,7 +56,7 @@ private static boolean canRunTest(final BsonDocument document) {
5456
}
5557

5658
if (document.containsKey("runOn")) {
57-
return canRunTest(document.getArray("runOn"));
59+
return canRunTest(document.getArray("runOn"), serverVersion);
5860
}
5961

6062
// Ignore certain matching types
@@ -73,10 +75,10 @@ private static boolean canRunTest(final BsonDocument document) {
7375
return true;
7476
}
7577

76-
private static boolean canRunTest(final BsonArray runOn) {
78+
private static boolean canRunTest(final BsonArray runOn, final ServerVersion serverVersion) {
7779
boolean topologyFound = false;
7880
for (BsonValue info : runOn) {
79-
topologyFound = canRunTest(info.asDocument());
81+
topologyFound = canRunTest(info.asDocument(), serverVersion);
8082
if (topologyFound) {
8183
break;
8284
}
@@ -100,13 +102,6 @@ private static boolean topologyMatches(final BsonArray topologyTypes) {
100102
return false;
101103
}
102104

103-
private static ServerVersion getServerVersion() {
104-
if (serverVersion == null) {
105-
serverVersion = ClusterFixture.getServerVersion();
106-
}
107-
return serverVersion;
108-
}
109-
110105
private static ServerVersion getServerVersionForField(final String fieldName, final BsonDocument document) {
111106
return new ServerVersion(getVersionList(document.getString(fieldName).getValue()));
112107
}

driver-embedded-android/src/androidTest/java/com/mongodb/embedded/client/CrudTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.client.MongoCollection;
2020
import com.mongodb.client.MongoDatabase;
21+
2122
import org.bson.BsonArray;
2223
import org.bson.BsonDocument;
2324
import org.bson.BsonValue;
@@ -33,6 +34,8 @@
3334
import java.util.Collection;
3435
import java.util.List;
3536

37+
import static com.mongodb.embedded.client.Fixture.serverVersionGreaterThan;
38+
import static com.mongodb.embedded.client.Fixture.serverVersionLessThan;
3639
import static org.junit.Assert.assertEquals;
3740

3841
// See https://github.com/mongodb/specifications/tree/master/source/crud/tests
@@ -45,11 +48,10 @@ public class CrudTest {
4548
private MongoCollection<BsonDocument> collection;
4649
private JsonPoweredCrudTestHelper helper;
4750

48-
public CrudTest(final String description, final BsonArray data, final BsonDocument definition, final boolean skipTest) {
51+
public CrudTest(final String description, final BsonArray data, final BsonDocument definition) {
4952
this.description = description;
5053
this.data = data;
5154
this.definition = definition;
52-
this.skipTest = skipTest;
5355
}
5456

5557
@Before
@@ -113,7 +115,12 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
113115
}
114116

115117
for (BsonDocument testDocument : JsonPoweredCrudTestHelper.getTestDocuments("crud")) {
116-
if (!canRunTests(testDocument)) {
118+
if (testDocument.containsKey("minServerVersion")
119+
&& serverVersionLessThan(testDocument.getString("minServerVersion").getValue())) {
120+
continue;
121+
}
122+
if (testDocument.containsKey("maxServerVersion")
123+
&& serverVersionGreaterThan(testDocument.getString("maxServerVersion").getValue())) {
117124
continue;
118125
}
119126
for (BsonValue test: testDocument.getArray("tests")) {

driver-embedded/src/test/functional/com/mongodb/embedded/client/CrudTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static com.mongodb.JsonTestServerVersionChecker.skipTest;
4141
import static com.mongodb.client.Fixture.getDefaultDatabaseName;
4242
import static com.mongodb.embedded.client.Fixture.getMongoClient;
43+
import static com.mongodb.embedded.client.Fixture.getServerVersion;
4344
import static org.junit.Assert.assertEquals;
4445
import static org.junit.Assume.assumeFalse;
4546

@@ -132,7 +133,7 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
132133
for (BsonValue test: testDocument.getArray("tests")) {
133134
data.add(new Object[]{file.getName(), test.asDocument().getString("description").getValue(),
134135
testDocument.getString("database_name", new BsonString(getDefaultDatabaseName())).getValue(),
135-
testDocument.getArray("data"), test.asDocument(), skipTest(testDocument, test.asDocument())});
136+
testDocument.getArray("data"), test.asDocument(), skipTest(testDocument, test.asDocument(), getServerVersion())});
136137
}
137138
}
138139
return data;

driver-embedded/src/test/functional/com/mongodb/embedded/client/Fixture.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,7 @@ static String getDefaultDatabaseName() {
8383
return DEFAULT_DATABASE_NAME;
8484
}
8585

86-
static boolean serverVersionLessThan(final String versionString) {
87-
return getServerVersion().compareTo(new ServerVersion(getVersionList(versionString).subList(0, 3))) < 0;
88-
}
89-
90-
static boolean serverVersionGreaterThan(final String versionString) {
91-
return getServerVersion().compareTo(new ServerVersion(getVersionList(versionString).subList(0, 3))) > 0;
92-
}
93-
94-
private static synchronized ServerVersion getServerVersion() {
86+
static synchronized ServerVersion getServerVersion() {
9587
if (serverVersion == null) {
9688
BsonDocument buildInfoResult = getMongoClient().getDatabase("admin")
9789
.runCommand(new Document("buildInfo", 1), BsonDocument.class);

0 commit comments

Comments
 (0)