Search in sources :

Example 6 with Buffer

use of org.komamitsu.fluency.buffer.Buffer in project fluency by komamitsu.

the class FlusherTest method testAsyncFlusher.

@Test
void testAsyncFlusher() throws IOException, InterruptedException {
    flusherConfig.setFlushAttemptIntervalMillis(500);
    Buffer buffer = spy(new Buffer(bufferConfig, new JsonRecordFormatter()));
    Flusher flusher = new Flusher(flusherConfig, buffer, ingester);
    verify(buffer, times(0)).flush(eq(ingester), anyBoolean());
    verify(buffer, times(0)).flush(eq(ingester), anyBoolean());
    flusher.flush();
    TimeUnit.MILLISECONDS.sleep(50);
    verify(buffer, times(0)).flush(eq(ingester), eq(false));
    verify(buffer, times(1)).flush(eq(ingester), eq(true));
    TimeUnit.SECONDS.sleep(1);
    verify(buffer, atLeast(1)).flush(eq(ingester), eq(false));
    verify(buffer, atMost(3)).flush(eq(ingester), eq(false));
    verify(buffer, times(1)).flush(eq(ingester), eq(true));
    verify(buffer, times(0)).close();
    flusher.close();
    verify(buffer, times(1)).close();
    verify(buffer, atLeast(2)).flush(eq(ingester), eq(false));
    verify(buffer, atMost(3)).flush(eq(ingester), eq(false));
    verify(buffer, atLeast(2)).flush(eq(ingester), eq(true));
    verify(buffer, atMost(3)).flush(eq(ingester), eq(true));
}
Also used : Buffer(org.komamitsu.fluency.buffer.Buffer) JsonRecordFormatter(org.komamitsu.fluency.JsonRecordFormatter) Test(org.junit.jupiter.api.Test)

Example 7 with Buffer

use of org.komamitsu.fluency.buffer.Buffer in project fluency by komamitsu.

the class FluencyTest method testWaitUntilFlushingAllBuffer.

@ParameterizedTest
@CsvSource({ "1, false", "3, true" })
void testWaitUntilFlushingAllBuffer(int waitUntilFlusherTerm, boolean expected) throws IOException, InterruptedException {
    flusherConfig.setFlushAttemptIntervalMillis(2000);
    Buffer buffer = new Buffer(bufferConfig, new JsonRecordFormatter());
    Flusher flusher = new Flusher(flusherConfig, buffer, ingester);
    try (Fluency fluency = new Fluency(buffer, flusher)) {
        fluency.emit("foo.bar", new HashMap<>());
        assertThat(fluency.waitUntilAllBufferFlushed(waitUntilFlusherTerm), is(expected));
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(org.komamitsu.fluency.buffer.Buffer) Flusher(org.komamitsu.fluency.flusher.Flusher) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with Buffer

use of org.komamitsu.fluency.buffer.Buffer in project fluency by komamitsu.

the class FluencyTest method testWaitUntilFlusherTerminated.

@ParameterizedTest
@CsvSource({ "1, false", "3, true" })
void testWaitUntilFlusherTerminated(int waitUntilFlusherTerm, boolean expected) throws IOException, InterruptedException {
    flusherConfig.setWaitUntilTerminated(1);
    // Wait before actually closing in Buffer
    int waitBeforeCloseMillis = 2000;
    Buffer buffer = spy(new Buffer(bufferConfig, new JsonRecordFormatter()));
    doAnswer((invocation) -> {
        long start = System.currentTimeMillis();
        try {
            TimeUnit.MILLISECONDS.sleep(waitBeforeCloseMillis);
        } catch (InterruptedException e) {
            long rest = waitBeforeCloseMillis - (System.currentTimeMillis() - start);
            if (rest > 0) {
                try {
                    TimeUnit.MILLISECONDS.sleep(rest);
                } catch (InterruptedException e1) {
                }
            }
        }
        return null;
    }).doCallRealMethod().when(buffer).close();
    Flusher flusher = new Flusher(flusherConfig, buffer, ingester);
    Fluency fluency = new Fluency(buffer, flusher);
    fluency.emit("foo.bar", new HashMap<>());
    fluency.close();
    assertThat(fluency.waitUntilFlusherTerminated(waitUntilFlusherTerm), is(expected));
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(org.komamitsu.fluency.buffer.Buffer) Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) CsvSource(org.junit.jupiter.params.provider.CsvSource) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) ByteBuffer(java.nio.ByteBuffer) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Ingester(org.komamitsu.fluency.ingester.Ingester) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Buffer(org.komamitsu.fluency.buffer.Buffer) Flusher(org.komamitsu.fluency.flusher.Flusher) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) Sender(org.komamitsu.fluency.ingester.sender.Sender) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Flusher(org.komamitsu.fluency.flusher.Flusher) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with Buffer

use of org.komamitsu.fluency.buffer.Buffer in project fluency by komamitsu.

the class FluencyTest method testBufferFullException.

@Test
public void testBufferFullException() throws IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    StuckIngester stuckIngester = new StuckIngester(latch);
    bufferConfig.setChunkInitialSize(64);
    bufferConfig.setChunkExpandRatio(2);
    bufferConfig.setMaxBufferSize(256);
    bufferConfig.setChunkRetentionSize(196);
    flusherConfig.setFlushAttemptIntervalMillis(1000);
    Buffer buffer = new Buffer(bufferConfig, new JsonRecordFormatter());
    Flusher flusher = new Flusher(flusherConfig, buffer, stuckIngester);
    try (Fluency fluency = new Fluency(buffer, flusher)) {
        Map<String, Object> event = new HashMap<>();
        // '{"name":"xxxx"}' (length: 15 bytes)
        event.put("name", "xxxx");
        // 15 * (8 + 1) = 135
        for (int i = 0; i < 8; i++) {
            fluency.emit("tag", event);
        }
        try {
            fluency.emit("tag", event);
            fail();
        } catch (BufferFullException e) {
            assertTrue(true);
        } finally {
            latch.countDown();
        }
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(org.komamitsu.fluency.buffer.Buffer) HashMap(java.util.HashMap) Flusher(org.komamitsu.fluency.flusher.Flusher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with Buffer

use of org.komamitsu.fluency.buffer.Buffer in project fluency by komamitsu.

the class FlusherTest method queueSizeShouldNotExceedLimit.

@Test
void queueSizeShouldNotExceedLimit() throws Exception {
    flusherConfig.setFlushAttemptIntervalMillis(200);
    Buffer buffer = new Buffer(bufferConfig, new JsonRecordFormatter());
    Flusher flusher = new Flusher(flusherConfig, buffer, ingester);
    BlockingQueue<Boolean> eventQueue = (BlockingQueue<Boolean>) extractValue(flusher, "eventQueue");
    int nonZeroCounts = 0;
    for (int index = 0; index < 10_000; index++) {
        flusher.flush();
        if (!eventQueue.isEmpty()) {
            nonZeroCounts++;
        }
        // The eventQueue will always have less that 16 elements (the default max size)
        assertTrue(eventQueue.size() <= 16);
    }
    // The eventQueue will be non empty at least a couple of times
    assertTrue(nonZeroCounts > 0);
    // Wait for sufficiently long (amount of time > queue poll wait) and the queue should be polled completely to become empty
    Thread.sleep(1000);
    assertEquals(0, eventQueue.size());
}
Also used : Buffer(org.komamitsu.fluency.buffer.Buffer) BlockingQueue(java.util.concurrent.BlockingQueue) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) JsonRecordFormatter(org.komamitsu.fluency.JsonRecordFormatter) Test(org.junit.jupiter.api.Test)

Aggregations

Buffer (org.komamitsu.fluency.buffer.Buffer)11 Flusher (org.komamitsu.fluency.flusher.Flusher)9 Test (org.junit.jupiter.api.Test)8 ByteBuffer (java.nio.ByteBuffer)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Fluency (org.komamitsu.fluency.Fluency)3 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CsvSource (org.junit.jupiter.params.provider.CsvSource)2 JsonRecordFormatter (org.komamitsu.fluency.JsonRecordFormatter)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.is (org.hamcrest.Matchers.is)1 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1