Last active
January 17, 2019 05:20
-
-
Save OhYea777/82bd5880f436f7ef2393a32fbdfd54ad to your computer and use it in GitHub Desktop.
Listeners being registered as null
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Testing started at 12:21 PM ... | |
12:21:22 PM: Executing tasks ':cleanTest :test --tests "net.minecraftforge.eventbus.test.ThreadedListenerExceptionTest.testListenerListConstruction"'... | |
Reckoned version: 0.4.0-milestone.0.0+20190116T232125Z | |
> Task :cleanTest | |
> Task :compileJava UP-TO-DATE | |
> Task :processResources NO-SOURCE | |
> Task :classes UP-TO-DATE | |
> Task :compileMlserviceJava UP-TO-DATE | |
> Task :processMlserviceResources UP-TO-DATE | |
> Task :mlserviceClasses UP-TO-DATE | |
> Task :compileTestJarsJava UP-TO-DATE | |
> Task :processTestJarsResources NO-SOURCE | |
> Task :testJarsClasses UP-TO-DATE | |
> Task :compileTestJava UP-TO-DATE | |
> Task :processTestResources NO-SOURCE | |
> Task :testClasses UP-TO-DATE | |
> Task :test FAILED | |
2019-01-17 12:21:27,448 Test worker ERROR Error processing element Queue ([Appenders: null]): CLASS_NOT_FOUND | |
2019-01-17 12:21:27,547 Test worker ERROR Unable to locate appender "ServerGuiConsole" for logger config "root" | |
java.lang.NullPointerException | |
at net.minecraftforge.eventbus.EventBus.post(EventBus.java:248) | |
at net.minecraftforge.eventbus.test.ThreadedListenerExceptionTest$TestEvent$Runner.run(ThreadedListenerExceptionTest.java:185) | |
priority=HIGHEST, listeners=[] | |
priority=HIGH, listeners=[] | |
priority=NORMAL, listeners=[null, net.minecraftforge.eventbus.EventBus$$Lambda$206/1563775655, net.minecraftforge.eventbus.EventBus$$Lambda$206/1563775655, net.minecraftforge.eventbus.EventBus$$Lambda$206/1563775655, net.minecraftforge.eventbus.EventBus$$Lambda$206/1563775655, net.minecraftforge.eventbus.EventBus$$Lambda$206/1563775655, net.minecraftforge.eventbus.EventBus$$Lambda$206/1563775655, net.minecraftforge.eventbus.EventBus$$Lambda$206/1563775655, net.minecraftforge.eventbus.EventBus$$Lambda$206/1563775655] | |
priority=LOW, listeners=[] | |
priority=LOWEST, listeners=[] | |
Exception in thread "Thread-25" java.lang.RuntimeException: java.lang.NullPointerException | |
at net.minecraftforge.eventbus.EventBus.post(EventBus.java:254) | |
at net.minecraftforge.eventbus.test.ThreadedListenerExceptionTest$TestEvent$Runner.run(ThreadedListenerExceptionTest.java:185) | |
Caused by: java.lang.NullPointerException | |
at net.minecraftforge.eventbus.EventBus.post(EventBus.java:248) | |
... 1 more | |
net.minecraftforge.eventbus.test.ThreadedListenerExceptionTest > testListenerListConstruction() FAILED | |
org.opentest4j.AssertionFailedError at ThreadedListenerExceptionTest.java:69 | |
1 test completed, 1 failed | |
FAILURE: Build failed with an exception. | |
* What went wrong: | |
Execution failed for task ':test'. | |
> There were failing tests. See the report at: file:///F:/Projects/Java/Minecraft/EventBus/build/reports/tests/test/index.html | |
* Try: | |
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. | |
* Get more help at https://help.gradle.org | |
BUILD FAILED in 5s | |
7 actionable tasks: 2 executed, 5 up-to-date | |
expected: <false> but was: <true> | |
org.opentest4j.AssertionFailedError: expected: <false> but was: <true> | |
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:38) | |
at org.junit.jupiter.api.AssertFalse.assertFalse(AssertFalse.java:40) | |
at org.junit.jupiter.api.AssertFalse.assertFalse(AssertFalse.java:35) | |
at org.junit.jupiter.api.Assertions.assertFalse(Assertions.java:185) | |
at net.minecraftforge.eventbus.test.ThreadedListenerExceptionTest.testListenerListConstruction(ThreadedListenerExceptionTest.java:69) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:498) | |
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:532) | |
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115) | |
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171) | |
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72) | |
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167) | |
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114) | |
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:108) | |
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74) | |
at java.util.ArrayList.forEach(ArrayList.java:1257) | |
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112) | |
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74) | |
at java.util.ArrayList.forEach(ArrayList.java:1257) | |
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112) | |
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98) | |
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74) | |
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) | |
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) | |
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) | |
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220) | |
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188) | |
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202) | |
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181) | |
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) | |
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:102) | |
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:82) | |
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:78) | |
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:498) | |
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) | |
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) | |
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) | |
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) | |
at com.sun.proxy.$Proxy2.stop(Unknown Source) | |
at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:132) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:498) | |
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) | |
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) | |
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:175) | |
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:157) | |
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404) | |
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) | |
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46) | |
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) | |
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) | |
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) | |
at java.lang.Thread.run(Thread.java:748) | |
There were failing tests. See the report at: file:///F:/Projects/Java/Minecraft/EventBus/build/reports/tests/test/index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.minecraftforge.eventbus.test; | |
import net.minecraftforge.eventbus.EventBus; | |
import net.minecraftforge.eventbus.ListenerList; | |
import net.minecraftforge.eventbus.api.Event; | |
import net.minecraftforge.eventbus.api.EventPriority; | |
import net.minecraftforge.eventbus.api.IEventListener; | |
import org.junit.jupiter.api.Test; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.stream.Collectors; | |
import static org.junit.jupiter.api.Assertions.assertFalse; | |
public class ThreadedListenerExceptionTest { | |
private static boolean failed; | |
private static final EventBus testEventBus = new EventBus((bus, event, listeners, index, throwable) -> { | |
failed = true; | |
throwable.printStackTrace(); | |
try { | |
final ListenerList listenerList = event.getListenerList(); | |
Method getInstance = listenerList.getClass().getDeclaredMethod("getInstance", int.class); | |
getInstance.setAccessible(true); | |
Object listenerListInst = getInstance.invoke(listenerList, 0); | |
Field prioritiesField = listenerListInst.getClass().getDeclaredField("priorities"); | |
prioritiesField.setAccessible(true); | |
@SuppressWarnings("unchecked") | |
ArrayList<ArrayList<IEventListener>> priorities = (ArrayList<ArrayList<IEventListener>>) prioritiesField.get(listenerListInst); | |
Arrays.stream(EventPriority.values()).forEach(priority -> { | |
System.out.println("priority=" + priority + ", listeners=[" + priorities.get(priority.ordinal()).stream().map(listener -> { | |
if (listener == null) { | |
return null; | |
} | |
return listener.getClass().getName(); | |
}).collect(Collectors.joining(", ")) + "]"); | |
}); | |
} catch (NoSuchMethodException | NoSuchFieldException | IllegalAccessException | InvocationTargetException ignored) { } | |
}); | |
@Test | |
public void testListenerListConstruction() { | |
for (int runs = 0; runs < 1000 && !failed; runs ++) { | |
new TestListenerOne.Runner().start(); | |
new TestListenerTwo.Runner().start(); | |
new TestListenerThree.Runner().start(); | |
new TestListenerFour.Runner().start(); | |
new TestListenerFive.Runner().start(); | |
for (int listeners = 0; listeners < 10 && !failed; listeners ++) { | |
new TestEvent.Runner().start(); | |
} | |
} | |
assertFalse(failed); | |
} | |
private static class TestListenerOne { | |
private TestListenerOne() { | |
testEventBus.addListener(this::testEvent); | |
} | |
private void testEvent(final TestEvent event) { | |
} | |
private static class Runner extends Thread { | |
@Override | |
public void run() { | |
new TestListenerOne(); | |
} | |
} | |
} | |
private static class TestListenerTwo { | |
private TestListenerTwo() { | |
testEventBus.addListener(this::testEvent); | |
} | |
private void testEvent(final TestEvent event) { | |
} | |
private static class Runner extends Thread { | |
@Override | |
public void run() { | |
new TestListenerTwo(); | |
} | |
} | |
} | |
private static class TestListenerThree { | |
private TestListenerThree() { | |
testEventBus.addListener(this::testEvent); | |
} | |
private void testEvent(final TestEvent event) { | |
} | |
private static class Runner extends Thread { | |
@Override | |
public void run() { | |
new TestListenerThree(); | |
} | |
} | |
} | |
private static class TestListenerFour { | |
private TestListenerFour() { | |
testEventBus.addListener(this::testEvent); | |
} | |
private void testEvent(final TestEvent event) { | |
} | |
private static class Runner extends Thread { | |
@Override | |
public void run() { | |
new TestListenerFour(); | |
} | |
} | |
} | |
private static class TestListenerFive { | |
private TestListenerFive() { | |
testEventBus.addListener(this::testEvent); | |
} | |
private void testEvent(final TestEvent event) { | |
} | |
private static class Runner extends Thread { | |
@Override | |
public void run() { | |
new TestListenerFive(); | |
} | |
} | |
} | |
public static class TestEvent extends Event { | |
public TestEvent() { } | |
private static class Runner extends Thread { | |
@Override | |
public void run() { | |
for (int i = 0; i < 10 && !failed; i ++) { | |
testEventBus.post(new TestEvent()); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.minecraftforge.eventbus.test; | |
import net.minecraftforge.eventbus.EventBus; | |
import net.minecraftforge.eventbus.ListenerList; | |
import net.minecraftforge.eventbus.api.Event; | |
import net.minecraftforge.eventbus.api.EventPriority; | |
import net.minecraftforge.eventbus.api.IEventBus; | |
import net.minecraftforge.eventbus.api.IEventExceptionHandler; | |
import net.minecraftforge.eventbus.api.IEventListener; | |
import org.junit.jupiter.api.Test; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.concurrent.CopyOnWriteArrayList; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
import java.util.stream.Collectors; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import static org.junit.jupiter.api.Assertions.assertTrue; | |
public class ThreadedListenerTest { | |
private static final EventBus EVENT_BUS = new EventBus(new TestEventExceptionHandler()); | |
private static final int NUM_THREADS = 10; | |
private static final int NUM_LISTENERS = 1000; | |
@Test | |
public void testAddListenerThreaded() throws Exception { | |
List<TestEvent.TestEventListener> listeners = new CopyOnWriteArrayList<>(); | |
List<Future> futures = new ArrayList<>(); | |
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); | |
for (int i = 0; i < NUM_LISTENERS; i ++) { | |
futures.add(executor.submit(new TestEvent.Register(EVENT_BUS, listeners))); | |
} | |
for (int i = 0; i < NUM_LISTENERS; i ++) { | |
futures.get(i).get(); | |
} | |
EVENT_BUS.post(new TestEvent(1337)); | |
assertEquals(NUM_LISTENERS, listeners.size(), "Failed to add all listeners"); | |
listeners.forEach(listener -> assertTrue(listener.getReceivedEvents().contains(1337), "Failed to receive all events")); | |
} | |
public static class TestEvent extends Event { | |
private int event; | |
public TestEvent() { } | |
private TestEvent(final int event) { | |
this.event = event; | |
} | |
private int getEvent() { | |
return event; | |
} | |
private static class TestEventListener { | |
private final List<Integer> receivedEvents = new ArrayList<>(); | |
private void received(final TestEvent event) { | |
receivedEvents.add(event.getEvent()); | |
} | |
private List<Integer> getReceivedEvents() { | |
return receivedEvents; | |
} | |
} | |
private static class Register implements Runnable { | |
private final EventBus eventBus; | |
private final List<TestEventListener> listeners; | |
private Register(final EventBus eventBus, final List<TestEventListener> listeners) { | |
this.eventBus = eventBus; | |
this.listeners = listeners; | |
} | |
@Override | |
public void run() { | |
final TestEventListener listener = new TestEventListener(); | |
eventBus.addListener(listener::received); | |
listeners.add(listener); | |
} | |
} | |
} | |
private static class TestEventExceptionHandler implements IEventExceptionHandler { | |
@Override | |
public void handleException(IEventBus bus, Event event, IEventListener[] listeners, int index, Throwable throwable) { | |
throwable.printStackTrace(); | |
try { | |
final ListenerList listenerList = event.getListenerList(); | |
Method getInstance = listenerList.getClass().getDeclaredMethod("getInstance", int.class); | |
getInstance.setAccessible(true); | |
Object listenerListInst = getInstance.invoke(listenerList, 0); | |
Field prioritiesField = listenerListInst.getClass().getDeclaredField("priorities"); | |
prioritiesField.setAccessible(true); | |
@SuppressWarnings("unchecked") | |
ArrayList<ArrayList<IEventListener>> priorities = (ArrayList<ArrayList<IEventListener>>) prioritiesField.get(listenerListInst); | |
Arrays.stream(EventPriority.values()).forEach(priority -> { | |
System.out.println("priority=" + priority + ", listeners=[" + priorities.get(priority.ordinal()).stream().map(listener -> { | |
if (listener == null) { | |
return null; | |
} | |
return listener.getClass().getName(); | |
}).collect(Collectors.joining(", ")) + "]"); | |
}); | |
} catch (NoSuchMethodException | NoSuchFieldException | IllegalAccessException | InvocationTargetException ignored) { } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment