use of org.kie.kogito.uow.events.UnitOfWorkEndEvent 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.UnitOfWorkEndEvent 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));
}
use of org.kie.kogito.uow.events.UnitOfWorkEndEvent in project kogito-runtimes by kiegroup.
the class MongoDBProcessInstancesIT method testValuesReadModeWithTransaction.
@Test
void testValuesReadModeWithTransaction() {
AbstractTransactionManager transactionManager = new AbstractTransactionManager(mongoClient, true) {
};
transactionManager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
testValuesReadMode(transactionManager);
transactionManager.onAfterEndEvent(new UnitOfWorkEndEvent(null));
}
use of org.kie.kogito.uow.events.UnitOfWorkEndEvent in project kogito-runtimes by kiegroup.
the class AbstractTransactionManagerIT 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);
AbstractTransactionManager 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.UnitOfWorkEndEvent in project kogito-runtimes by kiegroup.
the class MongoDBTransactionManagerIT method testInsertion.
@Test
void testInsertion() throws InterruptedException, ExecutionException, TimeoutException {
String testName = "test_insertion";
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";
String id2 = "test2";
String value2 = "test2";
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.insertOne(transactionManager.getClientSession(), new Document().append(DOCUMENT_ID, id1).append(TEST_KEY, value1));
Document result1 = mongoCollection1.find(transactionManager.getClientSession(), Filters.eq(DOCUMENT_ID, id1)).first();
assertEquals(new Document().append(DOCUMENT_ID, id1).append(TEST_KEY, value1), result1);
int size1 = (int) mongoCollection1.countDocuments(transactionManager.getClientSession());
assertEquals(2, size1);
} 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.insertOne(transactionManager.getClientSession(), new Document().append(DOCUMENT_ID, id2).append(TEST_KEY, value2));
Document result2 = mongoCollection2.find(transactionManager.getClientSession(), Filters.eq(DOCUMENT_ID, id2)).first();
assertEquals(new Document().append(DOCUMENT_ID, id2).append(TEST_KEY, value2), result2);
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(3, mongoCollection.countDocuments());
}
Aggregations