Search in sources :

Example 1 with UnitOfWorkStartEvent

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));
}
Also used : Document(org.bson.Document) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) TimeoutException(java.util.concurrent.TimeoutException) MongoDatabase(com.mongodb.client.MongoDatabase) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) Filters(com.mongodb.client.model.Filters) Future(java.util.concurrent.Future) UnitOfWorkAbortEvent(org.kie.kogito.uow.events.UnitOfWorkAbortEvent) MongoCursor(com.mongodb.client.MongoCursor) BeforeAll(org.junit.jupiter.api.BeforeAll) UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) Container(org.testcontainers.junit.jupiter.Container) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) MongoClients(com.mongodb.client.MongoClients) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) KogitoMongoDBContainer(org.kie.kogito.testcontainers.KogitoMongoDBContainer) UnitOfWorkEndEvent(org.kie.kogito.uow.events.UnitOfWorkEndEvent) ArrayList(java.util.ArrayList) Document(org.bson.Document) CountDownLatch(java.util.concurrent.CountDownLatch) UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) ExecutorService(java.util.concurrent.ExecutorService) UnitOfWorkEndEvent(org.kie.kogito.uow.events.UnitOfWorkEndEvent) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Example 2 with UnitOfWorkStartEvent

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);
}
Also used : UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) Document(org.bson.Document) UnitOfWorkAbortEvent(org.kie.kogito.uow.events.UnitOfWorkAbortEvent) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Example 3 with UnitOfWorkStartEvent

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();
}
Also used : UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) UnitOfWorkAbortEvent(org.kie.kogito.uow.events.UnitOfWorkAbortEvent) Test(org.junit.jupiter.api.Test)

Example 4 with UnitOfWorkStartEvent

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());
}
Also used : UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) Test(org.junit.jupiter.api.Test)

Example 5 with UnitOfWorkStartEvent

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));
}
Also used : UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) UnitOfWorkEndEvent(org.kie.kogito.uow.events.UnitOfWorkEndEvent) AbstractTransactionManager(org.kie.kogito.mongodb.transaction.AbstractTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)21 UnitOfWorkStartEvent (org.kie.kogito.uow.events.UnitOfWorkStartEvent)21 UnitOfWorkEndEvent (org.kie.kogito.uow.events.UnitOfWorkEndEvent)11 UnitOfWorkAbortEvent (org.kie.kogito.uow.events.UnitOfWorkAbortEvent)8 MongoDatabase (com.mongodb.client.MongoDatabase)6 ExecutorService (java.util.concurrent.ExecutorService)6 Document (org.bson.Document)6 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Future (java.util.concurrent.Future)4 AbstractTransactionManager (org.kie.kogito.mongodb.transaction.AbstractTransactionManager)3 MongoClient (com.mongodb.client.MongoClient)2 MongoClients (com.mongodb.client.MongoClients)2 MongoCollection (com.mongodb.client.MongoCollection)2 MongoCursor (com.mongodb.client.MongoCursor)2 Filters (com.mongodb.client.model.Filters)2 List (java.util.List)2 Objects (java.util.Objects)2 ExecutionException (java.util.concurrent.ExecutionException)2 Executors (java.util.concurrent.Executors)2