|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.internal.async.client; |
| 18 | + |
| 19 | +import com.mongodb.MongoException; |
| 20 | +import com.mongodb.MongoNamespace; |
| 21 | +import com.mongodb.MongoWriteConcernException; |
| 22 | +import com.mongodb.async.FutureResultCallback; |
| 23 | +import com.mongodb.client.result.InsertOneResult; |
| 24 | +import com.mongodb.client.test.CollectionHelper; |
| 25 | +import org.bson.BsonArray; |
| 26 | +import org.bson.BsonDocument; |
| 27 | +import org.bson.BsonInt32; |
| 28 | +import org.bson.BsonString; |
| 29 | +import org.bson.Document; |
| 30 | +import org.bson.codecs.DocumentCodec; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | + |
| 36 | +import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet; |
| 37 | +import static com.mongodb.ClusterFixture.serverVersionAtLeast; |
| 38 | +import static com.mongodb.internal.async.client.Fixture.getDefaultDatabaseName; |
| 39 | +import static java.lang.String.format; |
| 40 | +import static java.util.Arrays.asList; |
| 41 | +import static org.junit.Assert.assertEquals; |
| 42 | +import static org.junit.Assert.assertTrue; |
| 43 | +import static org.junit.Assert.fail; |
| 44 | +import static org.junit.Assume.assumeTrue; |
| 45 | + |
| 46 | +// See https://github.com/mongodb/specifications/tree/master/source/change-streams/tests/README.rst#prose-tests |
| 47 | +public class WriteConcernProseTest extends DatabaseTestCase { |
| 48 | + private BsonDocument failPointDocument; |
| 49 | + private CollectionHelper<Document> collectionHelper; |
| 50 | + |
| 51 | + @Before |
| 52 | + @Override |
| 53 | + public void setUp() { |
| 54 | + assumeTrue(canRunTests()); |
| 55 | + super.setUp(); |
| 56 | + collectionHelper = new CollectionHelper<>(new DocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), "test")); |
| 57 | + } |
| 58 | + |
| 59 | + // Ensure that the WriteConcernError errInfo object is propagated. |
| 60 | + @Test |
| 61 | + public void testWriteConcernErrInfoIsPropagated() { |
| 62 | + try { |
| 63 | + setFailPoint(); |
| 64 | + insertOneDocument(); |
| 65 | + } catch (MongoWriteConcernException e) { |
| 66 | + assertEquals(e.getWriteConcernError().getCode(), 100); |
| 67 | + assertTrue(e.getWriteConcernError().getCodeName().equals("UnsatisfiableWriteConcern")); |
| 68 | + assertEquals(e.getWriteConcernError().getDetails(), new BsonDocument("writeConcern", |
| 69 | + new BsonDocument("w", new BsonInt32(2)) |
| 70 | + .append("wtimeout", new BsonInt32(0)) |
| 71 | + .append("provenance", new BsonString("clientSupplied")))); |
| 72 | + } catch (Exception ex) { |
| 73 | + fail(format("Incorrect exception thrown in test: %s" + ex.getClass())); |
| 74 | + } finally { |
| 75 | + disableFailPoint(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void insertOneDocument() { |
| 80 | + FutureResultCallback<InsertOneResult> callback = new FutureResultCallback<>(); |
| 81 | + collection.insertOne(Document.parse("{ x: 1 }"), callback); |
| 82 | + callback.get(); |
| 83 | + } |
| 84 | + |
| 85 | + private void setFailPoint() { |
| 86 | + failPointDocument = new BsonDocument("configureFailPoint", new BsonString("failCommand")) |
| 87 | + .append("mode", new BsonDocument("times", new BsonInt32(1))) |
| 88 | + .append("data", new BsonDocument("failCommands", new BsonArray(asList(new BsonString("insert")))) |
| 89 | + .append("writeConcernError", new BsonDocument("code", new BsonInt32(100)) |
| 90 | + .append("codeName", new BsonString("UnsatisfiableWriteConcern")) |
| 91 | + .append("errmsg", new BsonString("Not enough data-bearing nodes")) |
| 92 | + .append("errInfo", new BsonDocument("writeConcern", new BsonDocument("w", new BsonInt32(2)) |
| 93 | + .append("wtimeout", new BsonInt32(0)) |
| 94 | + .append("provenance", new BsonString("clientSupplied")))))); |
| 95 | + collectionHelper.runAdminCommand(failPointDocument); |
| 96 | + } |
| 97 | + |
| 98 | + private void disableFailPoint() { |
| 99 | + collectionHelper.runAdminCommand(failPointDocument.append("mode", new BsonString("off"))); |
| 100 | + } |
| 101 | + |
| 102 | + private boolean canRunTests() { |
| 103 | + return isDiscoverableReplicaSet() && serverVersionAtLeast(4, 0); |
| 104 | + } |
| 105 | +} |
0 commit comments