LogSinkAppendTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/services/logging/LogSinkAppendTest.kt
1package eu.friedcutlet.yokan.services.logging23import eu.friedcutlet.yokan.services.files.JvmLocalFileService4import eu.friedcutlet.yokan.services.files.LocalFileService5import java.nio.file.Files6import kotlin.test.Test7import kotlin.test.assertEquals8import kotlin.test.assertTrue910class LogSinkAppendTest {11 @Test12 fun appendsWithoutReadingHistoryAndRotatesAtTheCap() {13 val root = Files.createTempDirectory("yokan-log-test-").toFile()14 try {15 val disk = JvmLocalFileService(root.absolutePath, root.absolutePath, root.absolutePath)16 val log = root.resolve("logs/yokan.log")17 log.parentFile.mkdirs()18 log.writeText("existing\n")19 val files = object : LocalFileService by disk {20 override fun readBytes(path: String): ByteArray = error("Log append must not read history")21 override fun writeBytes(path: String, bytes: ByteArray) = error("Log append must not replace history")22 }23 val lines = (0..99).map { "line $it\n".encodeToByteArray() }24 // Unknown size first, then the tracked size, exactly as LogSink does.25 var size = -1L26 lines.forEach { size = appendRotatingLog(files, log.absolutePath, size, it, maxBytes = 200) }2728 val rotated = root.resolve("logs/yokan.log.1")29 assertTrue(log.length() <= 200 && rotated.length() <= 200, "each file stays under the cap")30 assertEquals(log.length(), size)31 // Nothing between the rotated file and the live file is lost or reordered.32 val tail = (rotated.readLines() + log.readLines())33 assertEquals((100 - tail.size until 100).map { "line $it" }, tail)34 } finally {35 root.deleteRecursively()36 }37 }3839 @Test40 fun skipsAnEntryLargerThanTheFileCap() {41 val root = Files.createTempDirectory("yokan-log-oversized-test-").toFile()42 try {43 val disk = JvmLocalFileService(root.absolutePath, root.absolutePath, root.absolutePath)44 val log = root.resolve("logs/yokan.log")45 log.parentFile.mkdirs()46 log.writeBytes(ByteArray(190))4748 val size = appendRotatingLog(disk, log.absolutePath, 190L, ByteArray(201), maxBytes = 200)4950 assertEquals(190L, size)51 assertEquals(190L, log.length())52 assertTrue(!root.resolve("logs/yokan.log.1").exists())53 } finally {54 root.deleteRecursively()55 }56 }5758 @Test59 fun doesNotAppendWhenRotationPromotionFails() {60 val root = Files.createTempDirectory("yokan-log-promotion-test-").toFile()61 try {62 val disk = JvmLocalFileService(root.absolutePath, root.absolutePath, root.absolutePath)63 val log = root.resolve("logs/yokan.log")64 log.parentFile.mkdirs()65 log.writeBytes(ByteArray(190))66 val files = object : LocalFileService by disk {67 override fun promote(sourcePath: String, targetPath: String): Boolean = false68 }6970 val size = appendRotatingLog(files, log.absolutePath, 190L, ByteArray(11), maxBytes = 200)7172 assertEquals(190L, size)73 assertEquals(190L, log.length())74 assertTrue(!root.resolve("logs/yokan.log.1").exists())75 } finally {76 root.deleteRecursively()77 }78 }79}80