TestTerminalLogging.kt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2019-2022 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
  10. package net.mamoe.console.integrationtest.testpoints.terminal
  11. import net.mamoe.console.integrationtest.AbstractTestPoint
  12. import net.mamoe.mirai.console.MiraiConsole
  13. import net.mamoe.mirai.console.MiraiConsoleImplementation
  14. import net.mamoe.mirai.console.terminal.LoggingServiceI
  15. import net.mamoe.mirai.console.terminal.MiraiConsoleImplementationTerminal
  16. import net.mamoe.mirai.utils.cast
  17. import net.mamoe.mirai.utils.info
  18. import java.io.File
  19. import java.util.*
  20. import kotlin.math.min
  21. import kotlin.test.assertTrue
  22. import kotlin.test.fail
  23. internal object TestTerminalLogging : AbstractTestPoint() {
  24. override fun beforeConsoleStartup() {
  25. System.setProperty("mirai.console.terminal.log.buffer", "10")
  26. }
  27. override fun onConsoleStartSuccessfully() {
  28. val logService = MiraiConsoleImplementation.getInstance()
  29. .origin
  30. .cast<MiraiConsoleImplementationTerminal>()
  31. .logService.cast<LoggingServiceI>()
  32. logService.autoSplitTask.cancel(true)
  33. File("logs/log-0.log").delete()
  34. val stub = "Terminal Test: STUB" + UUID.randomUUID()
  35. MiraiConsole.mainLogger.info { stub }
  36. Thread.sleep(200L)
  37. assertTrue { File("logs/latest.log").isFile }
  38. assertTrue { File("logs/latest.log").readText().contains(stub) }
  39. logService.switchLogFileNow.invoke()
  40. assertTrue { File("logs/latest.log").isFile }
  41. assertTrue { !File("logs/latest.log").readText().contains(stub) }
  42. assertTrue { File("logs/log-0.log").isFile }
  43. assertTrue { File("logs/log-0.log").readText().contains(stub) }
  44. MiraiConsole.mainLogger.info("Pipeline size: " + logService.pipelineSize)
  45. val logs = mutableListOf<String>()
  46. logs.add("1================================================================")
  47. repeat(100) { logs.add("TEST LINE $it -") }
  48. logs.add("2================================================================")
  49. logs.forEach { MiraiConsole.mainLogger.info(it) }
  50. Thread.sleep(200L)
  51. val lns = File("logs/latest.log").readLines().mapNotNull { line ->
  52. logs.forEach { lx ->
  53. if (line.contains(lx)) {
  54. return@mapNotNull lx
  55. }
  56. }
  57. return@mapNotNull null
  58. }
  59. logService.switchLogFileNow.invoke()
  60. // lns.forEach { println(it) }
  61. var matched = 0
  62. for (i in 0 until min(lns.size, logs.size)) {
  63. if (lns[i] == logs[i]) matched++
  64. }
  65. println("Matched line: $matched, logs: ${logs.size}")
  66. if (matched < (logs.size * 80 / 100)) {
  67. lns.forEach { System.err.println(it) }
  68. fail()
  69. }
  70. }
  71. }