public Optional<Event> firstDeferredIfIdling() {
        lock.lock();
        try {
            if (eventHandler.head.next != eventHandler.head) {
                // There are events ready to run immediately. The queue is not idling.
                return Optional.empty();
            }
            Map.Entry<Long, EventContext> entry = eventHandler.deadlineMap.firstEntry();
            if (entry == null) {
                // The queue is idling, but not waiting for any deadline.
                return Optional.empty();
            }
            EventContext eventContext = entry.getValue();
            if (eventContext.insertionType != EventInsertionType.DEFERRED) {
                // Any event with a deadline is put in `deadlineMap`.
                // But events of type other than DEFERRED will run immediately,
                // so the queue will not idle waiting for their deadline.
                return Optional.empty();
            }
            return Optional.of(eventContext.event);
        } finally {
            lock.unlock();
        }
    }
Returns the deferred event that the queue is waiting for, idling until
its deadline comes, if there is any.
If the queue has immediate work to do, this returns empty.
This is useful for unit tests, where to make progress, we need to
speed the clock up until the next scheduled event is ready to run.
server-common/src/main/java/org/apache/kafka/queue/KafkaEventQueue.java