public void appendAndFlush(FilteredMessage filteredMessage, Consumer<List<FilteredMessage>> flusher) {
        List<FilteredMessage> flushBatch = null;
        synchronized (this) {
            // See class the class documentation for the reasoning behind the bufferLength calculation.
            buffer.add(filteredMessage);
            bufferLength += Math.max(filteredMessage.message().getIndexSets().size(), 1);
            bufferSizeBytes += estimateOsBulkRequestSize(filteredMessage.message(), objectMapper);

            if ((maxBufferSizeBytes != 0L && bufferSizeBytes >= maxBufferSizeBytes) ||
                    maxBufferSizeCount != 0 && bufferLength >= maxBufferSizeCount) {
                flushBatch = buffer;
                buffer = new ArrayList<>(bufferLength);
                bufferLength = 0;
                bufferSizeBytes = 0L;
            }
        }
        // if the current thread found it had to flush any messages, it does so but blocks.
        // this ensures we don't flush more than 'processorCount' in parallel.
        // TODO this will still be time limited by the OutputBufferProcessor and thus be called more often than it should
        if (flushBatch != null) {
            lastFlushTime.set(System.nanoTime());
            flusher.accept(flushBatch);
        }
    }
@Test
    void appendAndFlush() {
        final var messages = createNMessages(6);

        // With a buffer size of 5, only the 5th call should flush the messages.
        buffer.appendAndFlush(messages.get(0), flusher);
        verifyNoInteractions(flusher);
        buffer.appendAndFlush(messages.get(1), flusher);
        verifyNoInteractions(flusher);
        buffer.appendAndFlush(messages.get(2), flusher);
        verifyNoInteractions(flusher);
        buffer.appendAndFlush(messages.get(3), flusher);
        verifyNoInteractions(flusher);
        buffer.appendAndFlush(messages.get(4), flusher);
        // Flusher should only get the first 5 messages.
        verify(flusher, times(1)).accept(messages.subList(0, 5));

        // The 6th message shouldn't trigger a flush.
        buffer.appendAndFlush(messages.get(5), flusher);
        verifyNoMoreInteractions(flusher);
    }