diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java index 32da786..2949e20 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java @@ -5,6 +5,7 @@ package org.gcube.accounting.persistence; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.gcube.accounting.aggregation.scheduler.AggregationScheduler; import org.gcube.accounting.datamodel.SingleUsageRecord; @@ -136,7 +137,9 @@ public abstract class AccountingPersistence { } - public void flush() throws Exception { + public void flush(long timeout, TimeUnit timeUnit) throws Exception { + pool.awaitTermination(timeout, timeUnit); + final AccountingPersistence persistence = this; aggregationScheduler.flush(new AccountingPersistenceExecutor(){ @@ -146,6 +149,7 @@ public abstract class AccountingPersistence { } }); + } diff --git a/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java b/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java index da96d34..72eb7d9 100644 --- a/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java +++ b/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java @@ -3,6 +3,8 @@ */ package org.gcube.accounting.persistence; +import java.util.concurrent.TimeUnit; + import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.basetypes.TestUsageRecord; import org.gcube.accounting.testutility.StressTestUtility; @@ -17,6 +19,9 @@ import org.junit.Test; */ public class PersistenceTest { + public static final long timeout = 5000; + public static final TimeUnit timeUnit = TimeUnit.MILLISECONDS; + public static AccountingPersistence getPersistence(){ ScopeProvider.instance.set(PersistenceConfigurationTest.GCUBE_DEVNEXT_SCOPE); AccountingPersistenceFactory.setFallbackLocation(null); @@ -36,7 +41,7 @@ public class PersistenceTest { } }, 1); - persistence.flush(); + persistence.flush(timeout, timeUnit); } @Test @@ -50,7 +55,7 @@ public class PersistenceTest { } }, 1); - persistence.flush(); + persistence.flush(timeout, timeUnit); } @Test @@ -76,7 +81,7 @@ public class PersistenceTest { persistence.account(usageRecord); } }); - persistence.flush(); + persistence.flush(timeout, timeUnit); } }