-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
150 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package streams.utils | ||
|
||
import kotlinx.coroutines.delay | ||
|
||
suspend fun <T> retryForException(exceptions: Array<Class<out Throwable>>, retries: Int, delayTime: Long, action: () -> T): T { | ||
return try { | ||
action() | ||
} catch (e: Exception) { | ||
val isInstance = exceptions.any { it.isInstance(e) } | ||
if (isInstance && retries > 0) { | ||
delay(delayTime) | ||
retryForException(exceptions = exceptions, retries = retries - 1, delayTime = delayTime, action = action) | ||
} else { | ||
throw e | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
common/src/test/kotlin/streams/utils/CoroutineUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package streams.utils | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Test | ||
import java.io.IOException | ||
import java.lang.ClassCastException | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class CoroutineUtilsTest { | ||
|
||
@Test | ||
fun `should success after retry for known exception`() = runBlocking { | ||
var count = 0 | ||
var excuted = false | ||
retryForException<Unit>(exceptions = arrayOf(RuntimeException::class.java), | ||
retries = 4, delayTime = 100) { | ||
if (count < 2) { | ||
++count | ||
throw RuntimeException() | ||
} | ||
excuted = true | ||
} | ||
|
||
assertEquals(2, count) | ||
assertTrue { excuted } | ||
} | ||
|
||
@Test(expected = RuntimeException::class) | ||
fun `should fail after retry for known exception`() { | ||
var retries = 3 | ||
runBlocking { | ||
retryForException<Unit>(exceptions = arrayOf(RuntimeException::class.java), | ||
retries = 3, delayTime = 100) { | ||
if (retries >= 0) { | ||
--retries | ||
throw RuntimeException() | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `should fail fast unknown exception`() { | ||
var iteration = 0 | ||
var isIOException = false | ||
try { | ||
runBlocking { | ||
retryForException<Unit>(exceptions = arrayOf(RuntimeException::class.java), | ||
retries = 3, delayTime = 100) { | ||
if (iteration >= 0) { | ||
++iteration | ||
throw IOException() | ||
} | ||
} | ||
} | ||
} catch (e: Exception) { | ||
isIOException = e is IOException | ||
} | ||
assertTrue { isIOException } | ||
assertEquals(1, iteration) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...eams/kafka/connect/sink/PropertiesUtil.kt → ...ams/kafka/connect/utils/PropertiesUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters