From d32915cf3facee92d789950b5e52a018f3759bbc Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Thu, 20 Apr 2023 11:10:26 +0200 Subject: [PATCH] chore: remove autocommit, fix compilation --- conf/application.conf | 4 ++ .../com/github/polpetta/mezzotre/AppDI.java | 8 +--- .../orm/CallbackQueryContextCleaner.java | 3 ++ .../github/polpetta/mezzotre/orm/OrmDI.java | 2 +- .../polpetta/mezzotre/helper/Loader.java | 1 + ...ackQueryContextCleanerIntegrationTest.java | 47 +++++++++---------- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/conf/application.conf b/conf/application.conf index dfa8f8e..3d8fa39 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -3,6 +3,10 @@ db.url = "jdbc:postgresql://localhost:5433/example" db.user = example db.password = example + +hikari.autoCommit = false +hikari.maximumPoolSize = 4 + telegram.key = akey application.lang = en en-US it it-IT diff --git a/src/main/java/com/github/polpetta/mezzotre/AppDI.java b/src/main/java/com/github/polpetta/mezzotre/AppDI.java index 15d945f..811ca05 100644 --- a/src/main/java/com/github/polpetta/mezzotre/AppDI.java +++ b/src/main/java/com/github/polpetta/mezzotre/AppDI.java @@ -6,10 +6,8 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; import io.jooby.Jooby; import java.util.List; -import java.util.concurrent.TimeUnit; import javax.inject.Named; import javax.inject.Singleton; -import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; public class AppDI extends AbstractModule { @@ -37,9 +35,7 @@ public class AppDI extends AbstractModule { @Provides @Singleton @Named("services") - public List getApplicationServices( - Logger logger, - @Named("serviceRunningCheckTime") Pair serviceRunningCheckTime) { - return List.of(new BatchBeanCleanerService(logger, serviceRunningCheckTime)); + public List getApplicationServices(BatchBeanCleanerService batchBeanCleanerService) { + return List.of(batchBeanCleanerService); } } diff --git a/src/main/java/com/github/polpetta/mezzotre/orm/CallbackQueryContextCleaner.java b/src/main/java/com/github/polpetta/mezzotre/orm/CallbackQueryContextCleaner.java index 677a6b1..9603653 100644 --- a/src/main/java/com/github/polpetta/mezzotre/orm/CallbackQueryContextCleaner.java +++ b/src/main/java/com/github/polpetta/mezzotre/orm/CallbackQueryContextCleaner.java @@ -2,6 +2,7 @@ package com.github.polpetta.mezzotre.orm; import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; import com.github.polpetta.mezzotre.orm.model.query.QCallbackQueryContext; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import io.ebean.typequery.PString; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; @@ -44,6 +45,7 @@ public class CallbackQueryContextCleaner { * been deleted from the persistence layer * @see CallbackQueryContext#getEntryGroup() */ + @CanIgnoreReturnValue public CompletableFuture removeGroupAsync(String id) { log.trace("CallbackQueryContext entry group " + id + " queued for removal"); return batchBeanCleanerService.removeAsync(id, ENTRY_GROUP.get()); @@ -57,6 +59,7 @@ public class CallbackQueryContextCleaner { * been deleted from the persistence layer. Can be 0 or 1. * @see CallbackQueryContext#getId() */ + @CanIgnoreReturnValue public CompletableFuture removeIdAsync(String id) { log.trace("CallbackQueryContext single entity " + id + " queued for removal"); return batchBeanCleanerService.removeAsync(id, SINGLE_ENTRY.get()); diff --git a/src/main/java/com/github/polpetta/mezzotre/orm/OrmDI.java b/src/main/java/com/github/polpetta/mezzotre/orm/OrmDI.java index 48b1e87..62e2d70 100644 --- a/src/main/java/com/github/polpetta/mezzotre/orm/OrmDI.java +++ b/src/main/java/com/github/polpetta/mezzotre/orm/OrmDI.java @@ -2,13 +2,13 @@ package com.github.polpetta.mezzotre.orm; import com.google.inject.AbstractModule; import com.google.inject.Provides; -import com.google.inject.Singleton; import com.zaxxer.hikari.HikariConfig; import io.jooby.Extension; import io.jooby.ebean.EbeanModule; import io.jooby.flyway.FlywayModule; import io.jooby.hikari.HikariModule; import javax.inject.Named; +import javax.inject.Singleton; public class OrmDI extends AbstractModule { /** diff --git a/src/test/java/com/github/polpetta/mezzotre/helper/Loader.java b/src/test/java/com/github/polpetta/mezzotre/helper/Loader.java index 90bc7b9..6063993 100644 --- a/src/test/java/com/github/polpetta/mezzotre/helper/Loader.java +++ b/src/test/java/com/github/polpetta/mezzotre/helper/Loader.java @@ -30,6 +30,7 @@ public class Loader { hikariConnectionProperties.put("username", container.getUsername()); hikariConnectionProperties.put("password", container.getPassword()); hikariConnectionProperties.put("jdbcUrl", container.getJdbcUrl()); + hikariConnectionProperties.put("autoCommit", "false"); ebeanConnectionProperties.load(ebeanInputStream); ebeanConnectionProperties.put("datasource_db_username", container.getUsername()); diff --git a/src/test/java/com/github/polpetta/mezzotre/orm/CallbackQueryContextCleanerIntegrationTest.java b/src/test/java/com/github/polpetta/mezzotre/orm/CallbackQueryContextCleanerIntegrationTest.java index de487f2..afe607a 100644 --- a/src/test/java/com/github/polpetta/mezzotre/orm/CallbackQueryContextCleanerIntegrationTest.java +++ b/src/test/java/com/github/polpetta/mezzotre/orm/CallbackQueryContextCleanerIntegrationTest.java @@ -1,21 +1,18 @@ package com.github.polpetta.mezzotre.orm; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; import com.github.polpetta.mezzotre.helper.Loader; import com.github.polpetta.mezzotre.helper.TestConfig; +import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; +import com.github.polpetta.mezzotre.orm.model.query.QCallbackQueryContext; +import com.github.polpetta.types.json.CallbackQueryMetadata; import io.ebean.Database; -import io.ebean.typequery.PString; -import java.util.concurrent.CompletableFuture; +import java.time.Duration; import java.util.concurrent.TimeUnit; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; -import org.mockito.ArgumentCaptor; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.jupiter.api.*; import org.slf4j.LoggerFactory; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; @@ -33,7 +30,7 @@ class CallbackQueryContextCleanerIntegrationTest { private Database database; - private BatchBeanCleanerService fakeBatchBeanCleanerService; + private BatchBeanCleanerService batchBeanCleanerService; private CallbackQueryContextCleaner callbackQueryContextCleaner; @BeforeEach @@ -41,41 +38,41 @@ class CallbackQueryContextCleanerIntegrationTest { database = Loader.connectToDatabase(Loader.loadDefaultEbeanConfigWithPostgresSettings(postgresServer)); - fakeBatchBeanCleanerService = mock(BatchBeanCleanerService.class); + batchBeanCleanerService = + new BatchBeanCleanerService( + LoggerFactory.getLogger(BatchBeanCleanerService.class), + Pair.of(0, TimeUnit.MILLISECONDS)); + batchBeanCleanerService.startAsync().awaitRunning(Duration.ofSeconds(10)); callbackQueryContextCleaner = new CallbackQueryContextCleaner( - fakeBatchBeanCleanerService, - LoggerFactory.getLogger(CallbackQueryContextCleaner.class)); + batchBeanCleanerService, LoggerFactory.getLogger(CallbackQueryContextCleaner.class)); + } + + @AfterEach + void tearDown() throws Exception { + batchBeanCleanerService.stopAsync().awaitTerminated(Duration.ofSeconds(10)); } @Test @Timeout(value = 1, unit = TimeUnit.MINUTES) void shouldDeleteByGroupId() throws Exception { - when(fakeBatchBeanCleanerService.removeAsync(eq("an id"), any())) - .thenReturn(CompletableFuture.completedFuture(1)); + new CallbackQueryContext("doesn't matter", "an id", new CallbackQueryMetadata()).save(); final Integer got = callbackQueryContextCleaner.removeGroupAsync("an id").get(); - final ArgumentCaptor pStringArgumentCaptor = ArgumentCaptor.forClass(PString.class); - verify(fakeBatchBeanCleanerService, times(1)) - .removeAsync(eq("an id"), pStringArgumentCaptor.capture()); assertEquals(1, got); - assertEquals("entryGroup", pStringArgumentCaptor.getValue().toString()); + assertEquals(0, new QCallbackQueryContext().findCount()); } @Test @Timeout(value = 1, unit = TimeUnit.MINUTES) void shouldDeleteById() throws Exception { - when(fakeBatchBeanCleanerService.removeAsync(eq("an id"), any())) - .thenReturn(CompletableFuture.completedFuture(1)); + new CallbackQueryContext("an id", "doesn't matter", new CallbackQueryMetadata()).save(); final Integer got = callbackQueryContextCleaner.removeIdAsync("an id").get(); - final ArgumentCaptor pStringArgumentCaptor = ArgumentCaptor.forClass(PString.class); - verify(fakeBatchBeanCleanerService, times(1)) - .removeAsync(eq("an id"), pStringArgumentCaptor.capture()); assertEquals(1, got); - assertEquals("id", pStringArgumentCaptor.getValue().toString()); + assertEquals(0, new QCallbackQueryContext().findCount()); } }