|
| 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