Skip to content

Commit a62621d

Browse files
committed
Ensure ByteBufBsonDocument.createList releases ByteBufs
JAVA-2839
1 parent 84a32dc commit a62621d

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

driver-core/src/main/com/mongodb/internal/connection/ByteBufBsonDocument.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.bson.BsonReader;
2323
import org.bson.BsonType;
2424
import org.bson.ByteBuf;
25+
import org.bson.ByteBufNIO;
2526
import org.bson.RawBsonDocument;
2627
import org.bson.codecs.BsonDocumentCodec;
2728
import org.bson.codecs.DecoderContext;
@@ -51,7 +52,8 @@ static List<ByteBufBsonDocument> createList(final ResponseBuffers responseBuffer
5152
documentsBuffer.position(documentsBuffer.position() - 4);
5253
ByteBuf documentBuffer = documentsBuffer.duplicate();
5354
documentBuffer.limit(documentBuffer.position() + documentSizeInBytes);
54-
documents.add(new ByteBufBsonDocument(documentBuffer));
55+
documents.add(new ByteBufBsonDocument(new ByteBufNIO(documentBuffer.asNIO())));
56+
documentBuffer.release();
5557
documentsBuffer.position(documentsBuffer.position() + documentSizeInBytes);
5658
}
5759
return documents;

driver-core/src/test/functional/com/mongodb/internal/connection/QueryProtocolCommandEventSpecification.groovy

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.mongodb.internal.connection
1919
import com.mongodb.MongoQueryException
2020
import com.mongodb.OperationFunctionalSpecification
2121
import com.mongodb.client.model.CreateCollectionOptions
22+
import com.mongodb.connection.AsynchronousSocketChannelStreamFactory
2223
import com.mongodb.connection.ClusterId
2324
import com.mongodb.connection.ServerId
2425
import com.mongodb.connection.SocketSettings
@@ -35,28 +36,38 @@ import org.bson.BsonInt64
3536
import org.bson.BsonString
3637
import org.bson.BsonTimestamp
3738
import org.bson.codecs.BsonDocumentCodec
39+
import spock.lang.IgnoreIf
3840
import spock.lang.Shared
3941

40-
import static org.bson.BsonDocument.parse
4142
import static com.mongodb.ClusterFixture.getPrimary
4243
import static com.mongodb.ClusterFixture.getSslSettings
44+
import static com.mongodb.ClusterFixture.isNotAtLeastJava7
4345
import static com.mongodb.connection.ConnectionFixture.getCredentialListWithCache
4446
import static com.mongodb.internal.connection.ProtocolTestHelper.execute
47+
import static org.bson.BsonDocument.parse
4548

46-
49+
@IgnoreIf({ isNotAtLeastJava7() })
4750
class QueryProtocolCommandEventSpecification extends OperationFunctionalSpecification {
4851
@Shared
49-
InternalStreamConnection connection;
52+
InternalStreamConnection nettyConnection
53+
@Shared
54+
InternalStreamConnection nioConnection
5055

5156
def setupSpec() {
52-
connection = new InternalStreamConnectionFactory(new NettyStreamFactory(SocketSettings.builder().build(), getSslSettings()),
57+
nettyConnection = new InternalStreamConnectionFactory(new NettyStreamFactory(SocketSettings.builder().build(), getSslSettings()),
5358
getCredentialListWithCache(), null, null, [], null)
5459
.create(new ServerId(new ClusterId(), getPrimary()))
55-
connection.open();
60+
nettyConnection.open()
61+
62+
nioConnection = new InternalStreamConnectionFactory(new AsynchronousSocketChannelStreamFactory(SocketSettings.builder().build(),
63+
getSslSettings()), getCredentialListWithCache(), null, null, [], null)
64+
.create(new ServerId(new ClusterId(), getPrimary()))
65+
nioConnection.open()
5666
}
5767

5868
def cleanupSpec() {
59-
connection?.close()
69+
nettyConnection?.close()
70+
nioConnection?.close()
6071
}
6172

6273
def 'should deliver start and completed command events with numberToReturn'() {
@@ -93,7 +104,7 @@ class QueryProtocolCommandEventSpecification extends OperationFunctionalSpecific
93104
new CommandSucceededEvent(1, connection.getDescription(), 'find', response, 0)])
94105

95106
where:
96-
async << [false, true]
107+
[async, connection] << [[false, true], [nettyConnection, nioConnection]].combinations()
97108
}
98109

99110
def 'should deliver start and completed command events with limit and batchSize'() {
@@ -134,7 +145,7 @@ class QueryProtocolCommandEventSpecification extends OperationFunctionalSpecific
134145
new CommandSucceededEvent(1, connection.getDescription(), 'find', response, 0)])
135146

136147
where:
137-
async << [false, true]
148+
[async, connection] << [[false, true], [nettyConnection, nioConnection]].combinations()
138149
}
139150

140151
def 'should deliver start and completed command events when there is no projection'() {
@@ -164,7 +175,7 @@ class QueryProtocolCommandEventSpecification extends OperationFunctionalSpecific
164175
new CommandSucceededEvent(1, connection.getDescription(), 'find', response, 0)])
165176

166177
where:
167-
async << [false, true]
178+
[async, connection] << [[false, true], [nettyConnection, nioConnection]].combinations()
168179
}
169180

170181
def 'should deliver start and completed command events when there are boolean options'() {
@@ -206,7 +217,7 @@ class QueryProtocolCommandEventSpecification extends OperationFunctionalSpecific
206217
new CommandSucceededEvent(1, connection.getDescription(), 'find', response, 0)])
207218

208219
where:
209-
async << [false, true]
220+
[async, connection] << [[false, true], [nettyConnection, nioConnection]].combinations()
210221
}
211222

212223
def 'should deliver start and completed command events with meta operators'() {
@@ -279,7 +290,7 @@ class QueryProtocolCommandEventSpecification extends OperationFunctionalSpecific
279290
new CommandSucceededEvent(1, connection.getDescription(), 'find', response, 0)])
280291

281292
where:
282-
async << [false, true]
293+
[async, connection] << [[false, true], [nettyConnection, nioConnection]].combinations()
283294
}
284295

285296
def 'should deliver start and completed command events for an explain command when there is a $explain meta operator'() {
@@ -312,7 +323,7 @@ class QueryProtocolCommandEventSpecification extends OperationFunctionalSpecific
312323
new CommandSucceededEvent(1, connection.getDescription(), 'explain', expectedResponse, 0)])
313324

314325
where:
315-
async << [false, true]
326+
[async, connection] << [[false, true], [nettyConnection, nioConnection]].combinations()
316327
}
317328

318329
def 'should deliver start and failed command events'() {
@@ -338,6 +349,6 @@ class QueryProtocolCommandEventSpecification extends OperationFunctionalSpecific
338349
new CommandFailedEvent(1, connection.getDescription(), 'find', 0, e)])
339350

340351
where:
341-
async << [false, true]
352+
[async, connection] << [[false, true], [nettyConnection, nioConnection]].combinations()
342353
}
343354
}

0 commit comments

Comments
 (0)