Workarounds for Kotlin flaky resolve (KT-40998)
authorNicolay Mitropolsky <nicolay.mitropolsky@jetbrains.com>
Tue, 11 Aug 2020 16:23:20 +0000 (19:23 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Tue, 11 Aug 2020 19:51:51 +0000 (19:51 +0000)
GitOrigin-RevId: a9070fa263f60489d9ce0cd1910ce5da4a562388

java/testFramework/src/com/intellij/testFramework/fixtures/kotlin/KtFlakyErrorWorkaround.kt [new file with mode: 0644]

diff --git a/java/testFramework/src/com/intellij/testFramework/fixtures/kotlin/KtFlakyErrorWorkaround.kt b/java/testFramework/src/com/intellij/testFramework/fixtures/kotlin/KtFlakyErrorWorkaround.kt
new file mode 100644 (file)
index 0000000..b86bc12
--- /dev/null
@@ -0,0 +1,49 @@
+package com.intellij.testFramework.fixtures.kotlin
+
+import com.intellij.openapi.diagnostic.logger
+
+class KtFlakyErrorWorkaround(private val setUp: () -> Unit,
+                             private val tearDown: () -> Unit,
+                             private val repeatCount: Int = 5) {
+
+  fun wrapFlaky(actualMessagePattern: Regex, body: () -> Unit): Unit =
+    repeatTest({ e -> e is AssertionError && e.actual.contains(actualMessagePattern) }, body)
+
+  fun wrapFlaky(actualMessagePattern: Regex, body: Runnable): Unit = wrapFlaky(actualMessagePattern) { body.run() }
+
+  fun repeatTest(repeatCondition: (Throwable) -> Boolean = { true }, body: () -> Unit) {
+    var repeat: Boolean
+    var repeatedTimes = 0
+    do {
+      try {
+        body()
+        repeat = false
+      }
+      catch (e: Throwable) {
+        if (repeatCondition(e)) {
+          logger<KtFlakyErrorWorkaround>().warn("Workaround hit! ${e.javaClass} on try: $repeatedTimes")
+          repeatedTimes++
+          repeat = true
+          if (repeatedTimes > repeatCount) {
+            logger<KtFlakyErrorWorkaround>().error("Workaround: hit too many times: $repeatedTimes, max is $repeatCount")
+            throw e
+          }
+          tearDown()
+          setUp()
+        }
+        else {
+          throw e
+        }
+      }
+    }
+    while (repeat)
+  }
+
+  private val AssertionError.actual: String
+    get() = when (this) {
+      is org.junit.ComparisonFailure -> this.actual
+      is junit.framework.ComparisonFailure -> this.actual
+      else -> ""
+    }
+
+}
\ No newline at end of file