InBuffer implements WorkerRecoveryListener, ApplicationListener, Runnable { @Override public void onApplicationEvent(ApplicationEvent applicationEvent) { if (applicationEvent instanceof ContextRefreshedEvent && !endOfInit) { endOfInit = true; inShutdown = false; fillBufferThread.setName("WorkerFillBufferThread"); fillBufferThread.start(); } else if (applicationEvent instanceof ContextClosedEvent) { inShutdown = true; } }  void addExecutionMessage(ExecutionMessage msg); @Override void onApplicationEvent(ApplicationEvent applicationEvent); @Override void run(); @Override void doRecovery(); int getCapacity();  }
@Test(timeout = 5000) public void testRunBeforeCtxClosedEvent() throws Exception { ContextRefreshedEvent refreshEvent = mock(ContextRefreshedEvent.class); inBuffer.onApplicationEvent(refreshEvent); ContextClosedEvent event = mock(ContextClosedEvent.class); when(workerManager.isUp()).thenReturn(true); doReturn(true).when(workerStateUpdateService).isWorkerEnabled(); Thread thread = new Thread(inBuffer); thread.start(); verify(workerManager, timeout(1000).atLeastOnce()).getInBufferSize(); inBuffer.onApplicationEvent(event); while (thread.isAlive()) { Thread.sleep(100L); } }