Search in sources :

Example 6 with Flusher

use of org.komamitsu.fluency.flusher.Flusher 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 7 with Flusher

use of org.komamitsu.fluency.flusher.Flusher 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 8 with Flusher

use of org.komamitsu.fluency.flusher.Flusher 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 9 with Flusher

use of org.komamitsu.fluency.flusher.Flusher in project fluency by komamitsu.

the class FluencyBuilder method createFluency.

@VisibleForTesting
public Fluency createFluency(RecordFormatter recordFormatter, Ingester ingester, Buffer.Config bufferConfig, Flusher.Config flusherConfig) {
    Buffer buffer = new Buffer(bufferConfig, recordFormatter);
    Flusher flusher = new Flusher(flusherConfig, buffer, ingester);
    return new Fluency(buffer, flusher);
}
Also used : Buffer(org.komamitsu.fluency.buffer.Buffer) Flusher(org.komamitsu.fluency.flusher.Flusher) VisibleForTesting(org.msgpack.core.annotations.VisibleForTesting)

Aggregations

Buffer (org.komamitsu.fluency.buffer.Buffer)9 Flusher (org.komamitsu.fluency.flusher.Flusher)9 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.jupiter.api.Test)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 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)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 Assertions.fail (org.junit.jupiter.api.Assertions.fail)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1