use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.
the class MongoDBTransactionManagerIT method testDeletionUpdate.
@Test
void testDeletionUpdate() throws InterruptedException, ExecutionException, TimeoutException {
String testName = "test_deletion_update";
MongoDatabase mongoDatabase = mongoClient.getDatabase(testName);
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(testName);
MongoDBTransactionManager transactionManager = new TestTransactionManager(mongoClient);
String id1 = "test1";
String value1 = "test1";
String id2 = "test2";
String value2 = "test2";
mongoCollection.insertOne(new Document().append(DOCUMENT_ID, id1).append(TEST_KEY, value1));
mongoCollection.insertOne(new Document().append(DOCUMENT_ID, id2).append(TEST_KEY, value2));
ExecutorService service = Executors.newFixedThreadPool(TEST_THREADS);
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
Future<?> execution1 = service.submit(() -> {
try {
transactionManager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
MongoCollection<Document> mongoCollection1 = mongoDatabase.getCollection(testName);
mongoCollection1.deleteOne(transactionManager.getClientSession(), Filters.eq(DOCUMENT_ID, id1));
List<Document> values1 = new ArrayList<>();
try (MongoCursor<Document> cursor = mongoCollection1.find(transactionManager.getClientSession()).iterator()) {
while (cursor.hasNext()) {
values1.add(cursor.next());
}
assertEquals(1, values1.size());
assertTrue(values1.stream().allMatch(v -> id2.equals(v.get(DOCUMENT_ID).toString())));
}
Document value = mongoCollection1.find(transactionManager.getClientSession(), Filters.eq(DOCUMENT_ID, id2)).first();
assertEquals(new Document().append(DOCUMENT_ID, id2).append(TEST_KEY, value2), value);
} finally {
latch1.countDown();
assertTrue(latch2.await(10, TimeUnit.SECONDS));
}
transactionManager.onAfterEndEvent(new UnitOfWorkEndEvent(null));
return null;
});
Future<?> execution2 = service.submit(() -> {
try {
transactionManager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
MongoCollection<Document> mongoCollection2 = mongoDatabase.getCollection(testName);
mongoCollection2.replaceOne(transactionManager.getClientSession(), Filters.eq(DOCUMENT_ID, id2), new Document().append(DOCUMENT_ID, id2).append(TEST_KEY, value1));
Document values2 = mongoCollection2.find(transactionManager.getClientSession(), Filters.eq(DOCUMENT_ID, id2)).first();
assertEquals(new Document().append(DOCUMENT_ID, id2).append(TEST_KEY, value1), values2);
int size2 = (int) mongoCollection2.countDocuments(transactionManager.getClientSession());
assertEquals(2, size2);
} finally {
latch2.countDown();
assertTrue(latch1.await(10, TimeUnit.SECONDS));
}
transactionManager.onAfterEndEvent(new UnitOfWorkEndEvent(null));
return null;
});
execution1.get(10, TimeUnit.SECONDS);
execution2.get(10, TimeUnit.SECONDS);
assertEquals(1, mongoCollection.countDocuments());
assertEquals(value1, Objects.requireNonNull(mongoCollection.find(Filters.eq(DOCUMENT_ID, id2)).first()).getString(TEST_KEY));
}
use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.
the class MongoDBTransactionManagerIT method testAbort.
@Test
void testAbort() {
String testName = "test_abort";
MongoDatabase mongoDatabase = mongoClient.getDatabase(testName);
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(testName);
mongoCollection.insertOne(new Document().append(DOCUMENT_ID, "test0"));
MongoDBTransactionManager transactionManager = new TestTransactionManager(mongoClient);
String id1 = "test1";
String value1 = "test1";
transactionManager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
mongoCollection.insertOne(transactionManager.getClientSession(), new Document().append(DOCUMENT_ID, id1).append(TEST_KEY, value1));
Document result = mongoCollection.find(transactionManager.getClientSession(), Filters.eq(DOCUMENT_ID, id1)).first();
assertEquals(new Document().append(DOCUMENT_ID, id1).append(TEST_KEY, value1), result);
int size1 = (int) mongoCollection.countDocuments(transactionManager.getClientSession());
assertEquals(2, size1);
int size2 = (int) mongoCollection.countDocuments();
assertEquals(1, size2);
transactionManager.onAfterAbortEvent(new UnitOfWorkAbortEvent(null));
int size3 = (int) mongoCollection.countDocuments();
assertEquals(1, size3);
}
use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.
the class MongoDBTransactionManagerTest method onAfterAbortEvent.
@Test
void onAfterAbortEvent() {
manager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
manager.onAfterAbortEvent(new UnitOfWorkAbortEvent(null));
verify(clientSession, times(1)).abortTransaction();
verify(clientSession, times(1)).close();
}
use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.
the class MongoDBTransactionManagerTest method getClientSession.
@Test
void getClientSession() {
assertNull(manager.getClientSession());
manager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
assertEquals(clientSession, manager.getClientSession());
}
use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.
the class MongoDBProcessInstancesIT method testFindByIdReadModeWithTransaction.
@Test
void testFindByIdReadModeWithTransaction() {
AbstractTransactionManager transactionManager = new AbstractTransactionManager(mongoClient, true) {
};
transactionManager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
testFindByIdReadMode(transactionManager);
transactionManager.onAfterEndEvent(new UnitOfWorkEndEvent(null));
}
Aggregations