Skip to content

Commit 251041b

Browse files
authored
Fix CursorResourceManager.close (#1440) (#1447)
JAVA-5516
1 parent a8b686d commit 251041b

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

driver-core/src/main/com/mongodb/internal/operation/CursorResourceManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void endOperation() {
182182
void close() {
183183
boolean doClose = withLock(lock, () -> {
184184
State localState = state;
185-
if (localState == State.OPERATION_IN_PROGRESS) {
185+
if (localState.inProgress()) {
186186
state = State.CLOSE_PENDING;
187187
} else if (localState != State.CLOSED) {
188188
state = State.CLOSED;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
package com.mongodb.internal.operation;
17+
18+
import com.mongodb.MongoNamespace;
19+
import com.mongodb.ServerCursor;
20+
import com.mongodb.internal.binding.AsyncConnectionSource;
21+
import com.mongodb.internal.binding.ReferenceCounted;
22+
import com.mongodb.internal.connection.Connection;
23+
import com.mongodb.internal.mockito.MongoMockito;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
27+
import static org.mockito.Mockito.when;
28+
29+
final class CursorResourceManagerTest {
30+
@Test
31+
void doubleCloseExecutedConcurrentlyWithOperationBeingInProgressShouldNotFail() {
32+
CursorResourceManager<?, ?> cursorResourceManager = new CursorResourceManager<ReferenceCounted, ReferenceCounted>(
33+
new MongoNamespace("db", "coll"),
34+
MongoMockito.mock(AsyncConnectionSource.class, mock -> {
35+
when(mock.retain()).thenReturn(mock);
36+
when(mock.release()).thenReturn(1);
37+
}),
38+
null,
39+
MongoMockito.mock(ServerCursor.class)) {
40+
@Override
41+
void markAsPinned(final ReferenceCounted connectionToPin, final Connection.PinningMode pinningMode) {
42+
}
43+
44+
@Override
45+
void doClose() {
46+
}
47+
};
48+
cursorResourceManager.tryStartOperation();
49+
try {
50+
assertDoesNotThrow(() -> {
51+
cursorResourceManager.close();
52+
cursorResourceManager.close();
53+
cursorResourceManager.setServerCursor(null);
54+
});
55+
} finally {
56+
cursorResourceManager.endOperation();
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)