Skip to content

Instantly share code, notes, and snippets.

@jyemin
Created December 4, 2015 14:58
Show Gist options
  • Save jyemin/139b7bc2a94d49f5903f to your computer and use it in GitHub Desktop.
Save jyemin/139b7bc2a94d49f5903f to your computer and use it in GitHub Desktop.
Test demonstrating that asynchronous operations do not block even if the number of active operations exceeds the maximum pool size.
/*
* Copyright 2015 MongoDB Inc. <http://10gen.com>
*
* 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.
*
*/
import com.mongodb.ServerAddress;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoClient;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.connection.ConnectionPoolSettings;
import com.mongodb.connection.SocketSettings;
import org.bson.Document;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static java.util.Collections.singletonList;
public class AsyncIsNotBlockingTest {
public static void main(String[] args) throws InterruptedException {
MongoClient client = MongoClients.create(MongoClientSettings.builder()
.clusterSettings(ClusterSettings.builder()
.hosts(singletonList(new ServerAddress()))
.build())
.connectionPoolSettings(ConnectionPoolSettings.builder()
.maxSize(1) // set max pool size to 1
.build())
.build());
MongoCollection<Document> collection = client.getDatabase("test")
.getCollection("AsyncIsNotBlockingTest");
final int count = 100;
CountDownLatch latch = new CountDownLatch(count);
for (int i = 0; i < count; i++) {
final int idx = i;
collection.insertOne(new Document("_id", i), (result, t) -> {
System.out.println("Inserted document " + idx);
latch.countDown();
});
}
System.out.println("Inserts all queued");
latch.await();
client.close();
System.out.println("Exiting");
}
}
@jyemin
Copy link
Author

jyemin commented Dec 4, 2015

Executing this code you should see something like this for output:

Inserts all queued
Inserted document 0
Inserted document 1
Inserted document 2
Inserted document 3
Inserted document 4
Inserted document 5
Inserted document 6
Inserted document 7
Inserted document 8
Inserted document 9
Inserted document 10
Inserted document 11
Inserted document 12
Inserted document 13
Inserted document 14
Inserted document 15
Inserted document 16
Inserted document 17
Inserted document 18
Inserted document 19
Inserted document 20
Inserted document 21
Inserted document 22
Inserted document 23
Inserted document 24
Inserted document 25
Inserted document 26
Inserted document 27
Inserted document 28
Inserted document 29
Inserted document 30
Inserted document 31
Inserted document 32
Inserted document 33
Inserted document 34
Inserted document 35
Inserted document 36
Inserted document 37
Inserted document 38
Inserted document 39
Inserted document 40
Inserted document 41
Inserted document 42
Inserted document 43
Inserted document 44
Inserted document 45
Inserted document 46
Inserted document 47
Inserted document 48
Inserted document 49
Inserted document 50
Inserted document 51
Inserted document 52
Inserted document 53
Inserted document 54
Inserted document 55
Inserted document 56
Inserted document 57
Inserted document 58
Inserted document 59
Inserted document 60
Inserted document 61
Inserted document 62
Inserted document 63
Inserted document 64
Inserted document 65
Inserted document 66
Inserted document 67
Inserted document 68
Inserted document 69
Inserted document 70
Inserted document 71
Inserted document 72
Inserted document 73
Inserted document 74
Inserted document 75
Inserted document 76
Inserted document 77
Inserted document 78
Inserted document 79
Inserted document 80
Inserted document 81
Inserted document 82
Inserted document 83
Inserted document 84
Inserted document 85
Inserted document 86
Inserted document 87
Inserted document 88
Inserted document 89
Inserted document 90
Inserted document 91
Inserted document 92
Inserted document 93
Inserted document 94
Inserted document 95
Inserted document 96
Inserted document 97
Inserted document 98
Inserted document 99
Exiting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment