NonBlockingQueue implements IQueue<T> { @Override public void addNewItemCallback(final Runnable callback) { synchronized (callbacksListLock) { newElementCallbacks.add(callback); if (!queue.isEmpty()) { callback.run(); } } } NonBlockingQueue(final Queue<T> queue); @Override void put(final T item); @Override T take(); @Override T tryTake(); @Override void addNewItemCallback(final Runnable callback); @Override void removeNewItemCallback(final Runnable callback);  }
@Test public void Should_callCallbackImmediately_When_thereAlreadyAreElementsInQueue() throws Exception { Queue underlying = mock(Queue.class); Runnable callback = mock(Runnable.class); when(underlying.isEmpty()).thenReturn(false); IQueue queue = new NonBlockingQueue(underlying); queue.addNewItemCallback(callback); verify(callback).run(); }