Support asynchronous events; Addresses BUKKIT-1212

By: Wesley Wolfe <weswolf@aol.com>
This commit is contained in:
Bukkit/Spigot
2012-06-13 21:28:13 -05:00
parent ca71171e8c
commit ad7045596d
10 changed files with 300 additions and 27 deletions

View File

@@ -0,0 +1,48 @@
package org.bukkit;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
public class TestServer implements InvocationHandler {
private static interface MethodHandler {
Object handle(TestServer server, Object[] args);
}
private static final Constructor<? extends Server> constructor;
private static final HashMap<Method, MethodHandler> methods = new HashMap<Method, MethodHandler>();
static {
try {
methods.put(Server.class.getMethod("isPrimaryThread"),
new MethodHandler() {
public Object handle(TestServer server, Object[] args) {
return Thread.currentThread().equals(server.creatingThread);
}
});
constructor = Proxy.getProxyClass(Server.class.getClassLoader(), Server.class).asSubclass(Server.class).getConstructor(InvocationHandler.class);
} catch (Throwable t) {
throw new Error(t);
}
}
private Thread creatingThread = Thread.currentThread();
private TestServer() {};
public static Server getInstance() {
try {
return constructor.newInstance(new TestServer());
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public Object invoke(Object proxy, Method method, Object[] args) {
MethodHandler handler = methods.get(method);
if (handler != null) {
return handler.handle(this, args);
}
throw new UnsupportedOperationException(String.valueOf(method));
}
}

View File

@@ -0,0 +1,19 @@
package org.bukkit.event;
public class TestEvent extends Event {
private static final HandlerList handlers = new HandlerList();
public TestEvent(boolean async) {
super(async);
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@@ -0,0 +1,118 @@
package org.bukkit.plugin;
import static junit.framework.Assert.*;
import org.bukkit.Server;
import org.bukkit.TestServer;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.event.Event;
import org.bukkit.event.TestEvent;
import org.junit.Test;
public class PluginManagerTest {
private class MutableObject {
volatile Object value = null;
}
final Server server = TestServer.getInstance();
final SimpleCommandMap commandMap = new SimpleCommandMap(server);
final PluginManager pm = new SimplePluginManager(server, commandMap);
final MutableObject store = new MutableObject();
@Test
public void testAsyncSameThread() {
final Event event = new TestEvent(true);
try {
pm.callEvent(event);
} catch (IllegalStateException ex) {
assertEquals(event.getEventName() + " cannot be triggered asynchronously from primary server thread.", ex.getMessage());
return;
}
throw new IllegalStateException("No exception thrown");
}
@Test
public void testSyncSameThread() {
final Event event = new TestEvent(false);
pm.callEvent(event);
}
@Test
public void testAsyncLocked() throws InterruptedException {
final Event event = new TestEvent(true);
Thread secondThread = new Thread(
new Runnable() {
public void run() {
try {
synchronized (pm) {
pm.callEvent(event);
}
} catch (Throwable ex) {
store.value = ex;
}
}});
secondThread.start();
secondThread.join();
assertTrue(store.value instanceof IllegalStateException);
assertEquals(event.getEventName() + " cannot be triggered asynchronously from inside synchronized code.", ((Throwable) store.value).getMessage());
}
@Test
public void testAsyncUnlocked() throws InterruptedException {
final Event event = new TestEvent(true);
Thread secondThread = new Thread(
new Runnable() {
public void run() {
try {
pm.callEvent(event);
} catch (Throwable ex) {
store.value = ex;
}
}});
secondThread.start();
secondThread.join();
if (store.value != null) {
throw new RuntimeException((Throwable) store.value);
}
}
@Test
public void testSyncUnlocked() throws InterruptedException {
final Event event = new TestEvent(false);
Thread secondThread = new Thread(
new Runnable() {
public void run() {
try {
pm.callEvent(event);
} catch (Throwable ex) {
store.value = ex;
}
}});
secondThread.start();
secondThread.join();
if (store.value != null) {
throw new RuntimeException((Throwable) store.value);
}
}
@Test
public void testSyncLocked() throws InterruptedException {
final Event event = new TestEvent(false);
Thread secondThread = new Thread(
new Runnable() {
public void run() {
try {
synchronized (pm) {
pm.callEvent(event);
}
} catch (Throwable ex) {
store.value = ex;
}
}});
secondThread.start();
secondThread.join();
if (store.value != null) {
throw new RuntimeException((Throwable) store.value);
}
}
}