Search in sources :

Example 11 with UnitOfWorkStartEvent

use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.

the class MongoDBTransactionManagerTest method enabled.

@Test
void enabled() {
    MongoDBTransactionManager manager = new MongoDBTransactionManager(mongoClient) {

        @Override
        public boolean enabled() {
            return false;
        }
    };
    manager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
    manager.onAfterAbortEvent(new UnitOfWorkAbortEvent(null));
    verify(mongoClient, never()).startSession();
}
Also used : UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) UnitOfWorkAbortEvent(org.kie.kogito.uow.events.UnitOfWorkAbortEvent) Test(org.junit.jupiter.api.Test)

Example 12 with UnitOfWorkStartEvent

use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.

the class MongoDBTransactionManagerTest method onBeforeStartEvent.

@Test
void onBeforeStartEvent() {
    manager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
    verify(clientSession, times(1)).startTransaction(any());
}
Also used : UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) Test(org.junit.jupiter.api.Test)

Example 13 with UnitOfWorkStartEvent

use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.

the class MongoDBTransactionManagerTest method test.

@Test
void test() throws InterruptedException, ExecutionException, TimeoutException {
    ExecutorService service = Executors.newFixedThreadPool(TEST_THREADS);
    List<Future<?>> results = new ArrayList<>();
    for (int i = 0; i < TEST_THREADS; i++) {
        results.add(service.submit(() -> {
            manager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
            manager.onAfterEndEvent(new UnitOfWorkEndEvent(null));
        }));
    }
    for (Future<?> result : results) {
        result.get(2L, TimeUnit.SECONDS);
    }
    verify(mongoClient, times(TEST_THREADS)).startSession();
    verify(clientSession, times(TEST_THREADS)).startTransaction(any());
    verify(clientSession, times(TEST_THREADS)).commitTransaction();
    verify(clientSession, times(TEST_THREADS)).close();
}
Also used : UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) UnitOfWorkEndEvent(org.kie.kogito.uow.events.UnitOfWorkEndEvent) Test(org.junit.jupiter.api.Test)

Example 14 with UnitOfWorkStartEvent

use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.

the class MongoDBProcessInstancesIT method testWithTransaction.

@Test
void testWithTransaction() {
    AbstractTransactionManager transactionManager = new AbstractTransactionManager(mongoClient, true) {
    };
    transactionManager.onBeforeStartEvent(new UnitOfWorkStartEvent(null));
    test(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)

Example 15 with UnitOfWorkStartEvent

use of org.kie.kogito.uow.events.UnitOfWorkStartEvent in project kogito-runtimes by kiegroup.

the class AbstractTransactionManagerIT 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"));
    AbstractTransactionManager 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());
}
Also used : UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) ExecutorService(java.util.concurrent.ExecutorService) UnitOfWorkEndEvent(org.kie.kogito.uow.events.UnitOfWorkEndEvent) Document(org.bson.Document) CountDownLatch(java.util.concurrent.CountDownLatch) MongoDatabase(com.mongodb.client.MongoDatabase) 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