Skip to content

Commit

Permalink
Introduce a simple deterministic reproducer
Browse files Browse the repository at this point in the history
  • Loading branch information
stIncMale committed Jul 9, 2024
1 parent 3f6bca4 commit 5105484
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -300,24 +299,6 @@ void testLimitWithGetMore() {
assertTrue(cursor.isClosed());
}

@Test
void attemptJava5516() {
BsonDocument commandResult = executeFindCommand(5, 2);
cursor = new AsyncCommandBatchCursor<>(commandResult, 2, 0, DOCUMENT_DECODER,
null, connectionSource, connection);
assertNotNull(cursorNext());
// Calling `close` twice is the key to reproducing.
// It does not matter whether we call `close` twice from the same thread or not.
ForkJoinPool.commonPool().execute(() -> cursor.close());
ForkJoinPool.commonPool().execute(() -> cursor.close());
try {
assertNotNull(cursorNext());
assertNotNull(cursorNext());
} catch (IllegalStateException e) {
// one of the expected outcomes, because we call `cursorNext` concurrently with `close`
}
}

@Test
@DisplayName("test limit with large documents")
void testLimitWithLargeDocuments() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal.operation;

import com.mongodb.MongoNamespace;
import com.mongodb.ServerCursor;
import com.mongodb.internal.binding.AsyncConnectionSource;
import com.mongodb.internal.binding.ReferenceCounted;
import com.mongodb.internal.connection.Connection;
import com.mongodb.internal.mockito.MongoMockito;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.when;

final class CursorResourceManagerTest {
@Test
void doubleCloseExecutedConcurrentlyWithOperationBeingInProgressShouldNotFail() {
CursorResourceManager<?, ?> cursorResourceManager = new CursorResourceManager<ReferenceCounted, ReferenceCounted>(
new MongoNamespace("db", "coll"),
MongoMockito.mock(AsyncConnectionSource.class, mock -> {
when(mock.retain()).thenReturn(mock);
when(mock.release()).thenReturn(1);
}),
null,
MongoMockito.mock(ServerCursor.class)) {
@Override
void markAsPinned(final ReferenceCounted connectionToPin, final Connection.PinningMode pinningMode) {
}

@Override
void doClose() {
}
};
cursorResourceManager.tryStartOperation();
try {
assertDoesNotThrow(() -> {
cursorResourceManager.close();
cursorResourceManager.close();
cursorResourceManager.setServerCursor(null);
});
} finally {
cursorResourceManager.endOperation();
}
}
}

0 comments on commit 5105484

Please sign in to comment.