From f0f4bf975bc9834d7fad8c5a6928b10781c67cd2 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 25 May 2018 19:36:17 +0300 Subject: [PATCH] Added exception logging to the Processing system --- .../plan/system/cache/GeolocationCache.java | 3 -- .../plan/system/processing/Processing.java | 49 +++++++++++++------ .../system/cache/GeolocationCacheTest.java | 48 ++++++++++++++++++ 3 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 Plan/src/test/java/com/djrapitops/plan/system/cache/GeolocationCacheTest.java diff --git a/Plan/src/main/java/com/djrapitops/plan/system/cache/GeolocationCache.java b/Plan/src/main/java/com/djrapitops/plan/system/cache/GeolocationCache.java index 29b1dd6cb..8207fcc44 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/cache/GeolocationCache.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/cache/GeolocationCache.java @@ -6,7 +6,6 @@ import com.djrapitops.plan.system.file.FileSystem; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.utilities.Verify; -import com.google.common.cache.Cache; import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.exception.GeoIp2Exception; import com.maxmind.geoip2.model.CountryResponse; @@ -28,8 +27,6 @@ import java.util.zip.GZIPInputStream; * This class contains the geolocation cache. *

* It caches all IPs with their matching country. - *

- * This cache uses the Google Guava {@link Cache}. * * @author Fuzzlemann * @since 3.5.5 diff --git a/Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java b/Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java index 00b1c0d59..21c8bf6aa 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java @@ -9,10 +9,7 @@ import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.utilities.Verify; import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; +import java.util.concurrent.*; public class Processing implements SubSystem { @@ -24,6 +21,7 @@ public class Processing implements SubSystem { criticalExecutor = Executors.newFixedThreadPool(2); saveInstance(nonCriticalExecutor); saveInstance(criticalExecutor); + saveInstance(this); } public static void submit(Runnable runnable) { @@ -40,27 +38,27 @@ public class Processing implements SubSystem { public static void submitNonCritical(Runnable runnable) { saveInstance(runnable); - getInstance().nonCriticalExecutor.submit(runnable); + CompletableFuture.supplyAsync(() -> runnable, getInstance().nonCriticalExecutor) + .thenAccept(Runnable::run) + .handle(Processing::exceptionHandler); } public static void submitCritical(Runnable runnable) { saveInstance(runnable); - getInstance().criticalExecutor.submit(runnable); + CompletableFuture.supplyAsync(() -> runnable, getInstance().criticalExecutor) + .thenAccept(Runnable::run) + .handle(Processing::exceptionHandler); } public static void submitNonCritical(Runnable... runnables) { - ExecutorService nonCriticalExecutor = getInstance().nonCriticalExecutor; for (Runnable runnable : runnables) { - saveInstance(runnable); - nonCriticalExecutor.submit(runnable); + submitNonCritical(runnable); } } public static void submitCritical(Runnable... runnables) { - ExecutorService criticalExecutor = getInstance().criticalExecutor; for (Runnable runnable : runnables) { - saveInstance(runnable); - criticalExecutor.submit(runnable); + submitCritical(runnable); } } @@ -74,12 +72,35 @@ public class Processing implements SubSystem { public static Future submitNonCritical(Callable task) { saveInstance(task); - return getInstance().nonCriticalExecutor.submit(task); + return CompletableFuture.supplyAsync(() -> task, getInstance().nonCriticalExecutor) + .thenApply(tCallable -> { + try { + return tCallable.call(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + }) + .handle(Processing::exceptionHandler); + } + + private static T exceptionHandler(T t, Throwable throwable) { + if (throwable != null) { + Log.toLog(Processing.class, throwable.getCause()); + } + return t; } public static Future submitCritical(Callable task) { saveInstance(task); - return getInstance().criticalExecutor.submit(task); + return CompletableFuture.supplyAsync(() -> task, getInstance().criticalExecutor) + .thenApply(tCallable -> { + try { + return tCallable.call(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + }) + .handle(Processing::exceptionHandler); } public static Processing getInstance() { diff --git a/Plan/src/test/java/com/djrapitops/plan/system/cache/GeolocationCacheTest.java b/Plan/src/test/java/com/djrapitops/plan/system/cache/GeolocationCacheTest.java new file mode 100644 index 000000000..6def06e5b --- /dev/null +++ b/Plan/src/test/java/com/djrapitops/plan/system/cache/GeolocationCacheTest.java @@ -0,0 +1,48 @@ +package com.djrapitops.plan.system.cache; + +import com.djrapitops.plan.Plan; +import com.djrapitops.plan.api.exceptions.EnableException; +import com.djrapitops.plan.system.BukkitSystem; +import com.djrapitops.plugin.StaticHolder; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import utilities.mocks.BukkitMockUtil; + +import static org.junit.Assert.assertEquals; + +/** + * Test for GeolocationCache. + * + * @author Rsl1122 + */ +public class GeolocationCacheTest { + + @ClassRule + public static TemporaryFolder temporaryFolder = new TemporaryFolder(); + private static Plan planMock; + + @BeforeClass + public static void setUpClass() throws Exception { + BukkitMockUtil mockUtil = BukkitMockUtil.setUp() + .withDataFolder(temporaryFolder.getRoot()) + .withLogging() + .withPluginDescription() + .withResourceFetchingFromJar() + .withServer(); + planMock = mockUtil.getPlanMock(); + StaticHolder.saveInstance(GeolocationCacheTest.class, planMock.getClass()); + } + + @Test + public void testGeolocationCache() throws EnableException { + BukkitSystem system = new BukkitSystem(planMock); + system.enable(); + + String expected = "Germany"; + String result = GeolocationCache.getCountry("141.52.255.1"); + assertEquals(expected, result); + } + +} \ No newline at end of file