Skip to content

Commit

Permalink
Multithreading testing
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Jun 29, 2018
1 parent 5e5c9da commit ae2ce81
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/clubobsidian/trident/util/EventNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/EventDoublyLinkedListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
13 changes: 13 additions & 0 deletions src/test/java/JavaAssistEventManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

0 comments on commit ae2ce81

Please sign in to comment.