Search in sources :

Example 1 with UnitOfWorkEndEvent

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));
}
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 UnitOfWorkEndEvent

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));
}
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 3 with UnitOfWorkEndEvent

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));
}
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 4 with UnitOfWorkEndEvent

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));
}
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 5 with UnitOfWorkEndEvent

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());
}
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)11 UnitOfWorkEndEvent (org.kie.kogito.uow.events.UnitOfWorkEndEvent)11 UnitOfWorkStartEvent (org.kie.kogito.uow.events.UnitOfWorkStartEvent)11 ExecutorService (java.util.concurrent.ExecutorService)6 MongoDatabase (com.mongodb.client.MongoDatabase)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Future (java.util.concurrent.Future)4 Document (org.bson.Document)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 TimeUnit (java.util.concurrent.TimeUnit)2