From bd25a560068c37598f95acf117acb85f53d3107c Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Sat, 2 Nov 2024 23:32:43 +0900 Subject: [PATCH 1/5] Adding some tests in FileBackupTest --- build.gradle.kts | 2 +- .../komamitsu/fluency/buffer/FileBackup.java | 17 +++- .../org/komamitsu/fluency/FluencyTest.java | 11 ++- .../komamitsu/fluency/buffer/BufferTest.java | 17 ++-- .../fluency/buffer/FileBackupTest.java | 81 +++++++++++++++++++ 5 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java diff --git a/build.gradle.kts b/build.gradle.kts index 4d6e4fd1..562d7fd9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,7 +42,7 @@ subprojects { testImplementation("org.junit.jupiter:junit-jupiter-engine:5.10.2") testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.2") testImplementation("ch.qos.logback:logback-classic:1.3.14") - testImplementation("org.hamcrest:hamcrest-all:1.3") + testImplementation("org.assertj:assertj-core:3.26.3") testImplementation("org.mockito:mockito-core:4.11.0") testImplementation("com.google.guava:guava:33.1.0-jre") } diff --git a/fluency-core/src/main/java/org/komamitsu/fluency/buffer/FileBackup.java b/fluency-core/src/main/java/org/komamitsu/fluency/buffer/FileBackup.java index b8e17eb0..e0843fae 100644 --- a/fluency-core/src/main/java/org/komamitsu/fluency/buffer/FileBackup.java +++ b/fluency-core/src/main/java/org/komamitsu/fluency/buffer/FileBackup.java @@ -26,6 +26,7 @@ import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; @@ -54,7 +55,7 @@ public FileBackup(File backupDir, Buffer userBuffer, String prefix) this.backupDir = backupDir; this.userBuffer = userBuffer; this.prefix = prefix; - this.pattern = Pattern.compile(userBuffer.bufferFormatType() + prefix() + PARAM_DELIM_IN_FILENAME + "([\\w\\.\\-" + PARAM_DELIM_IN_FILENAME + "]+)" + EXT_FILENAME); + this.pattern = Pattern.compile("^" + userBuffer.bufferFormatType() + prefix() + PARAM_DELIM_IN_FILENAME + "([\\w\\.\\-" + PARAM_DELIM_IN_FILENAME + "]+)" + EXT_FILENAME + "$"); LOG.debug(this.toString()); } @@ -85,7 +86,7 @@ public List getSavedFiles() LOG.debug("Checking backup files. files.length={}", files.length); ArrayList savedBuffers = new ArrayList<>(); for (File f : files) { - Matcher matcher = pattern.matcher(f.getName()); + Matcher matcher = pattern.matcher(f.toPath().getFileName().toString()); if (matcher.find()) { if (matcher.groupCount() != 1) { LOG.warn("Invalid backup filename: file={}", f.getName()); @@ -210,6 +211,18 @@ public void close() } } + @Override + public String toString() { + return "SavedBuffer{" + + "params=" + params + + ", savedFile=" + savedFile + + '}'; + } + + public Path getPath() { + return savedFile.toPath(); + } + public interface Callback { void process(List params, FileChannel channel); diff --git a/fluency-core/src/test/java/org/komamitsu/fluency/FluencyTest.java b/fluency-core/src/test/java/org/komamitsu/fluency/FluencyTest.java index 0695aac2..bebfe9d0 100644 --- a/fluency-core/src/test/java/org/komamitsu/fluency/FluencyTest.java +++ b/fluency-core/src/test/java/org/komamitsu/fluency/FluencyTest.java @@ -35,8 +35,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -83,9 +82,9 @@ void testGetAllocatedBufferSize() Buffer buffer = new Buffer(bufferConfig, new JsonRecordFormatter()); Flusher flusher = new Flusher(flusherConfig, buffer, ingester); try (Fluency fluency = new Fluency(buffer, flusher)) { - assertThat(fluency.getAllocatedBufferSize(), is(0L)); + assertThat(fluency.getAllocatedBufferSize()).isEqualTo(0L); fluency.emit("foodb.bartbl", ImmutableMap.of("comment", "hello, world")); - assertThat(fluency.getAllocatedBufferSize(), is(1024L)); + assertThat(fluency.getAllocatedBufferSize()).isEqualTo(1024L); } } @@ -122,7 +121,7 @@ void testWaitUntilFlusherTerminated(int waitUntilFlusherTerm, boolean expected) fluency.emit("foo.bar", new HashMap<>()); fluency.close(); - assertThat(fluency.waitUntilFlusherTerminated(waitUntilFlusherTerm), is(expected)); + assertThat(fluency.waitUntilFlusherTerminated(waitUntilFlusherTerm)).isEqualTo(expected); } @ParameterizedTest @@ -136,7 +135,7 @@ void testWaitUntilFlushingAllBuffer(int waitUntilFlusherTerm, boolean expected) 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)); + assertThat(fluency.waitUntilAllBufferFlushed(waitUntilFlusherTerm)).isEqualTo(expected); } } diff --git a/fluency-core/src/test/java/org/komamitsu/fluency/buffer/BufferTest.java b/fluency-core/src/test/java/org/komamitsu/fluency/buffer/BufferTest.java index 35c83248..ec02867c 100644 --- a/fluency-core/src/test/java/org/komamitsu/fluency/buffer/BufferTest.java +++ b/fluency-core/src/test/java/org/komamitsu/fluency/buffer/BufferTest.java @@ -40,10 +40,7 @@ import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.number.OrderingComparison.greaterThan; -import static org.hamcrest.number.OrderingComparison.lessThan; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -337,13 +334,13 @@ void testGetAllocatedSize() { bufferConfig.setChunkInitialSize(256 * 1024); try (Buffer buffer = new Buffer(bufferConfig, recordFormatter)) { - assertThat(buffer.getAllocatedSize(), is(0L)); + assertThat(buffer.getAllocatedSize()).isEqualTo(0L); Map map = new HashMap<>(); map.put("name", "komamitsu"); for (int i = 0; i < 10; i++) { buffer.append("foo.bar", new Date().getTime(), map); } - assertThat(buffer.getAllocatedSize(), is(256 * 1024L)); + assertThat(buffer.getAllocatedSize()).isEqualTo(256 * 1024L); } } @@ -353,18 +350,18 @@ void testGetBufferedDataSize() { bufferConfig.setChunkInitialSize(256 * 1024); try (Buffer buffer = new Buffer(bufferConfig, recordFormatter)) { - assertThat(buffer.getBufferedDataSize(), is(0L)); + assertThat(buffer.getBufferedDataSize()).isEqualTo(0L); Map map = new HashMap<>(); map.put("name", "komamitsu"); for (int i = 0; i < 10; i++) { buffer.append("foo.bar", new Date().getTime(), map); } - assertThat(buffer.getBufferedDataSize(), is(greaterThan(0L))); - assertThat(buffer.getBufferedDataSize(), is(lessThan(512L))); + assertThat(buffer.getBufferedDataSize()).isGreaterThan(0L); + assertThat(buffer.getBufferedDataSize()).isLessThan(512L); buffer.flush(ingester, true); - assertThat(buffer.getBufferedDataSize(), is(0L)); + assertThat(buffer.getBufferedDataSize()).isEqualTo(0L); } } diff --git a/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java b/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java new file mode 100644 index 00000000..31de0981 --- /dev/null +++ b/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java @@ -0,0 +1,81 @@ +package org.komamitsu.fluency.buffer; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +class FileBackupTest { + + private void createTempFile(File dir, String filename) throws IOException { + Path tempfilePath = Files.createFile(dir.toPath().resolve(filename)); + tempfilePath.toFile().deleteOnExit(); + } + + @Test + void getSavedFiles_GivenEmptyFiles_ShouldReturnEmpty() throws IOException { + File backupDir = Files.createTempDirectory("test").toFile(); + Buffer buffer = mock(Buffer.class); + String prefix = "my_prefix"; + FileBackup fileBackup = new FileBackup(backupDir, buffer, prefix); + + assertThat(fileBackup.getSavedFiles()).isEmpty(); + } + + @Test + void getSavedFiles_GivenSomeFiles_ShouldReturnThem() throws IOException { + long nanoSeconds1 = System.nanoTime(); + long nanoSeconds2 = System.nanoTime(); + long nanoSeconds3 = System.nanoTime(); + File backupDir = Files.createTempDirectory("test").toFile(); + backupDir.deleteOnExit(); + createTempFile(backupDir, + String.format("xmy_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime())); + createTempFile(backupDir, + String.format("xmy_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime())); + createTempFile(backupDir, + String.format("y_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime())); + createTempFile(backupDir, + String.format("my_buf_type_my_prefix#%d#paramA#paramB.buf", nanoSeconds1)); + createTempFile(backupDir, + String.format("my_buf_type_my_prefix#%d#param-a#param-b.buf", nanoSeconds2)); + createTempFile(backupDir, + String.format("my_buf_type_my_prefix#%d#param_a#param_b.buf", nanoSeconds3)); + createTempFile(backupDir, + String.format("my_buf_type_my_prefixz#%d#param_a#param_b.buf", System.nanoTime())); + createTempFile(backupDir, + String.format("my_buf_type_my_prefi#%d#param_a#param_b.buf", System.nanoTime())); + createTempFile(backupDir, + String.format("my_buf_type_my_prefix#%d#param:a#param:b.buf", System.nanoTime())); + createTempFile(backupDir, + String.format("my_buf_type_my_prefix#%d#param_a#param_b", System.nanoTime())); + createTempFile(backupDir, + String.format("my_buf_type_my_prefix#%d#param_a#param_b.buff", System.nanoTime())); + Buffer buffer = mock(Buffer.class); + doReturn("my_buf_type").when(buffer).bufferFormatType(); + String prefix = "my_prefix"; + FileBackup fileBackup = new FileBackup(backupDir, buffer, prefix); + + List savedFiles = fileBackup.getSavedFiles().stream().sorted( + Comparator.comparing(FileBackup.SavedBuffer::getPath)).collect(Collectors.toList()); + System.out.println(savedFiles); + assertThat(savedFiles).size().isEqualTo(3); + assertThat(savedFiles.get(0).getPath()).isEqualTo(backupDir.toPath().resolve( + String.format("my_buf_type_my_prefix#%d#paramA#paramB.buf", nanoSeconds1))); + assertThat(savedFiles.get(1).getPath()).isEqualTo(backupDir.toPath().resolve( + String.format("my_buf_type_my_prefix#%d#param-a#param-b.buf", nanoSeconds2))); + assertThat(savedFiles.get(2).getPath()).isEqualTo(backupDir.toPath().resolve( + String.format("my_buf_type_my_prefix#%d#param_a#param_b.buf", nanoSeconds3))); + + // TODO: Check the file contents. + } +} \ No newline at end of file From 0b7209e4111649776b62bd871df414a20faab3ec Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Sun, 3 Nov 2024 20:08:36 +0900 Subject: [PATCH 2/5] Add some assertions --- .../fluency/buffer/FileBackupTest.java | 71 ++++++++++++++----- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java b/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java index 31de0981..697a1d20 100644 --- a/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java +++ b/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java @@ -4,8 +4,11 @@ import java.io.File; import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -16,11 +19,28 @@ class FileBackupTest { - private void createTempFile(File dir, String filename) throws IOException { + private void createTempFile(File dir, String filename, String content) throws IOException { Path tempfilePath = Files.createFile(dir.toPath().resolve(filename)); + Files.write(tempfilePath, content.getBytes(StandardCharsets.UTF_8)); tempfilePath.toFile().deleteOnExit(); } + private void assertSavedBuffer(FileBackup.SavedBuffer savedBuffer, Path expectedPath, byte[] expectedContent, String ... expectedParams) { + assertThat(savedBuffer.getPath()).isEqualTo(expectedPath); + savedBuffer.open((params, channel) -> { + // FIXME + assertThat(params).isEqualTo(Arrays.stream(expectedParams).limit(expectedParams.length - 1).collect(Collectors.toList())); + try { + long size = channel.size(); + ByteBuffer buf = ByteBuffer.allocate((int) size); + channel.read(buf); + assertThat(buf.array()).isEqualTo(expectedContent); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + @Test void getSavedFiles_GivenEmptyFiles_ShouldReturnEmpty() throws IOException { File backupDir = Files.createTempDirectory("test").toFile(); @@ -39,27 +59,27 @@ void getSavedFiles_GivenSomeFiles_ShouldReturnThem() throws IOException { File backupDir = Files.createTempDirectory("test").toFile(); backupDir.deleteOnExit(); createTempFile(backupDir, - String.format("xmy_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime())); + String.format("xmy_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); createTempFile(backupDir, - String.format("xmy_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime())); + String.format("xmy_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); createTempFile(backupDir, - String.format("y_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime())); + String.format("y_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#paramA#paramB.buf", nanoSeconds1)); + String.format("my_buf_type_my_prefix#%d#paramA#paramB.buf", nanoSeconds1), "content1"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param-a#param-b.buf", nanoSeconds2)); + String.format("my_buf_type_my_prefix#%d#param-a#param-b.buf", nanoSeconds2), "content2"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param_a#param_b.buf", nanoSeconds3)); + String.format("my_buf_type_my_prefix#%d#param_a#param_b.buf", nanoSeconds3), "content3"); createTempFile(backupDir, - String.format("my_buf_type_my_prefixz#%d#param_a#param_b.buf", System.nanoTime())); + String.format("my_buf_type_my_prefixz#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefi#%d#param_a#param_b.buf", System.nanoTime())); + String.format("my_buf_type_my_prefi#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param:a#param:b.buf", System.nanoTime())); + String.format("my_buf_type_my_prefix#%d#param:a#param:b.buf", System.nanoTime()), "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param_a#param_b", System.nanoTime())); + String.format("my_buf_type_my_prefix#%d#param_a#param_b", System.nanoTime()), "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param_a#param_b.buff", System.nanoTime())); + String.format("my_buf_type_my_prefix#%d#param_a#param_b.buff", System.nanoTime()), "ignored"); Buffer buffer = mock(Buffer.class); doReturn("my_buf_type").when(buffer).bufferFormatType(); String prefix = "my_prefix"; @@ -69,13 +89,26 @@ void getSavedFiles_GivenSomeFiles_ShouldReturnThem() throws IOException { Comparator.comparing(FileBackup.SavedBuffer::getPath)).collect(Collectors.toList()); System.out.println(savedFiles); assertThat(savedFiles).size().isEqualTo(3); - assertThat(savedFiles.get(0).getPath()).isEqualTo(backupDir.toPath().resolve( - String.format("my_buf_type_my_prefix#%d#paramA#paramB.buf", nanoSeconds1))); - assertThat(savedFiles.get(1).getPath()).isEqualTo(backupDir.toPath().resolve( - String.format("my_buf_type_my_prefix#%d#param-a#param-b.buf", nanoSeconds2))); - assertThat(savedFiles.get(2).getPath()).isEqualTo(backupDir.toPath().resolve( - String.format("my_buf_type_my_prefix#%d#param_a#param_b.buf", nanoSeconds3))); - // TODO: Check the file contents. + assertSavedBuffer(savedFiles.get(0), + backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#%d#paramA#paramB.buf", nanoSeconds1)), + "content1".getBytes(StandardCharsets.UTF_8), + String.valueOf(nanoSeconds1), + "paramA", + "paramB"); + + assertSavedBuffer(savedFiles.get(1), + backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#%d#param-a#param-b.buf", nanoSeconds2)), + "content2".getBytes(StandardCharsets.UTF_8), + String.valueOf(nanoSeconds2), + "param-a", + "param-b"); + + assertSavedBuffer(savedFiles.get(2), + backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#%d#param_a#param_b.buf", nanoSeconds3)), + "content3".getBytes(StandardCharsets.UTF_8), + String.valueOf(nanoSeconds3), + "param_a", + "param_b"); } } \ No newline at end of file From 4ee9535289df3c2142c461d1e570b12465759b8d Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Sun, 3 Nov 2024 22:58:27 +0900 Subject: [PATCH 3/5] Add test to FileBackupTest --- .../fluency/buffer/FileBackupTest.java | 135 ++++++++++++++---- 1 file changed, 105 insertions(+), 30 deletions(-) diff --git a/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java b/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java index 697a1d20..02261888 100644 --- a/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java +++ b/fluency-core/src/test/java/org/komamitsu/fluency/buffer/FileBackupTest.java @@ -11,6 +11,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; @@ -25,11 +26,10 @@ private void createTempFile(File dir, String filename, String content) throws IO tempfilePath.toFile().deleteOnExit(); } - private void assertSavedBuffer(FileBackup.SavedBuffer savedBuffer, Path expectedPath, byte[] expectedContent, String ... expectedParams) { + private void assertSavedBuffer(FileBackup.SavedBuffer savedBuffer, Path expectedPath, byte[] expectedContent, String... expectedParams) { assertThat(savedBuffer.getPath()).isEqualTo(expectedPath); savedBuffer.open((params, channel) -> { - // FIXME - assertThat(params).isEqualTo(Arrays.stream(expectedParams).limit(expectedParams.length - 1).collect(Collectors.toList())); + assertThat(params.toArray()).isEqualTo(expectedParams); try { long size = channel.size(); ByteBuffer buf = ByteBuffer.allocate((int) size); @@ -41,13 +41,33 @@ private void assertSavedBuffer(FileBackup.SavedBuffer savedBuffer, Path expected }); } + private void assertSavedFile(File savedFile, + String bufferFormatType, + String prefix, + long startNanos, + long endNanos, + String param1, + String param2, + byte[] expectedContent) throws IOException { + String fileName = savedFile.toPath().getFileName().toString(); + assertThat(fileName).endsWith(".buf"); + + String[] partsOfPath = fileName.substring(0, fileName.length() - ".buf".length()).split("#"); + assertThat(partsOfPath).hasSize(4); + assertThat(partsOfPath[0]).isEqualTo(bufferFormatType + "_" + prefix); + assertThat(partsOfPath[1]).isEqualTo(param1); + assertThat(partsOfPath[2]).isEqualTo(param2); + assertThat(Long.valueOf(partsOfPath[3])).isBetween(startNanos, endNanos); + assertThat(Files.readAllBytes(savedFile.toPath())).isEqualTo(expectedContent); + } + @Test void getSavedFiles_GivenEmptyFiles_ShouldReturnEmpty() throws IOException { File backupDir = Files.createTempDirectory("test").toFile(); + backupDir.deleteOnExit(); Buffer buffer = mock(Buffer.class); String prefix = "my_prefix"; FileBackup fileBackup = new FileBackup(backupDir, buffer, prefix); - assertThat(fileBackup.getSavedFiles()).isEmpty(); } @@ -59,27 +79,38 @@ void getSavedFiles_GivenSomeFiles_ShouldReturnThem() throws IOException { File backupDir = Files.createTempDirectory("test").toFile(); backupDir.deleteOnExit(); createTempFile(backupDir, - String.format("xmy_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); + String.format("xmy_buf_type_my_prefix#param_a#param_b#%d.buf", System.nanoTime()), + "ignored"); createTempFile(backupDir, - String.format("xmy_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); + String.format("xmy_buf_type_my_prefix#param_a#param_b#%d.buf", System.nanoTime()), + "ignored"); createTempFile(backupDir, - String.format("y_buf_type_my_prefix#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); + String.format("y_buf_type_my_prefix#param_a#param_b#%d.buf", System.nanoTime()), + "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#paramA#paramB.buf", nanoSeconds1), "content1"); + String.format("my_buf_type_my_prefix#1paramA#1paramB#%d.buf", nanoSeconds1), + "content1"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param-a#param-b.buf", nanoSeconds2), "content2"); + String.format("my_buf_type_my_prefix#2param-a#2param-b#%d.buf", nanoSeconds2), + "content2"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param_a#param_b.buf", nanoSeconds3), "content3"); + String.format("my_buf_type_my_prefix#3param_a#3param_b#%d.buf", nanoSeconds3), + "content3"); createTempFile(backupDir, - String.format("my_buf_type_my_prefixz#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); + String.format("my_buf_type_my_prefixz#param_a#param_b#%d.buf", System.nanoTime()), + "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefi#%d#param_a#param_b.buf", System.nanoTime()), "ignored"); + String.format("my_buf_type_my_prefi#param_a#param_b#%d.buf", System.nanoTime()), + "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param:a#param:b.buf", System.nanoTime()), "ignored"); + String.format("my_buf_type_my_prefix#param:a#param:b#%d.buf", System.nanoTime()), + "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param_a#param_b", System.nanoTime()), "ignored"); + String.format("my_buf_type_my_prefix#param_a#param_b#%d", System.nanoTime()), + "ignored"); createTempFile(backupDir, - String.format("my_buf_type_my_prefix#%d#param_a#param_b.buff", System.nanoTime()), "ignored"); + String.format("my_buf_type_my_prefix#param_a#param_b#%d.buff", System.nanoTime()), + "ignored"); Buffer buffer = mock(Buffer.class); doReturn("my_buf_type").when(buffer).bufferFormatType(); String prefix = "my_prefix"; @@ -89,26 +120,70 @@ void getSavedFiles_GivenSomeFiles_ShouldReturnThem() throws IOException { Comparator.comparing(FileBackup.SavedBuffer::getPath)).collect(Collectors.toList()); System.out.println(savedFiles); assertThat(savedFiles).size().isEqualTo(3); - assertSavedBuffer(savedFiles.get(0), - backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#%d#paramA#paramB.buf", nanoSeconds1)), + backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#1paramA#1paramB#%d.buf", nanoSeconds1)), "content1".getBytes(StandardCharsets.UTF_8), - String.valueOf(nanoSeconds1), - "paramA", - "paramB"); - + "1paramA", + "1paramB"); assertSavedBuffer(savedFiles.get(1), - backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#%d#param-a#param-b.buf", nanoSeconds2)), + backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#2param-a#2param-b#%d.buf", nanoSeconds2)), "content2".getBytes(StandardCharsets.UTF_8), - String.valueOf(nanoSeconds2), - "param-a", - "param-b"); - + "2param-a", + "2param-b"); assertSavedBuffer(savedFiles.get(2), - backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#%d#param_a#param_b.buf", nanoSeconds3)), + backupDir.toPath().resolve(String.format("my_buf_type_my_prefix#3param_a#3param_b#%d.buf", nanoSeconds3)), "content3".getBytes(StandardCharsets.UTF_8), - String.valueOf(nanoSeconds3), - "param_a", - "param_b"); + "3param_a", + "3param_b"); + } + + @Test + void saveBuffer() throws IOException { + File backupDir = Files.createTempDirectory("test").toFile(); + backupDir.deleteOnExit(); + Buffer buffer = mock(Buffer.class); + doReturn("my_buf_type").when(buffer).bufferFormatType(); + String prefix = "my_prefix"; + FileBackup fileBackup = new FileBackup(backupDir, buffer, prefix); + long startNanos = System.nanoTime(); + fileBackup.saveBuffer( + Arrays.asList("1paramA", "1paramB"), + ByteBuffer.wrap("content1".getBytes(StandardCharsets.UTF_8))); + fileBackup.saveBuffer( + Arrays.asList("2param-a", "2param-b"), + ByteBuffer.wrap("content2".getBytes(StandardCharsets.UTF_8))); + fileBackup.saveBuffer( + Arrays.asList("3param_a", "3param_b"), + ByteBuffer.wrap("content3".getBytes(StandardCharsets.UTF_8))); + long endNanos = System.nanoTime(); + + List savedFiles = Arrays.stream(Objects.requireNonNull(backupDir.listFiles())) + .sorted(Comparator.comparing(File::toString)) + .collect(Collectors.toList()); + assertThat(savedFiles).size().isEqualTo(3); + assertSavedFile(savedFiles.get(0), + "my_buf_type", + "my_prefix", + startNanos, + endNanos, + "1paramA", + "1paramB", + "content1".getBytes(StandardCharsets.UTF_8)); + assertSavedFile(savedFiles.get(1), + "my_buf_type", + "my_prefix", + startNanos, + endNanos, + "2param-a", + "2param-b", + "content2".getBytes(StandardCharsets.UTF_8)); + assertSavedFile(savedFiles.get(2), + "my_buf_type", + "my_prefix", + startNanos, + endNanos, + "3param_a", + "3param_b", + "content3".getBytes(StandardCharsets.UTF_8)); } } \ No newline at end of file From 0fde79e09cb395d654b0cb763412945a7aa0f2c9 Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Mon, 4 Nov 2024 00:00:35 +0900 Subject: [PATCH 4/5] Minor refactoring --- .../org/komamitsu/fluency/buffer/FileBackup.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/fluency-core/src/main/java/org/komamitsu/fluency/buffer/FileBackup.java b/fluency-core/src/main/java/org/komamitsu/fluency/buffer/FileBackup.java index e0843fae..257aca4c 100644 --- a/fluency-core/src/main/java/org/komamitsu/fluency/buffer/FileBackup.java +++ b/fluency-core/src/main/java/org/komamitsu/fluency/buffer/FileBackup.java @@ -55,7 +55,7 @@ public FileBackup(File backupDir, Buffer userBuffer, String prefix) this.backupDir = backupDir; this.userBuffer = userBuffer; this.prefix = prefix; - this.pattern = Pattern.compile("^" + userBuffer.bufferFormatType() + prefix() + PARAM_DELIM_IN_FILENAME + "([\\w\\.\\-" + PARAM_DELIM_IN_FILENAME + "]+)" + EXT_FILENAME + "$"); + this.pattern = Pattern.compile(String.format("^%s%s%s([\\w.\\-%s]+)%s$", userBuffer.bufferFormatType(), prefix(), PARAM_DELIM_IN_FILENAME, PARAM_DELIM_IN_FILENAME, EXT_FILENAME)); LOG.debug(this.toString()); } @@ -84,7 +84,7 @@ public List getSavedFiles() } LOG.debug("Checking backup files. files.length={}", files.length); - ArrayList savedBuffers = new ArrayList<>(); + List savedBuffers = new ArrayList<>(); for (File f : files) { Matcher matcher = pattern.matcher(f.toPath().getFileName().toString()); if (matcher.find()) { @@ -134,7 +134,7 @@ public void saveBuffer(List params, ByteBuffer buffer) channel.write(buffer); } catch (Exception e) { - LOG.error("Failed to save buffer to file: params=" + copiedParams + ", path=" + file.getAbsolutePath() + ", buffer=" + buffer, e); + LOG.error("Failed to save buffer to file: params={}, path={}, buffer={}", copiedParams, file.getAbsolutePath(), buffer, e); } finally { if (channel != null) { @@ -142,7 +142,7 @@ public void saveBuffer(List params, ByteBuffer buffer) channel.close(); } catch (IOException e) { - LOG.warn("Failed to close Channel: channel=" + channel); + LOG.warn("Failed to close Channel: channel={}", channel); } } } @@ -169,14 +169,14 @@ public void open(Callback callback) success(); } catch (Exception e) { - LOG.error("Failed to process file. Skipping the file: file=" + savedFile, e); + LOG.error("Failed to process file. Skipping the file: file={}", savedFile, e); } finally { try { close(); } catch (IOException e) { - LOG.warn("Failed to close file: file=" + savedFile, e); + LOG.warn("Failed to close file: file={}", savedFile, e); } } } @@ -184,7 +184,7 @@ public void open(Callback callback) public void remove() { if (!savedFile.delete()) { - LOG.warn("Failed to delete file: file=" + savedFile); + LOG.warn("Failed to delete file: file={}", savedFile); } } @@ -194,7 +194,7 @@ private void success() close(); } catch (IOException e) { - LOG.warn("Failed to close file: file=" + savedFile, e); + LOG.warn("Failed to close file: file={}", savedFile, e); } finally { remove(); From 35fbc63f15d353d66a7000854283d6703de10978 Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Mon, 4 Nov 2024 18:48:49 +0900 Subject: [PATCH 5/5] Fix tests --- .../FluencyExtBuilderForFluentdTest.java | 61 +++--- .../ingester/sender/UnixSocketSenderTest.java | 46 ++-- .../fluency/buffer/BufferForFluentdTest.java | 31 ++- .../fluency/fluentd/EventTimeTest.java | 48 ++--- .../fluentd/FluencyBuilderForFluentdTest.java | 199 +++++++++--------- .../fluency/fluentd/FluencyTest.java | 8 +- .../fluentd/FluencyTestWithMockServer.java | 23 +- .../fluentd/ingester/sender/ConfigTest.java | 7 +- .../ingester/sender/MultiSenderTest.java | 14 +- .../ingester/sender/RetryableSenderTest.java | 9 +- .../ingester/sender/SSLSenderTest.java | 51 ++--- .../ingester/sender/TCPSenderTest.java | 47 +++-- 12 files changed, 262 insertions(+), 282 deletions(-) diff --git a/fluency-fluentd-ext/src/test/java/org/komamitsu/fluency/fluentd/FluencyExtBuilderForFluentdTest.java b/fluency-fluentd-ext/src/test/java/org/komamitsu/fluency/fluentd/FluencyExtBuilderForFluentdTest.java index 51fd1f95..715062a4 100644 --- a/fluency-fluentd-ext/src/test/java/org/komamitsu/fluency/fluentd/FluencyExtBuilderForFluentdTest.java +++ b/fluency-fluentd-ext/src/test/java/org/komamitsu/fluency/fluentd/FluencyExtBuilderForFluentdTest.java @@ -4,13 +4,10 @@ import org.komamitsu.fluency.Fluency; import org.komamitsu.fluency.buffer.Buffer; import org.komamitsu.fluency.fluentd.ingester.sender.FluentdSender; -import org.komamitsu.fluency.fluentd.ingester.sender.MultiSender; import org.komamitsu.fluency.fluentd.ingester.sender.RetryableSender; import org.komamitsu.fluency.fluentd.ingester.sender.UnixSocketSender; import org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.FailureDetector; import org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.PhiAccrualFailureDetectStrategy; -import org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.Heartbeater; -import org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.SSLHeartbeater; import org.komamitsu.fluency.fluentd.ingester.sender.heartbeat.UnixSocketHeartbeater; import org.komamitsu.fluency.fluentd.ingester.sender.retry.ExponentialBackOffRetryStrategy; import org.komamitsu.fluency.flusher.Flusher; @@ -18,70 +15,66 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; -import java.util.stream.Stream; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; class FluencyExtBuilderForFluentdTest { // These assertMethods are copied from FluencyBuilderForFluentdTest private void assertBuffer(Buffer buffer) { - assertThat(buffer.getMaxBufferSize(), is(512 * 1024 * 1024L)); - assertThat(buffer.getFileBackupDir(), is(nullValue())); - assertThat(buffer.bufferFormatType(), is("packed_forward")); - assertThat(buffer.getChunkExpandRatio(), is(2f)); - assertThat(buffer.getChunkRetentionSize(), is(4 * 1024 * 1024)); - assertThat(buffer.getChunkInitialSize(), is(1 * 1024 * 1024)); - assertThat(buffer.getChunkRetentionTimeMillis(), is(1000)); - assertThat(buffer.getJvmHeapBufferMode(), is(false)); + assertThat(buffer.getMaxBufferSize()).isEqualTo(512 * 1024 * 1024L); + assertThat(buffer.getFileBackupDir()).isNull(); + assertThat(buffer.bufferFormatType()).isEqualTo("packed_forward"); + assertThat(buffer.getChunkExpandRatio()).isEqualTo(2f); + assertThat(buffer.getChunkRetentionSize()).isEqualTo(4 * 1024 * 1024); + assertThat(buffer.getChunkInitialSize()).isEqualTo(1 * 1024 * 1024); + assertThat(buffer.getChunkRetentionTimeMillis()).isEqualTo(1000); + assertThat(buffer.getJvmHeapBufferMode()).isFalse(); } private void assertFlusher(Flusher flusher) { - assertThat(flusher.isTerminated(), is(false)); - assertThat(flusher.getFlushAttemptIntervalMillis(), is(600)); - assertThat(flusher.getWaitUntilBufferFlushed(), is(60)); - assertThat(flusher.getWaitUntilTerminated(), is(60)); + assertThat(flusher.isTerminated()).isFalse(); + assertThat(flusher.getFlushAttemptIntervalMillis()).isEqualTo(600); + assertThat(flusher.getWaitUntilBufferFlushed()).isEqualTo(60); + assertThat(flusher.getWaitUntilTerminated()).isEqualTo(60); } private void assertDefaultRetryableSender(RetryableSender sender, Class expectedBaseClass) { - assertThat(sender.getRetryStrategy(), instanceOf(ExponentialBackOffRetryStrategy.class)); + assertThat(sender.getRetryStrategy()).isInstanceOf(ExponentialBackOffRetryStrategy.class); ExponentialBackOffRetryStrategy retryStrategy = (ExponentialBackOffRetryStrategy) sender.getRetryStrategy(); - assertThat(retryStrategy.getMaxRetryCount(), is(7)); - assertThat(retryStrategy.getBaseIntervalMillis(), is(400)); - assertThat(sender.getBaseSender(), instanceOf(expectedBaseClass)); + assertThat(retryStrategy.getMaxRetryCount()).isEqualTo(7); + assertThat(retryStrategy.getBaseIntervalMillis()).isEqualTo(400); + assertThat(sender.getBaseSender()).isInstanceOf(expectedBaseClass); } private void assertUnixSocketSender(UnixSocketSender sender, Path expectedPath, boolean shouldHaveFailureDetector) { - assertThat(sender.getPath(), is(expectedPath)); - assertThat(sender.getConnectionTimeoutMilli(), is(5000)); - assertThat(sender.getReadTimeoutMilli(), is(5000)); + assertThat(sender.getPath()).isEqualTo(expectedPath); + assertThat(sender.getConnectionTimeoutMilli()).isEqualTo(5000); + assertThat(sender.getReadTimeoutMilli()).isEqualTo(5000); FailureDetector failureDetector = sender.getFailureDetector(); if (shouldHaveFailureDetector) { - assertThat(failureDetector.getFailureIntervalMillis(), is(3 * 1000)); - assertThat(failureDetector.getFailureDetectStrategy(), instanceOf(PhiAccrualFailureDetectStrategy.class)); - assertThat(failureDetector.getHeartbeater(), instanceOf(UnixSocketHeartbeater.class)); + assertThat(failureDetector.getFailureIntervalMillis()).isEqualTo(3 * 1000); + assertThat(failureDetector.getFailureDetectStrategy()).isInstanceOf(PhiAccrualFailureDetectStrategy.class); + assertThat(failureDetector.getHeartbeater()).isInstanceOf(UnixSocketHeartbeater.class); { UnixSocketHeartbeater hb = (UnixSocketHeartbeater) failureDetector.getHeartbeater(); - assertThat(hb.getPath(), is(expectedPath)); + assertThat(hb.getPath()).isEqualTo(expectedPath); } - assertThat(failureDetector.getHeartbeater().getIntervalMillis(), is(1000)); + assertThat(failureDetector.getHeartbeater().getIntervalMillis()).isEqualTo(1000); } else { - assertThat(failureDetector, is(nullValue())); + assertThat(failureDetector).isNull(); } } private void assertDefaultFluentdSender(FluentdSender sender, Path expectedPath) { - assertThat(sender, instanceOf(RetryableSender.class)); + assertThat(sender).isInstanceOf(RetryableSender.class); RetryableSender retryableSender = (RetryableSender) sender; assertDefaultRetryableSender(retryableSender, UnixSocketSender.class); assertUnixSocketSender((UnixSocketSender) retryableSender.getBaseSender(), expectedPath, false); diff --git a/fluency-fluentd-ext/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/UnixSocketSenderTest.java b/fluency-fluentd-ext/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/UnixSocketSenderTest.java index f53d7f6d..4c5fa74c 100644 --- a/fluency-fluentd-ext/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/UnixSocketSenderTest.java +++ b/fluency-fluentd-ext/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/UnixSocketSenderTest.java @@ -16,7 +16,6 @@ package org.komamitsu.fluency.fluentd.ingester.sender; -import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; import org.komamitsu.fluency.fluentd.MockUnixSocketServer; import org.komamitsu.fluency.fluentd.ingester.sender.failuredetect.FailureDetector; @@ -35,10 +34,9 @@ import java.util.Arrays; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.is; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; class UnixSocketSenderTest @@ -51,10 +49,12 @@ void testSend() throws Exception { testSendBase(socketPath -> { - UnixSocketSender.Config senderConfig = new UnixSocketSender.Config(); - senderConfig.setPath(socketPath); - return new UnixSocketSender(senderConfig); - }, is(1), is(1)); + UnixSocketSender.Config senderConfig = new UnixSocketSender.Config(); + senderConfig.setPath(socketPath); + return new UnixSocketSender(senderConfig); + }, + count -> assertThat(count).isEqualTo(1), + count -> assertThat(count).isEqualTo(1)); } @Test @@ -62,24 +62,26 @@ void testSendWithHeartbeart() throws Exception { testSendBase(socketPath -> { - UnixSocketHeartbeater.Config hbConfig = new UnixSocketHeartbeater.Config(); - hbConfig.setPath(socketPath); - hbConfig.setIntervalMillis(400); + UnixSocketHeartbeater.Config hbConfig = new UnixSocketHeartbeater.Config(); + hbConfig.setPath(socketPath); + hbConfig.setIntervalMillis(400); - UnixSocketSender.Config senderConfig = new UnixSocketSender.Config(); - senderConfig.setPath(socketPath); + UnixSocketSender.Config senderConfig = new UnixSocketSender.Config(); + senderConfig.setPath(socketPath); - return new UnixSocketSender(senderConfig, + return new UnixSocketSender(senderConfig, new FailureDetector( - new PhiAccrualFailureDetectStrategy(), - new UnixSocketHeartbeater(hbConfig))); - }, greaterThan(1), greaterThan(1)); + new PhiAccrualFailureDetectStrategy(), + new UnixSocketHeartbeater(hbConfig))); + }, + count -> assertThat(count).isGreaterThan(1), + count -> assertThat(count).isGreaterThan(1)); } private void testSendBase( SenderCreator senderCreator, - Matcher connectCountMatcher, - Matcher closeCountMatcher) + Consumer connectCountAssertion, + Consumer closeCountAssertion) throws Exception { try (MockUnixSocketServer server = new MockUnixSocketServer()) { @@ -135,9 +137,9 @@ private void testSendBase( } LOG.debug("recvCount={}", recvCount); - assertThat(connectCount, connectCountMatcher); - assertThat(recvLen, is((long) concurrency * reqNum * 10)); - assertThat(closeCount, closeCountMatcher); + connectCountAssertion.accept(connectCount); + assertThat(recvLen).isEqualTo((long) concurrency * reqNum * 10); + closeCountAssertion.accept(closeCount); } } diff --git a/fluency-fluentd/src/test/java/org/komamitsu/fluency/buffer/BufferForFluentdTest.java b/fluency-fluentd/src/test/java/org/komamitsu/fluency/buffer/BufferForFluentdTest.java index 59b4bcb9..04b634e6 100644 --- a/fluency-fluentd/src/test/java/org/komamitsu/fluency/buffer/BufferForFluentdTest.java +++ b/fluency-fluentd/src/test/java/org/komamitsu/fluency/buffer/BufferForFluentdTest.java @@ -18,7 +18,6 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; -import org.hamcrest.CoreMatchers; import org.junit.jupiter.api.Test; import org.komamitsu.fluency.EventTime; import org.komamitsu.fluency.fluentd.ingester.FluentdIngester; @@ -44,9 +43,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -108,9 +105,9 @@ static class BufferTestHelper void baseTestMessageBuffer(final int loopCount, final boolean multiTags, final boolean syncFlush, final boolean eventTime, final Buffer buffer) throws IOException, InterruptedException { - assertThat(buffer.getBufferUsage(), is(0f)); - assertThat(buffer.getAllocatedSize(), is(0L)); - assertThat(buffer.getBufferedDataSize(), is(0L)); + assertThat(buffer.getBufferUsage()).isEqualTo(0f); + assertThat(buffer.getAllocatedSize()).isEqualTo(0L); + assertThat(buffer.getBufferedDataSize()).isEqualTo(0L); final int concurrency = 4; final CountDownLatch latch = new CountDownLatch(concurrency); @@ -167,9 +164,9 @@ void baseTestMessageBuffer(final int loopCount, final boolean multiTags, final b executorService.execute(emitTask); } assertTrue(latch.await(10, TimeUnit.SECONDS)); - assertThat(buffer.getBufferUsage(), is(greaterThan(0f))); - assertThat(buffer.getAllocatedSize(), is(greaterThan(0L))); - assertThat(buffer.getBufferedDataSize(), is(greaterThan(0L))); + assertThat(buffer.getBufferUsage()).isGreaterThan(0f); + assertThat(buffer.getAllocatedSize()).isGreaterThan(0L); + assertThat(buffer.getBufferedDataSize()).isGreaterThan(0L); buffer.flush(ingester, true); buffer.close(); @@ -186,9 +183,9 @@ void baseTestMessageBuffer(final int loopCount, final boolean multiTags, final b flushService.shutdownNow(); } buffer.close(); // Just in case - assertThat(buffer.getBufferUsage(), is(0f)); - assertThat(buffer.getAllocatedSize(), is(0L)); - assertThat(buffer.getBufferedDataSize(), is(0L)); + assertThat(buffer.getBufferUsage()).isEqualTo(0f); + assertThat(buffer.getAllocatedSize()).isEqualTo(0L); + assertThat(buffer.getBufferedDataSize()).isEqualTo(0L); int totalLoopCount = concurrency * loopCount; @@ -260,17 +257,17 @@ private void analyzeResult(String tag, ImmutableValue timestamp, Map expectedBaseClass) { - assertThat(sender.getRetryStrategy(), instanceOf(ExponentialBackOffRetryStrategy.class)); + assertThat(sender.getRetryStrategy()).isInstanceOf(ExponentialBackOffRetryStrategy.class); ExponentialBackOffRetryStrategy retryStrategy = (ExponentialBackOffRetryStrategy) sender.getRetryStrategy(); - assertThat(retryStrategy.getMaxRetryCount(), is(7)); - assertThat(retryStrategy.getBaseIntervalMillis(), is(400)); - assertThat(sender.getBaseSender(), instanceOf(expectedBaseClass)); + assertThat(retryStrategy.getMaxRetryCount()).isEqualTo(7); + assertThat(retryStrategy.getBaseIntervalMillis()).isEqualTo(400); + assertThat(sender.getBaseSender()).isInstanceOf(expectedBaseClass); } private void assertDefaultFluentdSender(FluentdSender sender, String expectedHost, int expectedPort, Class expectedBaseClass) { - assertThat(sender, instanceOf(RetryableSender.class)); + assertThat(sender).isInstanceOf(RetryableSender.class); RetryableSender retryableSender = (RetryableSender) sender; assertDefaultRetryableSender(retryableSender, expectedBaseClass); - assertThat(retryableSender.getBaseSender(), instanceOf(InetSocketSender.class)); + assertThat(retryableSender.getBaseSender()).isInstanceOf(InetSocketSender.class); InetSocketSender networkSender = (InetSocketSender) retryableSender.getBaseSender(); - assertThat(networkSender.getHost(), is(expectedHost)); - assertThat(networkSender.getPort(), is(expectedPort)); - assertThat(networkSender.getConnectionTimeoutMilli(), is(5000)); - assertThat(networkSender.getReadTimeoutMilli(), is(5000)); + assertThat(networkSender.getHost()).isEqualTo(expectedHost); + assertThat(networkSender.getPort()).isEqualTo(expectedPort); + assertThat(networkSender.getConnectionTimeoutMilli()).isEqualTo(5000); + assertThat(networkSender.getReadTimeoutMilli()).isEqualTo(5000); FailureDetector failureDetector = networkSender.getFailureDetector(); - assertThat(failureDetector, is(nullValue())); + assertThat(failureDetector).isNull(); } @Test @@ -211,7 +210,7 @@ void buildWithComplexConfig() throws IOException { String tmpdir = System.getProperty("java.io.tmpdir"); - assertThat(tmpdir, is(notNullValue())); + assertThat(tmpdir).isNotNull(); FluencyBuilderForFluentd builder = new FluencyBuilderForFluentd(); builder.setFlushAttemptIntervalMillis(200); @@ -235,74 +234,74 @@ void buildWithComplexConfig() new InetSocketAddress("333.333.333.333", 11111), new InetSocketAddress("444.444.444.444", 22222)))) { - assertThat(fluency.getBuffer(), instanceOf(Buffer.class)); + assertThat(fluency.getBuffer()).isInstanceOf(Buffer.class); Buffer buffer = fluency.getBuffer(); - assertThat(buffer.getMaxBufferSize(), is(Long.MAX_VALUE)); - assertThat(buffer.getFileBackupDir(), is(tmpdir)); - assertThat(buffer.bufferFormatType(), is("packed_forward")); - assertThat(buffer.getChunkRetentionTimeMillis(), is(19 * 1000)); - assertThat(buffer.getChunkExpandRatio(), is(2f)); - assertThat(buffer.getChunkInitialSize(), is(7 * 1024 * 1024)); - assertThat(buffer.getChunkRetentionSize(), is(13 * 1024 * 1024)); - assertThat(buffer.getJvmHeapBufferMode(), is(true)); + assertThat(buffer.getMaxBufferSize()).isEqualTo(Long.MAX_VALUE); + assertThat(buffer.getFileBackupDir()).isEqualTo(tmpdir); + assertThat(buffer.bufferFormatType()).isEqualTo("packed_forward"); + assertThat(buffer.getChunkRetentionTimeMillis()).isEqualTo(19 * 1000); + assertThat(buffer.getChunkExpandRatio()).isEqualTo(2f); + assertThat(buffer.getChunkInitialSize()).isEqualTo(7 * 1024 * 1024); + assertThat(buffer.getChunkRetentionSize()).isEqualTo(13 * 1024 * 1024); + assertThat(buffer.getJvmHeapBufferMode()).isEqualTo(true); Flusher flusher = fluency.getFlusher(); - assertThat(flusher.isTerminated(), is(false)); - assertThat(flusher.getFlushAttemptIntervalMillis(), is(200)); - assertThat(flusher.getWaitUntilBufferFlushed(), is(42)); - assertThat(flusher.getWaitUntilTerminated(), is(24)); + assertThat(flusher.isTerminated()).isFalse(); + assertThat(flusher.getFlushAttemptIntervalMillis()).isEqualTo(200); + assertThat(flusher.getWaitUntilBufferFlushed()).isEqualTo(42); + assertThat(flusher.getWaitUntilTerminated()).isEqualTo(24); - assertThat(flusher.getIngester().getSender(), instanceOf(RetryableSender.class)); + assertThat(flusher.getIngester().getSender()).isInstanceOf(RetryableSender.class); RetryableSender retryableSender = (RetryableSender) flusher.getIngester().getSender(); - assertThat(retryableSender.getRetryStrategy(), instanceOf(ExponentialBackOffRetryStrategy.class)); + assertThat(retryableSender.getRetryStrategy()).isInstanceOf(ExponentialBackOffRetryStrategy.class); ExponentialBackOffRetryStrategy retryStrategy = (ExponentialBackOffRetryStrategy) retryableSender.getRetryStrategy(); - assertThat(retryStrategy.getMaxRetryCount(), is(99)); - assertThat(retryStrategy.getBaseIntervalMillis(), is(20)); - assertThat(retryStrategy.getMaxIntervalMillis(), is(100000)); + assertThat(retryStrategy.getMaxRetryCount()).isEqualTo(99); + assertThat(retryStrategy.getBaseIntervalMillis()).isEqualTo(20); + assertThat(retryStrategy.getMaxIntervalMillis()).isEqualTo(100000); - assertThat(retryableSender.getBaseSender(), instanceOf(MultiSender.class)); + assertThat(retryableSender.getBaseSender()).isInstanceOf(MultiSender.class); MultiSender multiSender = (MultiSender) retryableSender.getBaseSender(); - assertThat(multiSender.getSenders().size(), is(2)); + assertThat(multiSender.getSenders().size()).isEqualTo(2); - assertThat(multiSender.getSenders().get(0), instanceOf(TCPSender.class)); + assertThat(multiSender.getSenders().get(0)).isInstanceOf(TCPSender.class); { TCPSender sender = (TCPSender) multiSender.getSenders().get(0); - assertThat(sender.getHost(), is("333.333.333.333")); - assertThat(sender.getPort(), is(11111)); - assertThat(sender.getConnectionTimeoutMilli(), is(12345)); - assertThat(sender.getReadTimeoutMilli(), is(9876)); + assertThat(sender.getHost()).isEqualTo("333.333.333.333"); + assertThat(sender.getPort()).isEqualTo(11111); + assertThat(sender.getConnectionTimeoutMilli()).isEqualTo(12345); + assertThat(sender.getReadTimeoutMilli()).isEqualTo(9876); FailureDetector failureDetector = sender.getFailureDetector(); - assertThat(failureDetector.getFailureIntervalMillis(), is(3 * 1000)); - assertThat(failureDetector.getFailureDetectStrategy(), instanceOf(PhiAccrualFailureDetectStrategy.class)); - assertThat(failureDetector.getHeartbeater(), instanceOf(TCPHeartbeater.class)); + assertThat(failureDetector.getFailureIntervalMillis()).isEqualTo(3 * 1000); + assertThat(failureDetector.getFailureDetectStrategy()).isInstanceOf(PhiAccrualFailureDetectStrategy.class); + assertThat(failureDetector.getHeartbeater()).isInstanceOf(TCPHeartbeater.class); { TCPHeartbeater hb = (TCPHeartbeater) failureDetector.getHeartbeater(); - assertThat(hb.getHost(), is("333.333.333.333")); - assertThat(hb.getPort(), is(11111)); + assertThat(hb.getHost()).isEqualTo("333.333.333.333"); + assertThat(hb.getPort()).isEqualTo(11111); } - assertThat(failureDetector.getHeartbeater().getIntervalMillis(), is(1000)); + assertThat(failureDetector.getHeartbeater().getIntervalMillis()).isEqualTo(1000); } - assertThat(multiSender.getSenders().get(1), instanceOf(TCPSender.class)); + assertThat(multiSender.getSenders().get(1)).isInstanceOf(TCPSender.class); { TCPSender sender = (TCPSender) multiSender.getSenders().get(1); - assertThat(sender.getHost(), is("444.444.444.444")); - assertThat(sender.getPort(), is(22222)); - assertThat(sender.getConnectionTimeoutMilli(), is(12345)); - assertThat(sender.getReadTimeoutMilli(), is(9876)); + assertThat(sender.getHost()).isEqualTo("444.444.444.444"); + assertThat(sender.getPort()).isEqualTo(22222); + assertThat(sender.getConnectionTimeoutMilli()).isEqualTo(12345); + assertThat(sender.getReadTimeoutMilli()).isEqualTo(9876); FailureDetector failureDetector = sender.getFailureDetector(); - assertThat(failureDetector.getFailureIntervalMillis(), is(3 * 1000)); - assertThat(failureDetector.getFailureDetectStrategy(), instanceOf(PhiAccrualFailureDetectStrategy.class)); - assertThat(failureDetector.getHeartbeater(), instanceOf(TCPHeartbeater.class)); + assertThat(failureDetector.getFailureIntervalMillis()).isEqualTo(3 * 1000); + assertThat(failureDetector.getFailureDetectStrategy()).isInstanceOf(PhiAccrualFailureDetectStrategy.class); + assertThat(failureDetector.getHeartbeater()).isInstanceOf(TCPHeartbeater.class); { TCPHeartbeater hb = (TCPHeartbeater) failureDetector.getHeartbeater(); - assertThat(hb.getHost(), is("444.444.444.444")); - assertThat(hb.getPort(), is(22222)); + assertThat(hb.getHost()).isEqualTo("444.444.444.444"); + assertThat(hb.getPort()).isEqualTo(22222); } - assertThat(failureDetector.getHeartbeater().getIntervalMillis(), is(1000)); + assertThat(failureDetector.getHeartbeater().getIntervalMillis()).isEqualTo(1000); } } } @@ -312,7 +311,7 @@ void buildWithSslAndComplexConfig() throws IOException { String tmpdir = System.getProperty("java.io.tmpdir"); - assertThat(tmpdir, is(notNullValue())); + assertThat(tmpdir).isNotNull(); FluencyBuilderForFluentd builder = new FluencyBuilderForFluentd(); builder.setSslEnabled(true); @@ -336,56 +335,56 @@ void buildWithSslAndComplexConfig() new InetSocketAddress("333.333.333.333", 11111), new InetSocketAddress("444.444.444.444", 22222)))) { - assertThat(fluency.getFlusher().getIngester().getSender(), instanceOf(RetryableSender.class)); + assertThat(fluency.getFlusher().getIngester().getSender()).isInstanceOf(RetryableSender.class); RetryableSender retryableSender = (RetryableSender) fluency.getFlusher().getIngester().getSender(); - assertThat(retryableSender.getRetryStrategy(), instanceOf(ExponentialBackOffRetryStrategy.class)); + assertThat(retryableSender.getRetryStrategy()).isInstanceOf(ExponentialBackOffRetryStrategy.class); ExponentialBackOffRetryStrategy retryStrategy = (ExponentialBackOffRetryStrategy) retryableSender.getRetryStrategy(); - assertThat(retryStrategy.getMaxRetryCount(), is(99)); - assertThat(retryStrategy.getBaseIntervalMillis(), is(20)); - assertThat(retryStrategy.getMaxIntervalMillis(), is(100000)); + assertThat(retryStrategy.getMaxRetryCount()).isEqualTo(99); + assertThat(retryStrategy.getBaseIntervalMillis()).isEqualTo(20); + assertThat(retryStrategy.getMaxIntervalMillis()).isEqualTo(100000); - assertThat(retryableSender.getBaseSender(), instanceOf(MultiSender.class)); + assertThat(retryableSender.getBaseSender()).isInstanceOf(MultiSender.class); MultiSender multiSender = (MultiSender) retryableSender.getBaseSender(); - assertThat(multiSender.getSenders().size(), is(2)); + assertThat(multiSender.getSenders().size()).isEqualTo(2); - assertThat(multiSender.getSenders().get(0), instanceOf(SSLSender.class)); + assertThat(multiSender.getSenders().get(0)).isInstanceOf(SSLSender.class); { SSLSender sender = (SSLSender) multiSender.getSenders().get(0); - assertThat(sender.getHost(), is("333.333.333.333")); - assertThat(sender.getPort(), is(11111)); - assertThat(sender.getConnectionTimeoutMilli(), is(12345)); - assertThat(sender.getReadTimeoutMilli(), is(9876)); + assertThat(sender.getHost()).isEqualTo("333.333.333.333"); + assertThat(sender.getPort()).isEqualTo(11111); + assertThat(sender.getConnectionTimeoutMilli()).isEqualTo(12345); + assertThat(sender.getReadTimeoutMilli()).isEqualTo(9876); FailureDetector failureDetector = sender.getFailureDetector(); - assertThat(failureDetector.getFailureIntervalMillis(), is(3 * 1000)); - assertThat(failureDetector.getFailureDetectStrategy(), instanceOf(PhiAccrualFailureDetectStrategy.class)); - assertThat(failureDetector.getHeartbeater(), instanceOf(SSLHeartbeater.class)); + assertThat(failureDetector.getFailureIntervalMillis()).isEqualTo(3 * 1000); + assertThat(failureDetector.getFailureDetectStrategy()).isInstanceOf(PhiAccrualFailureDetectStrategy.class); + assertThat(failureDetector.getHeartbeater()).isInstanceOf(SSLHeartbeater.class); { SSLHeartbeater hb = (SSLHeartbeater) failureDetector.getHeartbeater(); - assertThat(hb.getHost(), is("333.333.333.333")); - assertThat(hb.getPort(), is(11111)); + assertThat(hb.getHost()).isEqualTo("333.333.333.333"); + assertThat(hb.getPort()).isEqualTo(11111); } - assertThat(failureDetector.getHeartbeater().getIntervalMillis(), is(1000)); + assertThat(failureDetector.getHeartbeater().getIntervalMillis()).isEqualTo(1000); } - assertThat(multiSender.getSenders().get(1), instanceOf(SSLSender.class)); + assertThat(multiSender.getSenders().get(1)).isInstanceOf(SSLSender.class); { SSLSender sender = (SSLSender) multiSender.getSenders().get(1); - assertThat(sender.getHost(), is("444.444.444.444")); - assertThat(sender.getPort(), is(22222)); - assertThat(sender.getConnectionTimeoutMilli(), is(12345)); - assertThat(sender.getReadTimeoutMilli(), is(9876)); + assertThat(sender.getHost()).isEqualTo("444.444.444.444"); + assertThat(sender.getPort()).isEqualTo(22222); + assertThat(sender.getConnectionTimeoutMilli()).isEqualTo(12345); + assertThat(sender.getReadTimeoutMilli()).isEqualTo(9876); FailureDetector failureDetector = sender.getFailureDetector(); - assertThat(failureDetector.getFailureIntervalMillis(), is(3 * 1000)); - assertThat(failureDetector.getFailureDetectStrategy(), instanceOf(PhiAccrualFailureDetectStrategy.class)); - assertThat(failureDetector.getHeartbeater(), instanceOf(SSLHeartbeater.class)); + assertThat(failureDetector.getFailureIntervalMillis()).isEqualTo(3 * 1000); + assertThat(failureDetector.getFailureDetectStrategy()).isInstanceOf(PhiAccrualFailureDetectStrategy.class); + assertThat(failureDetector.getHeartbeater()).isInstanceOf(SSLHeartbeater.class); { SSLHeartbeater hb = (SSLHeartbeater) failureDetector.getHeartbeater(); - assertThat(hb.getHost(), is("444.444.444.444")); - assertThat(hb.getPort(), is(22222)); + assertThat(hb.getHost()).isEqualTo("444.444.444.444"); + assertThat(hb.getPort()).isEqualTo(22222); } - assertThat(failureDetector.getHeartbeater().getIntervalMillis(), is(1000)); + assertThat(failureDetector.getHeartbeater().getIntervalMillis()).isEqualTo(1000); } } } @@ -394,7 +393,7 @@ void buildWithSslAndComplexConfig() void defaultRecordFormatter() { FluencyBuilderForFluentd builder = new FluencyBuilderForFluentd(); - assertThat(builder.getRecordFormatter(), instanceOf(FluentdRecordFormatter.class)); + assertThat(builder.getRecordFormatter()).isInstanceOf(FluentdRecordFormatter.class); } @Test @@ -402,7 +401,7 @@ void customRecordFormatter() { FluencyBuilderForFluentd builder = new FluencyBuilderForFluentd(); builder.setRecordFormatter(new CustomFluentdRecordFormatter()); - assertThat(builder.getRecordFormatter(), instanceOf(CustomFluentdRecordFormatter.class)); + assertThat(builder.getRecordFormatter()).isInstanceOf(CustomFluentdRecordFormatter.class); } private static class CustomFluentdRecordFormatter extends FluentdRecordFormatter diff --git a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/FluencyTest.java b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/FluencyTest.java index 5768a1c2..ec799abb 100644 --- a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/FluencyTest.java +++ b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/FluencyTest.java @@ -51,9 +51,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Stream; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -96,7 +94,7 @@ void testSenderErrorHandler() throw new AssertionError("Timeout"); } - assertThat(errorContainer.get(), is(instanceOf(RetryableSender.RetryOverException.class))); + assertThat(errorContainer.get()).isInstanceOf(RetryableSender.RetryOverException.class); } } @@ -267,7 +265,7 @@ void testBufferWithJacksonModule() event.put("foo", foo); fluency.emit("tag", event); - assertThat(serialized.get(), is(true)); + assertThat(serialized.get()).isEqualTo(true); } static class Foo diff --git a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/FluencyTestWithMockServer.java b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/FluencyTestWithMockServer.java index 8c11e0ef..59a1bae2 100644 --- a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/FluencyTestWithMockServer.java +++ b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/FluencyTestWithMockServer.java @@ -55,10 +55,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Stream; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -474,21 +471,21 @@ else if (options.fileBackup) { secondaryFluentd.stop(); if (options.failover) { - assertThat(fluentd.connectCounter.get(), is(greaterThan(0L))); - assertThat(fluentd.connectCounter.get(), is(lessThanOrEqualTo(10L))); - assertThat(fluentd.closeCounter.get(), is(greaterThan(0L))); - assertThat(fluentd.closeCounter.get(), is(lessThanOrEqualTo(10L))); + assertThat(fluentd.connectCounter.get()).isGreaterThan(0L); + assertThat(fluentd.connectCounter.get()).isLessThanOrEqualTo(10L); + assertThat(fluentd.closeCounter.get()).isGreaterThan(0L); + assertThat(fluentd.closeCounter.get()).isLessThanOrEqualTo(10L); } else { - assertThat(fluentd.connectCounter.get(), is(greaterThan(0L))); - assertThat(fluentd.connectCounter.get(), is(lessThanOrEqualTo(2L))); + assertThat(fluentd.connectCounter.get()).isGreaterThan(0L); + assertThat(fluentd.connectCounter.get()).isLessThanOrEqualTo(2L); if (options.closeInsteadOfFlush) { - assertThat(fluentd.closeCounter.get(), is(greaterThan(0L))); + assertThat(fluentd.closeCounter.get()).isGreaterThan(0L); } else { - assertThat(fluentd.closeCounter.get(), is(0L)); + assertThat(fluentd.closeCounter.get()).isEqualTo(0L); } - assertThat(fluentd.closeCounter.get(), is(lessThanOrEqualTo(2L))); + assertThat(fluentd.closeCounter.get()).isLessThanOrEqualTo(2L); } assertEquals((long) concurrency * reqNum, fluentd.ageEventsCounter.get()); diff --git a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/ConfigTest.java b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/ConfigTest.java index 38b99194..3f152e86 100644 --- a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/ConfigTest.java +++ b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/ConfigTest.java @@ -23,8 +23,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.fail; class ConfigTest @@ -38,14 +37,14 @@ void errorHandler() config.setErrorHandler(e -> errorOccurred.set(true)); new DummySender(config, false).send(ByteBuffer.allocate(8)); - assertThat(errorOccurred.get(), is(false)); + assertThat(errorOccurred.get()).isEqualTo(false); try { new DummySender(config, true).send(ByteBuffer.allocate(8)); fail(); } catch (Exception e) { - assertThat(errorOccurred.get(), is(true)); + assertThat(errorOccurred.get()).isEqualTo(true); } } diff --git a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/MultiSenderTest.java b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/MultiSenderTest.java index 9c9e51c3..3f5169c4 100644 --- a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/MultiSenderTest.java +++ b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/MultiSenderTest.java @@ -36,11 +36,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; import static org.komamitsu.fluency.fluentd.SSLTestSocketFactories.SSL_CLIENT_SOCKET_FACTORY; @@ -80,7 +76,7 @@ void testConstructorForTCPSender() new TCPSender(senderConfig1, createFailureDetector(new TCPHeartbeater(hbConfig1))))); - assertThat(multiSender.toString().length(), greaterThan(0)); + assertThat(multiSender.toString().length()).isGreaterThan(0); assertEquals(2, multiSender.getSenders().size()); @@ -141,7 +137,7 @@ void testConstructorForSSLSender() new SSLSender(senderConfig1, createFailureDetector(new SSLHeartbeater(hbConfig1))))); - assertThat(multiSender.toString().length(), greaterThan(0)); + assertThat(multiSender.toString().length()).isGreaterThan(0); assertEquals(2, multiSender.getSenders().size()); @@ -314,8 +310,8 @@ public void run() // But w/o ack responses, Sender can't detect dropped requests. So some margin for expected result is allowed here. long minExpectedRecvLen = ((long) (concurency * (sslEnabled ? 0.5 : 0.8)) * reqNum) * 10; long maxExpectedRecvLen = ((long) concurency * reqNum) * 10; - assertThat(recvLen, is(greaterThanOrEqualTo(minExpectedRecvLen))); - assertThat(recvLen, is(lessThanOrEqualTo(maxExpectedRecvLen))); + assertThat(recvLen).isGreaterThanOrEqualTo(minExpectedRecvLen); + assertThat(recvLen).isLessThanOrEqualTo(maxExpectedRecvLen); assertEquals(1, closeCount); } } diff --git a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/RetryableSenderTest.java b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/RetryableSenderTest.java index 85dc9958..f1307f21 100644 --- a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/RetryableSenderTest.java +++ b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/RetryableSenderTest.java @@ -23,8 +23,7 @@ import java.nio.ByteBuffer; import java.util.List; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; class RetryableSenderTest @@ -42,9 +41,9 @@ void testSend() new FailurableSender(3), new ExponentialBackOffRetryStrategy(retryStrategyConfig)); FailurableSender baseSender = (FailurableSender) sender.getBaseSender(); - assertThat(baseSender.getRetry(), is(0)); + assertThat(baseSender.getRetry()).isEqualTo(0); sender.send(ByteBuffer.allocate(64)); - assertThat(baseSender.getRetry(), is(3)); + assertThat(baseSender.getRetry()).isEqualTo(3); } @Test @@ -60,7 +59,7 @@ void testSendRetryOver() new FailurableSender(3), new ExponentialBackOffRetryStrategy(retryStrategyConfig)); FailurableSender baseSender = (FailurableSender) sender.getBaseSender(); - assertThat(baseSender.getRetry(), is(0)); + assertThat(baseSender.getRetry()).isEqualTo(0); assertThrows(RetryableSender.RetryOverException.class, () -> sender.send(ByteBuffer.allocate(64))); } diff --git a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/SSLSenderTest.java b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/SSLSenderTest.java index d15264d6..c6191667 100644 --- a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/SSLSenderTest.java +++ b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/SSLSenderTest.java @@ -16,7 +16,6 @@ package org.komamitsu.fluency.fluentd.ingester.sender; -import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; import org.komamitsu.fluency.fluentd.MockTCPServer; import org.komamitsu.fluency.fluentd.MockTCPServerWithMetrics; @@ -34,13 +33,11 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.Locale; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.is; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -56,11 +53,13 @@ void testSend() throws Exception { testSendBase(port -> { - SSLSender.Config config = new SSLSender.Config(); - config.setPort(port); - config.setSslSocketFactory(SSL_CLIENT_SOCKET_FACTORY); - return new SSLSender(config); - }, is(1), is(1)); + SSLSender.Config config = new SSLSender.Config(); + config.setPort(port); + config.setSslSocketFactory(SSL_CLIENT_SOCKET_FACTORY); + return new SSLSender(config); + }, + count -> assertThat(count).isEqualTo(1), + count -> assertThat(count).isEqualTo(1)); } @Test @@ -68,23 +67,25 @@ void testSendWithHeartbeart() throws Exception { testSendBase(port -> { - SSLHeartbeater.Config hbConfig = new SSLHeartbeater.Config(); - hbConfig.setPort(port); - hbConfig.setIntervalMillis(400); - SSLSender.Config senderConfig = new SSLSender.Config(); - senderConfig.setPort(port); - senderConfig.setSslSocketFactory(SSL_CLIENT_SOCKET_FACTORY); - return new SSLSender(senderConfig, + SSLHeartbeater.Config hbConfig = new SSLHeartbeater.Config(); + hbConfig.setPort(port); + hbConfig.setIntervalMillis(400); + SSLSender.Config senderConfig = new SSLSender.Config(); + senderConfig.setPort(port); + senderConfig.setSslSocketFactory(SSL_CLIENT_SOCKET_FACTORY); + return new SSLSender(senderConfig, new FailureDetector( - new PhiAccrualFailureDetectStrategy(), - new SSLHeartbeater(hbConfig))); - }, greaterThan(1), greaterThan(1)); + new PhiAccrualFailureDetectStrategy(), + new SSLHeartbeater(hbConfig))); + }, + count -> assertThat(count).isGreaterThan(1), + count -> assertThat(count).isGreaterThan(1)); } private void testSendBase( SSLSenderCreator sslSenderCreator, - Matcher connectCountMatcher, - Matcher closeCountMatcher) + Consumer connectCountAssertion, + Consumer closeCountAssertion) throws Exception { MockTCPServerWithMetrics server = new MockTCPServerWithMetrics(true); @@ -143,9 +144,9 @@ private void testSendBase( } LOG.debug("recvCount={}", recvCount); - assertThat(connectCount, connectCountMatcher); - assertThat(recvLen, is((long) concurency * reqNum * 10)); - assertThat(closeCount, closeCountMatcher); + connectCountAssertion.accept(connectCount); + assertThat(recvLen).isEqualTo((long) concurency * reqNum * 10); + closeCountAssertion.accept(closeCount); } @Test diff --git a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/TCPSenderTest.java b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/TCPSenderTest.java index b565aeba..cd6bfa92 100644 --- a/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/TCPSenderTest.java +++ b/fluency-fluentd/src/test/java/org/komamitsu/fluency/fluentd/ingester/sender/TCPSenderTest.java @@ -16,7 +16,6 @@ package org.komamitsu.fluency.fluentd.ingester.sender; -import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; import org.komamitsu.fluency.fluentd.MockTCPServer; import org.komamitsu.fluency.fluentd.MockTCPServerWithMetrics; @@ -40,14 +39,12 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.is; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.komamitsu.fluency.fluentd.SSLTestSocketFactories.SSL_CLIENT_SOCKET_FACTORY; class TCPSenderTest { @@ -58,10 +55,12 @@ void testSend() throws Exception { testSendBase(port -> { - TCPSender.Config senderConfig = new TCPSender.Config(); - senderConfig.setPort(port); - return new TCPSender(senderConfig); - }, is(1), is(1)); + TCPSender.Config senderConfig = new TCPSender.Config(); + senderConfig.setPort(port); + return new TCPSender(senderConfig); + }, + count -> assertThat(count).isEqualTo(1), + count -> assertThat(count).isEqualTo(1)); } @Test @@ -69,24 +68,26 @@ void testSendWithHeartbeart() throws Exception { testSendBase(port -> { - TCPHeartbeater.Config hbConfig = new TCPHeartbeater.Config(); - hbConfig.setPort(port); - hbConfig.setIntervalMillis(400); + TCPHeartbeater.Config hbConfig = new TCPHeartbeater.Config(); + hbConfig.setPort(port); + hbConfig.setIntervalMillis(400); - TCPSender.Config senderConfig = new TCPSender.Config(); - senderConfig.setPort(port); + TCPSender.Config senderConfig = new TCPSender.Config(); + senderConfig.setPort(port); - return new TCPSender(senderConfig, + return new TCPSender(senderConfig, new FailureDetector( - new PhiAccrualFailureDetectStrategy(), - new TCPHeartbeater(hbConfig))); - }, greaterThan(1), greaterThan(1)); + new PhiAccrualFailureDetectStrategy(), + new TCPHeartbeater(hbConfig))); + }, + count -> assertThat(count).isGreaterThan(1), + count -> assertThat(count).isGreaterThan(1)); } private void testSendBase( TCPSenderCreater senderCreater, - Matcher connectCountMatcher, - Matcher closeCountMatcher) + Consumer connectCountAssertion, + Consumer closeCountAssertion) throws Exception { MockTCPServerWithMetrics server = new MockTCPServerWithMetrics(false); @@ -143,9 +144,9 @@ private void testSendBase( } LOG.debug("recvCount={}", recvCount); - assertThat(connectCount, connectCountMatcher); - assertThat(recvLen, is((long) concurency * reqNum * 10)); - assertThat(closeCount, closeCountMatcher); + connectCountAssertion.accept(connectCount); + assertThat(recvLen).isEqualTo((long) concurency * reqNum * 10); + closeCountAssertion.accept(closeCount); } @Test