From 3d78b110f0b6725674024fbe3d800c9b82cae7de Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 26 Jul 2018 01:02:32 +0100 Subject: [PATCH] Move all event dispatching to it's own thread. Update tests to wait a bit --- core/src/main/groovy/com/muwire/core/EventBus.groovy | 12 ++++++++++++ .../com/muwire/core/files/FileManagerTest.groovy | 10 +++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/src/main/groovy/com/muwire/core/EventBus.groovy b/core/src/main/groovy/com/muwire/core/EventBus.groovy index d10ed868..e63f368c 100644 --- a/core/src/main/groovy/com/muwire/core/EventBus.groovy +++ b/core/src/main/groovy/com/muwire/core/EventBus.groovy @@ -1,6 +1,8 @@ package com.muwire.core import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executor +import java.util.concurrent.Executors import com.muwire.core.files.FileSharedEvent @@ -9,8 +11,18 @@ import groovy.util.logging.Log class EventBus { private Map handlers = new HashMap() + private final Executor executor = Executors.newSingleThreadExecutor {r -> + def rv = new Thread(r) + rv.setDaemon(true) + rv.setName("event-bus") + rv + } void publish(Event e) { + executor.execute({publishInternal(e)} as Runnable) + } + + private void publishInternal(Event e) { log.fine "publishing event of type ${e.getClass().getSimpleName()} seqNo ${e.seqNo} timestamp ${e.timestamp}" def currentHandlers final def clazz = e.getClass() diff --git a/core/src/test/groovy/com/muwire/core/files/FileManagerTest.groovy b/core/src/test/groovy/com/muwire/core/files/FileManagerTest.groovy index 1b4f5d78..09664d9f 100644 --- a/core/src/test/groovy/com/muwire/core/files/FileManagerTest.groovy +++ b/core/src/test/groovy/com/muwire/core/files/FileManagerTest.groovy @@ -14,7 +14,7 @@ class FileManagerTest { EventBus eventBus FileManager manager - ResultsEvent results + volatile ResultsEvent results def listener = new Object() { void onResultsEvent(ResultsEvent e) { @@ -42,6 +42,7 @@ class FileManagerTest { SearchEvent se = new SearchEvent(searchHash: ih.getRoot(), uuid: uuid) manager.onSearchEvent(se) + Thread.sleep(20) assert results != null assert results.results.size() == 1 @@ -61,6 +62,7 @@ class FileManagerTest { SearchEvent se = new SearchEvent(searchHash: ih.getRoot(), uuid: uuid) manager.onSearchEvent(se) + Thread.sleep(20) assert results != null assert results.results.size() == 2 @@ -78,6 +80,7 @@ class FileManagerTest { manager.onFileHashedEvent(fhe) manager.onSearchEvent new SearchEvent(searchHash: new byte[32], uuid: UUID.randomUUID()) + Thread.sleep(20) assert results == null } @@ -92,6 +95,7 @@ class FileManagerTest { UUID uuid = UUID.randomUUID() manager.onSearchEvent new SearchEvent(searchTerms: ["a"], uuid:uuid) + Thread.sleep(20) assert results != null assert results.results.size() == 1 @@ -113,6 +117,7 @@ class FileManagerTest { UUID uuid = UUID.randomUUID() manager.onSearchEvent new SearchEvent(searchTerms: ["c"], uuid:uuid) + Thread.sleep(20) assert results != null assert results.results.size() == 2 @@ -130,6 +135,7 @@ class FileManagerTest { manager.onFileHashedEvent(fhe) manager.onSearchEvent new SearchEvent(searchTerms: ["d"], uuid: UUID.randomUUID()) + Thread.sleep(20) assert results == null } @@ -145,6 +151,7 @@ class FileManagerTest { manager.onFileUnsharedEvent new FileUnsharedEvent(unsharedFile: sf2) manager.onSearchEvent new SearchEvent(searchHash : ih.getRoot()) + Thread.sleep(20) assert results != null assert results.results.size() == 1 assert results.results.contains(sf1) @@ -166,6 +173,7 @@ class FileManagerTest { // 1 match left manager.onSearchEvent new SearchEvent(searchTerms: ["c"]) + Thread.sleep(20) assert results != null assert results.results.size() == 1 assert results.results.contains(sf1)