From 00e1ed3aa8004e34b69df35782939ec88266ad6d Mon Sep 17 00:00:00 2001 From: Andrii Shvaika Date: Fri, 29 Jan 2021 13:33:30 +0200 Subject: [PATCH] More tests for Deduplication Executor --- .../utils/EventDeduplicationExecutor.java | 4 +- .../util/EventDeduplicationExecutorTest.java | 40 ++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/utils/EventDeduplicationExecutor.java b/application/src/main/java/org/thingsboard/server/utils/EventDeduplicationExecutor.java index 1c67868740..4ce0d2f3d5 100644 --- a/application/src/main/java/org/thingsboard/server/utils/EventDeduplicationExecutor.java +++ b/application/src/main/java/org/thingsboard/server/utils/EventDeduplicationExecutor.java @@ -56,14 +56,14 @@ public class EventDeduplicationExecutor

{ log.info("[{}] Executing task: {}", name, params); function.accept(params); } catch (Throwable e) { - log.warn("Failed to process task with parameters: {}", params, e); + log.warn("[{}] Failed to process task with parameters: {}", name, params, e); throw e; } finally { unlockAndProcessIfAny(); } }); } catch (Throwable e) { - log.warn("Failed to submit task with parameters: {}", params, e); + log.warn("[{}] Failed to submit task with parameters: {}", name, params, e); unlockAndProcessIfAny(); throw e; } diff --git a/application/src/test/java/org/thingsboard/server/util/EventDeduplicationExecutorTest.java b/application/src/test/java/org/thingsboard/server/util/EventDeduplicationExecutorTest.java index 26718b6624..c58f44e2eb 100644 --- a/application/src/test/java/org/thingsboard/server/util/EventDeduplicationExecutorTest.java +++ b/application/src/test/java/org/thingsboard/server/util/EventDeduplicationExecutorTest.java @@ -41,15 +41,24 @@ public class EventDeduplicationExecutorTest { periodicFlow(MoreExecutors.newDirectExecutorService()); } + @Test + public void testExceptionFlowSameThread() throws InterruptedException { + exceptionFlow(MoreExecutors.newDirectExecutorService()); + } @Test public void testSimpleFlowSingleThread() throws InterruptedException { - simpleFlow(Executors.newFixedThreadPool(1)); + simpleFlow(Executors.newSingleThreadExecutor()); } @Test public void testPeriodicFlowSingleThread() throws InterruptedException { - periodicFlow(Executors.newFixedThreadPool(1)); + periodicFlow(Executors.newSingleThreadExecutor()); + } + + @Test + public void testExceptionFlowSingleThread() throws InterruptedException { + exceptionFlow(Executors.newSingleThreadExecutor()); } @Test @@ -62,6 +71,11 @@ public class EventDeduplicationExecutorTest { periodicFlow(Executors.newFixedThreadPool(3)); } + @Test + public void testExceptionFlowMultiThread() throws InterruptedException { + exceptionFlow(Executors.newFixedThreadPool(3)); + } + private void simpleFlow(ExecutorService executorService) throws InterruptedException { try { Consumer function = Mockito.spy(StringConsumer.class); @@ -105,6 +119,28 @@ public class EventDeduplicationExecutorTest { } } + private void exceptionFlow(ExecutorService executorService) throws InterruptedException { + try { + Consumer function = Mockito.spy(StringConsumer.class); + EventDeduplicationExecutor executor = new EventDeduplicationExecutor<>(EventDeduplicationExecutorTest.class.getSimpleName(), executorService, function); + + String params1 = "params1"; + String params2 = "params2"; + String params3 = "params3"; + + Mockito.doThrow(new RuntimeException()).when(function).accept("params1"); + executor.submit(params1); + executor.submit(params2); + Thread.sleep(500); + executor.submit(params3); + Thread.sleep(500); + Mockito.verify(function).accept(params2); + Mockito.verify(function).accept(params3); + } finally { + executorService.shutdownNow(); + } + } + public static class StringConsumer implements Consumer { @Override public void accept(String s) {