From ae2ce819a7fadd8ef6a9ead606cb60bd8e270ff5 Mon Sep 17 00:00:00 2001 From: virustotalop Date: Thu, 28 Jun 2018 22:52:29 -0700 Subject: [PATCH] Multithreading testing --- .../trident/util/EventDoublyLinkedList.java | 8 +-- .../clubobsidian/trident/util/EventNode.java | 8 +-- src/test/java/EventDoublyLinkedListTest.java | 50 +++++++++++++++++++ src/test/java/JavaAssistEventManagerTest.java | 13 +++++ 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java b/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java index ddfd606..910e770 100644 --- a/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java +++ b/src/main/java/com/clubobsidian/trident/util/EventDoublyLinkedList.java @@ -21,12 +21,12 @@ public EventDoublyLinkedList() this.head = null; } - public EventNode getHead() + public synchronized EventNode getHead() { return this.head; } - public EventNode insert(MethodExecutor executor, EventPriority priority) + public synchronized EventNode insert(MethodExecutor executor, EventPriority priority) { if(executor == null || priority == null) return null; @@ -71,7 +71,7 @@ else if(found.getNext() == null) } } - public EventNode remove(MethodExecutor executor) + public synchronized EventNode remove(MethodExecutor executor) { EventNode found = this.find(executor); if(found == null) @@ -102,7 +102,7 @@ else if(found.getNext() == null) } - public EventNode find(MethodExecutor executor) + public synchronized EventNode find(MethodExecutor executor) { EventNode node = this.head; while(node != null) diff --git a/src/main/java/com/clubobsidian/trident/util/EventNode.java b/src/main/java/com/clubobsidian/trident/util/EventNode.java index 023b20a..3611cdc 100644 --- a/src/main/java/com/clubobsidian/trident/util/EventNode.java +++ b/src/main/java/com/clubobsidian/trident/util/EventNode.java @@ -24,22 +24,22 @@ public int getPriority() return this.priority; } - public EventNode getNext() + public synchronized EventNode getNext() { return this.next; } - public void setNext(EventNode next) + public synchronized void setNext(EventNode next) { this.next = next; } - public EventNode getPrev() + public synchronized EventNode getPrev() { return this.prev; } - public void setPrev(EventNode prev) + public synchronized void setPrev(EventNode prev) { this.prev = prev; } diff --git a/src/test/java/EventDoublyLinkedListTest.java b/src/test/java/EventDoublyLinkedListTest.java index b2a5589..3a17509 100644 --- a/src/test/java/EventDoublyLinkedListTest.java +++ b/src/test/java/EventDoublyLinkedListTest.java @@ -3,6 +3,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Random; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; @@ -302,4 +303,53 @@ public void nullCaseTest() EventNode inserted = list.insert(null, null); assertTrue("Inserted was not null", inserted == null); } + + @Test + public void multiThreadingTest() + { + AtomicInteger count = new AtomicInteger(0); + final EventDoublyLinkedList list = new EventDoublyLinkedList(); + Runnable testRunnable = new Runnable() + { + @Override + public void run() + { + for(int i = 0; i < 1000; i++) + { + try + { + int inc = count.incrementAndGet(); + System.out.println(inc); + TestListener testListener = new TestListener("test" + inc); + MethodExecutor testExecutor = new JavaAssistMethodExecutor(testListener, testListener.getClass().getDeclaredMethod("test", TestEvent.class)); + EventNode node = list.insert(testExecutor, EventPriority.LOWEST); + assertTrue("Node was null when inserted", node != null); + } + catch (NoSuchMethodException | SecurityException e) + { + e.printStackTrace(); + } + } + Thread.currentThread().interrupt(); + } + }; + Thread th1 = new Thread(testRunnable); + Thread th2 = new Thread(testRunnable); + + th1.start(); + th2.start(); + + while(th1.isAlive() || th2.isAlive()); + + int nodeCount = 0; + EventNode node = list.getHead(); + while(node != null) + { + nodeCount+= 1; + node = node.getNext(); + } + System.out.println("node: " + nodeCount); + System.out.println("count: " + count.get()); + assertTrue("Node count not equal when running from multiple thread", nodeCount == count.get()); + } } \ No newline at end of file diff --git a/src/test/java/JavaAssistEventManagerTest.java b/src/test/java/JavaAssistEventManagerTest.java index b88d449..306a541 100644 --- a/src/test/java/JavaAssistEventManagerTest.java +++ b/src/test/java/JavaAssistEventManagerTest.java @@ -80,4 +80,17 @@ public void testUnregister() assertTrue("Listener was not registered", manager.unregisterEvents(test)); assertTrue("Listener was still registered", !manager.unregisterEvents(test)); } + + @Test + public void benchmark() + { + TestListener test = new TestListener("test"); + EventManager manager = new JavaAssistEventManager(); + manager.registerEvents(test); + + for(int i = 0; i < 1000000; i++) + { + manager.callEvent(new TestEvent()); + } + } } \ No newline at end of file