use of com.mongodb.reactivestreams.client.MongoDatabase in project ditto by eclipse.
the class MongoSearchUpdaterFlowTest method testThroughput.
@Test
@SuppressWarnings("unchecked")
public void testThroughput() throws Throwable {
new TestKit(actorSystem) {
{
// GIVEN: Persistence has high latency
final var latency = Duration.ofSeconds(1);
final MongoDatabase db = Mockito.mock(MongoDatabase.class);
final MongoCollection<Document> collection = Mockito.mock(MongoCollection.class);
Mockito.when(db.getCollection(any(), any(Class.class))).thenReturn(collection);
doAnswer(inv -> {
final var size = inv.<List<?>>getArgument(0).size();
final BulkWriteResult result = BulkWriteResult.acknowledged(0, size, 0, size, List.of(), List.of());
return Source.single(result).delay(latency, DelayOverflowStrategy.backpressure()).runWith(Sink.asPublisher(AsPublisher.WITHOUT_FANOUT), actorSystem);
}).when(collection).bulkWrite(any(), any(BulkWriteOptions.class));
final MongoSearchUpdaterFlow flow = MongoSearchUpdaterFlow.of(db, DefaultPersistenceStreamConfig.of(ConfigFactory.empty()), SearchUpdateMapper.get(actorSystem));
// WHEN: 25 updates go through 32 parallel streams
final int numberOfChanges = 25;
final CountDownLatch latch = new CountDownLatch(numberOfChanges);
final Sink<Source<AbstractWriteModel, NotUsed>, CompletionStage<Done>> sink = flow.start(false, 32, 1).map(writeResultAndErrors -> {
latch.countDown();
return writeResultAndErrors;
}).toMat(Sink.ignore(), Keep.right());
final Metadata metadata = Metadata.of(ThingId.of("thing:id"), 1L, PolicyId.of("policy:id"), 1L, null);
final AbstractWriteModel abstractWriteModel = ThingWriteModel.of(metadata, new BsonDocument());
final Thread testRunnerThread = Thread.currentThread();
final AtomicReference<Throwable> errorBox = new AtomicReference<>();
Source.repeat(Source.single(abstractWriteModel)).take(numberOfChanges).runWith(Objects.requireNonNull(sink), actorSystem).exceptionally(error -> {
errorBox.set(error);
testRunnerThread.interrupt();
return null;
});
try {
final var result = latch.await(20L, TimeUnit.SECONDS);
assertThat(latch.getCount()).isZero();
assertThat(result).isTrue();
} catch (final InterruptedException e) {
if (errorBox.get() != null) {
throw errorBox.get();
} else {
throw e;
}
}
}
};
}
use of com.mongodb.reactivestreams.client.MongoDatabase in project ditto by eclipse.
the class MongoClientWrapperTest method assertWithExpected.
private static void assertWithExpected(final DittoMongoClient mongoClient, final boolean sslEnabled, final boolean withCredentials) {
final MongoClientSettings mongoClientSettings = mongoClient.getClientSettings();
assertThat(mongoClientSettings.getClusterSettings().getHosts()).isEqualTo(Collections.singletonList(new ServerAddress(KNOWN_SERVER_ADDRESS)));
@Nullable final MongoCredential expectedCredential = withCredentials ? MongoCredential.createCredential(KNOWN_USER, KNOWN_DB_NAME, KNOWN_PASSWORD.toCharArray()) : null;
assertThat(mongoClientSettings.getCredential()).isEqualTo(expectedCredential);
assertThat(mongoClientSettings.getSslSettings().isEnabled()).isEqualTo(sslEnabled);
final MongoDatabase mongoDatabase = mongoClient.getDefaultDatabase();
assertThat(mongoDatabase).isNotNull();
assertThat(mongoDatabase.getName()).isEqualTo(KNOWN_DB_NAME);
}
use of com.mongodb.reactivestreams.client.MongoDatabase in project ditto by eclipse.
the class ConnectionPersistenceOperationsActor method props.
/**
* Create Props of this actor.
*
* @param pubSubMediator Akka pub-sub mediator.
* @param mongoDbConfig the MongoDB configuration settings.
* @param config configuration with info about event journal, snapshot store and database.
* @param persistenceOperationsConfig the persistence operations configuration settings.
* @return a Props object.
*/
public static Props props(final ActorRef pubSubMediator, final MongoDbConfig mongoDbConfig, final Config config, final PersistenceOperationsConfig persistenceOperationsConfig) {
return Props.create(ConnectionPersistenceOperationsActor.class, () -> {
final MongoEventSourceSettings eventSourceSettings = MongoEventSourceSettings.fromConfig(config, ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX, false, ConnectionPersistenceActor.JOURNAL_PLUGIN_ID, ConnectionPersistenceActor.SNAPSHOT_PLUGIN_ID);
final MongoClientWrapper mongoClient = MongoClientWrapper.newInstance(mongoDbConfig);
final MongoDatabase db = mongoClient.getDefaultDatabase();
final EntityPersistenceOperations entitiesOps = MongoEntitiesPersistenceOperations.of(db, eventSourceSettings);
return new ConnectionPersistenceOperationsActor(pubSubMediator, entitiesOps, mongoClient, persistenceOperationsConfig);
});
}
use of com.mongodb.reactivestreams.client.MongoDatabase in project helidon by oracle.
the class MongoDbCommandExecutor method callStatement.
private static Flow.Publisher<DbRow> callStatement(MongoDbStatement dbStatement, MongoDbStatement.MongoStatement mongoStmt, CompletableFuture<Void> statementFuture, CompletableFuture<Long> commandFuture) {
MongoDatabase db = dbStatement.db();
Document command = mongoStmt.getQuery();
LOGGER.fine(() -> String.format("Command: %s", command.toString()));
Publisher<Document> publisher = dbStatement.noTx() ? db.runCommand(command) : db.runCommand(dbStatement.txManager().tx(), command);
return new CommandRows(publisher, dbStatement, statementFuture, commandFuture).publisher();
}
use of com.mongodb.reactivestreams.client.MongoDatabase in project helidon by oracle.
the class MongoDbClientTest method testUnwrapExecutorClass.
@Test
void testUnwrapExecutorClass() {
MongoDbClient dbClient = new MongoDbClient(new MongoDbClientProviderBuilder(), CLIENT, DB);
dbClient.execute(exec -> {
Single<MongoDatabase> future = exec.unwrap(MongoDatabase.class);
MongoDatabase connection = future.await();
assertThat(connection, notNullValue());
return exec.query("{\"operation\": \"command\", \"query\": { ping: 1 }}");
});
}
Aggregations