On Mon, 21 Oct 2024 18:39:07 GMT, Alexey Ivanov <aiva...@openjdk.org> wrote:
>>> To ensure all the events in the queue are handled, I added >>> `robot.waitForIdle()`. >> >> This will flush events which were posted after the windows gained focus, but >> before the patch the test flushed all events (including event to show the >> window which might be asynchronous). My point is that this operation might >> be longer that 1 second(one of the reason we have all that deleyas). So we >> should waitForIdle first, then wait via delay or other sync_ops. > > This will flush all the pending events in the queue, which includes events > that were posted before the `windowGainedFocus` latch is released as well as > events are posted afterwards. > > There's no difference whether I place `waitForIdle` before or after. > > If I place `robot.waitForIdle` before `windowGainedFocus.await`, then `await` > immediately returns, that is the delay added by `await` is zero. > > If I place `robot.waitForIdle` after `windowGainedFocus.await`, the main > thread stops until the window gained focus event is delivered, after which > `robot.waitForIdle` flushes the remaining events in the queue. > > In both cases, there's a slight possibility `windowGainedFocus` is never > triggered if the window didn't get focus for whatever reason… > > On my Windows 11 laptop, the average time (over 20 runs of the `bug4323121` > test) > > * for `windowGainedFocus.await`: 12 ms > * for `robot.waitForIdle`: 36 ms > * or cumulatively: 48 ms > > If I change the order of calls, > > * for `robot.waitForIdle`: 51 ms > * for `windowGainedFocus.await`: 0 ms > * or cumulatively: 51 ms > > Both versions align well — `robot.waitForIdle` takes about 50 ms. (The > version where `waitForIdle` follows `await` is slightly quicker because > `await` does nothing at all whereas `waitForIdle` posts events from its own > copy of the queue for handling. If there's no difference, I see no reason to complicate the code. ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/21475#discussion_r1809325454