cheaper UIUtil.dispatchAllInvocationEvents

avoid reflection and assertions on each event
This commit is contained in:
peter
2019-01-21 12:30:01 +01:00
parent e119c75361
commit 340412cca0
2 changed files with 28 additions and 25 deletions
@@ -2360,14 +2360,14 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase {
long deadline = System.currentTimeMillis() + 60_000;
while (!daemonIsWorkingOrPending()) {
if (System.currentTimeMillis() > deadline) fail("Too long waiting for daemon to start");
UIUtil.dispatchInvocationEvent();
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue();
}
while (daemonIsWorkingOrPending()) {
if (System.currentTimeMillis() > deadline) {
dumpThreadsToConsole();
fail("Too long waiting for daemon to finish");
}
UIUtil.dispatchInvocationEvent();
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue();
}
}
@@ -2321,12 +2321,36 @@ public class UIUtil {
g.setComposite(X_RENDER_ACTIVE.getValue() ? AlphaComposite.SrcOver : AlphaComposite.Src);
}
/** @see #pump() */
/**
* In tests, consider using {@link com.intellij.testFramework.PlatformTestUtil#dispatchAllInvocationEventsInIdeEventQueue()}
* @see #pump()
*/
@TestOnly
public static void dispatchAllInvocationEvents() {
assert EdtInvocationManager.getInstance().isEventDispatchThread() : Thread.currentThread() + "; EDT: "+getEventQueueThread();
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
Method dispatchEventMethod;
try {
dispatchEventMethod = eventQueue.getClass().getDeclaredMethod("dispatchEvent", AWTEvent.class);
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
for (int i = 1; ; i++) {
AWTEvent event = dispatchInvocationEvent();
AWTEvent event = eventQueue.peekEvent();
if (event == null) break;
try {
event = eventQueue.getNextEvent();
if (event instanceof InvocationEvent) {
dispatchEventMethod.invoke(eventQueue, event);
}
}
catch (InvocationTargetException e) {
ExceptionUtil.rethrowAllAsUnchecked(e.getCause());
}
catch (Exception e) {
LOG.error(e);
}
if (i % 10000 == 0) {
//noinspection UseOfSystemOutOrSystemErr
@@ -2335,27 +2359,6 @@ public class UIUtil {
}
}
@TestOnly
public static AWTEvent dispatchInvocationEvent() {
assert EdtInvocationManager.getInstance().isEventDispatchThread() : Thread.currentThread() + "; EDT: "+getEventQueueThread();
final EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
AWTEvent event = eventQueue.peekEvent();
if (event == null) return null;
try {
event = eventQueue.getNextEvent();
if (event instanceof InvocationEvent) {
eventQueue.getClass().getDeclaredMethod("dispatchEvent", AWTEvent.class).invoke(eventQueue, event);
}
}
catch (InvocationTargetException e) {
ExceptionUtil.rethrowAllAsUnchecked(e.getCause());
}
catch (Exception e) {
LOG.error(e);
}
return event;
}
private static Thread getEventQueueThread() {
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
try {