use of com.mongodb.connection.ConnectionDescription in project mongo-java-driver by mongodb.
the class DefaultConnectionPoolTest method newControllableConnectionFactory.
private static ControllableConnectionFactory newControllableConnectionFactory(final ExecutorService asyncOpenExecutor) {
ControllableConnectionFactory.OpenDurationHandle openDurationHandle = new ControllableConnectionFactory.OpenDurationHandle();
InternalConnectionFactory connectionFactory = (serverId, connectionGenerationSupplier) -> {
InternalConnection connection = mock(InternalConnection.class, withSettings().stubOnly());
when(connection.getGeneration()).thenReturn(connectionGenerationSupplier.getGeneration());
when(connection.getDescription()).thenReturn(new ConnectionDescription(serverId));
AtomicBoolean open = new AtomicBoolean(false);
when(connection.opened()).thenAnswer(invocation -> open.get());
Runnable doOpen = () -> {
sleepMillis(openDurationHandle.getDurationAndCountDown().toMillis());
if (ThreadLocalRandom.current().nextFloat() < 0.2) {
// add a bit more randomness
sleepMillis(ThreadLocalRandom.current().nextInt(7, 15));
}
open.set(true);
};
doAnswer(invocation -> {
doOpen.run();
return null;
}).when(connection).open();
doAnswer(invocation -> {
SingleResultCallback<?> callback = invocation.getArgument(0, SingleResultCallback.class);
asyncOpenExecutor.execute(() -> {
doOpen.run();
callback.onResult(null, null);
});
return null;
}).when(connection).openAsync(any());
return connection;
};
return new ControllableConnectionFactory(connectionFactory, openDurationHandle);
}
use of com.mongodb.connection.ConnectionDescription in project mongo-java-driver by mongodb.
the class PlainAuthenticatorTest method setUp.
@Before
public void setUp() {
String host = System.getProperty("org.mongodb.test.host");
userName = System.getProperty("org.mongodb.test.userName");
source = System.getProperty("org.mongod.test.source");
password = System.getProperty("org.mongodb.test.password");
internalConnection = new InternalStreamConnectionFactory(ClusterConnectionMode.SINGLE, streamFactory, null, null, null, Collections.<MongoCompressor>emptyList(), null, getServerApi()).create(new ServerId(new ClusterId(), new ServerAddress(host)));
connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
}
use of com.mongodb.connection.ConnectionDescription in project mongo-java-driver by mongodb.
the class InternalStreamConnectionInitializer method createInitializationDescription.
private InternalConnectionInitializationDescription createInitializationDescription(final BsonDocument helloResult, final InternalConnection internalConnection, final long startTime) {
ConnectionId connectionId = internalConnection.getDescription().getConnectionId();
ConnectionDescription connectionDescription = createConnectionDescription(clusterConnectionMode, connectionId, helloResult);
ServerDescription serverDescription = createServerDescription(internalConnection.getDescription().getServerAddress(), helloResult, System.nanoTime() - startTime);
return new InternalConnectionInitializationDescription(connectionDescription, serverDescription);
}
use of com.mongodb.connection.ConnectionDescription in project mongo-java-driver by mongodb.
the class DescriptionHelper method createConnectionDescription.
static ConnectionDescription createConnectionDescription(final ClusterConnectionMode clusterConnectionMode, final ConnectionId connectionId, final BsonDocument helloResult) {
ConnectionDescription connectionDescription = new ConnectionDescription(connectionId, getMaxWireVersion(helloResult), getServerType(helloResult), getMaxWriteBatchSize(helloResult), getMaxBsonObjectSize(helloResult), getMaxMessageSizeBytes(helloResult), getCompressors(helloResult), helloResult.getArray("saslSupportedMechs", null));
if (helloResult.containsKey("connectionId")) {
ConnectionId newConnectionId = connectionDescription.getConnectionId().withServerValue(helloResult.getNumber("connectionId").intValue());
connectionDescription = connectionDescription.withConnectionId(newConnectionId);
}
if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {
ObjectId serviceId = getServiceId(helloResult);
if (serviceId != null) {
connectionDescription = connectionDescription.withServiceId(serviceId);
} else {
throw new MongoClientException("Driver attempted to initialize in load balancing mode, but the server does not support " + "this mode");
}
}
return connectionDescription;
}
use of com.mongodb.connection.ConnectionDescription in project mongo-java-driver by mongodb.
the class X509AuthenticatorNoUserNameTest method before.
@Before
public void before() {
connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
connectionDescriptionThreeTwo = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), 4, ServerType.STANDALONE, 1000, 16000, 48000, Collections.<String>emptyList());
connectionDescriptionThreeFour = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), 5, ServerType.STANDALONE, 1000, 16000, 48000, Collections.<String>emptyList());
}
Aggregations