### Question:
ElasticsearchAppender extends AbstractAppender { @Override public void stop() { LOGGER.debug("Stopping {}", getClass().getSimpleName()); super.stop(); lifecycleStop(); LOGGER.debug("{} stopped", getClass().getSimpleName()); } protected  ElasticsearchAppender(String name, Filter filter, Layout layout,
            boolean ignoreExceptions, BatchDelivery batchDelivery, boolean messageOnly, IndexNameFormatter indexNameFormatter); void append(LogEvent event); @PluginBuilderFactory static Builder newBuilder(); @Override void start(); @Override void stop(); @Override boolean stop(long timeout, TimeUnit timeUnit); static final String PLUGIN_NAME; }### Answer:
@Test public void lifecycleStopStopsItemAppenderOnlyOnce() { ItemSourceAppender mockItemAppender = mock(ItemSourceAppender.class); when(mockItemAppender.isStopped()).thenAnswer(LifecycleTestHelper.falseOnlyOnce()); ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() { @Override public ItemSourceAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) { return mockItemAppender; } }; TestElasticsearchAppender appender = createTestElasticsearchAppender(itemAppenderFactory); appender.stop(); appender.stop(); verify(mockItemAppender).stop(); }
@Test public void lifecycleStopWithTimeoutStopsItemAppenderOnlyOnce() { ItemSourceAppender mockItemAppender = mock(ItemSourceAppender.class); when(mockItemAppender.isStopped()).thenAnswer(LifecycleTestHelper.falseOnlyOnce()); ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() { @Override public ItemSourceAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) { return mockItemAppender; } }; TestElasticsearchAppender appender = createTestElasticsearchAppender(itemAppenderFactory); appender.stop(0, TimeUnit.MILLISECONDS); appender.stop(0, TimeUnit.MILLISECONDS); verify(mockItemAppender).stop(); assertFalse(appender.isStarted()); assertTrue(appender.isStopped()); }
@Test public void lifecycleStopStopsItemSourceLayout() { StringAppender mockItemAppender = mock(StringAppender.class); when(mockItemAppender.isStopped()).thenAnswer(LifecycleTestHelper.falseOnlyOnce()); ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() { @Override public ItemAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) { return mockItemAppender; } }; JacksonJsonLayout layout = mock(JacksonJsonLayout.class); TestElasticsearchAppender appender = createTestElasticsearchAppender(itemAppenderFactory, layout); appender.stop(0, TimeUnit.MILLISECONDS); assertFalse(appender.isStarted()); assertTrue(appender.isStopped()); verify(layout).stop(); }
@Test public void lifecycleStopDoesntInteractWithAbstractLayout() { StringAppender mockItemAppender = mock(StringAppender.class); when(mockItemAppender.isStopped()).thenAnswer(LifecycleTestHelper.falseOnlyOnce()); ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() { @Override public ItemAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) { return mockItemAppender; } }; AbstractLayout abstractLayout = mock(AbstractLayout.class); TestElasticsearchAppender appender = createTestElasticsearchAppender(itemAppenderFactory, abstractLayout); appender.stop(0, TimeUnit.MILLISECONDS); assertTrue(mockingDetails(abstractLayout).getInvocations().size() == 0); assertFalse(appender.isStarted()); assertTrue(appender.isStopped()); }