Skip to content

Commit 40e4e66

Browse files
committed
ANDROID-16807 Add waitForCondition at loggerazzi logs comparation
1 parent ac1acdb commit 40e4e66

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

loggerazzi/src/main/java/com/telefonica/loggerazzi/LoggerazziRule.kt

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,55 @@ open class GenericLoggerazziRule<LogType>(
5353
val testName = "${description?.className}_${description?.methodName}"
5454
val fileName = "${testName}.${System.nanoTime()}"
5555

56-
val recordedLogs = recorder.getRecordedLogs()
57-
val log = recordedLogs.joinToString("\n") { stringMapper.fromLog(it) }
58-
val testFile = File(recordedDir, fileName)
59-
testFile.createNewFile()
60-
testFile.writeText(log)
61-
56+
val recordedLogs: List<LogType>
6257
if (InstrumentationRegistry.getArguments().getString("record") != "true" && !isTestIgnored) {
6358
val goldenFile =
6459
InstrumentationRegistry.getInstrumentation().context.assets.open(
6560
"loggerazzi-golden-files/${testName}.txt"
6661
)
6762
val goldenStringLogs = String(goldenFile.readBytes()).takeIf { it.isNotEmpty() }?.split("\n") ?: emptyList()
68-
val result = comparator.compare(recordedLogs, goldenStringLogs.map { stringMapper.toLog(it) })
69-
if (result != null) {
63+
val comparationResult = compare(goldenStringLogs)
64+
if (!comparationResult.success) {
7065
val compareFile = File(failuresDir, fileName)
7166
compareFile.createNewFile()
72-
compareFile.writeText(result)
73-
throw AssertionError("Logs do not match:\n$result")
67+
compareFile.writeText(comparationResult.result!!)
68+
throw AssertionError("Logs do not match:\n${comparationResult.result}")
7469
}
70+
recordedLogs = comparationResult.recordedLogs
71+
} else {
72+
recordedLogs = recorder.getRecordedLogs()
7573
}
74+
75+
val log = recordedLogs.joinToString("\n") { stringMapper.fromLog(it) }
76+
val testFile = File(recordedDir, fileName)
77+
testFile.createNewFile()
78+
testFile.writeText(log)
79+
}
80+
81+
private fun compare(goldenStringLogs: List<String>): ComparationResult<LogType> {
82+
val startTime = System.currentTimeMillis()
83+
var comparationResult: ComparationResult<LogType>
84+
do {
85+
val recordedLogs = recorder.getRecordedLogs()
86+
val result = comparator.compare(recordedLogs, goldenStringLogs.map { stringMapper.toLog(it) })
87+
comparationResult = ComparationResult(result, recordedLogs)
88+
if (!comparationResult.success) {
89+
Thread.sleep(RESULT_POLLING_INTERVAL_MS)
90+
}
91+
} while (!comparationResult.success && System.currentTimeMillis() - startTime < RESULT_TIMEOUT_MS)
92+
return comparationResult
93+
}
94+
95+
private data class ComparationResult<LogType>(
96+
val result: String?,
97+
val recordedLogs: List<LogType>,
98+
) {
99+
val success: Boolean
100+
get() = result == null
101+
}
102+
103+
private companion object {
104+
const val RESULT_POLLING_INTERVAL_MS = 500L
105+
const val RESULT_TIMEOUT_MS = 5000L
76106
}
77107
}

0 commit comments

Comments
 (0)