-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #929 from ably/feature/retry-ably-operations-core
Add methods for detecting retriable and fatal Ably exceptions
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 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
85 changes: 85 additions & 0 deletions
85
common/src/test/java/com/ably/tracking/common/ConnectionExceptionTests.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,85 @@ | ||
package com.ably.tracking.common | ||
|
||
import com.ably.tracking.ConnectionException | ||
import com.ably.tracking.ErrorInformation | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
|
||
@RunWith(Parameterized::class) | ||
class ConnectionExceptionTests( | ||
private val statusCode: Int, | ||
private val isFatal: Boolean, | ||
private val isRetriable: Boolean, | ||
) { | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters( | ||
name = "Status code: {0} | Is fatal: {1} | Is retriable: {2}" | ||
) | ||
fun data() = listOf( | ||
params( | ||
statusCode = 399, | ||
isFatal = false, | ||
isRetriable = false, | ||
), | ||
params( | ||
statusCode = 400, | ||
isFatal = true, | ||
isRetriable = false, | ||
), | ||
params( | ||
statusCode = 499, | ||
isFatal = true, | ||
isRetriable = false, | ||
), | ||
params( | ||
statusCode = 500, | ||
isFatal = false, | ||
isRetriable = true, | ||
), | ||
params( | ||
statusCode = 504, | ||
isFatal = false, | ||
isRetriable = true, | ||
), | ||
params( | ||
statusCode = 505, | ||
isFatal = false, | ||
isRetriable = false, | ||
), | ||
) | ||
|
||
private fun params(statusCode: Int, isFatal: Boolean, isRetriable: Boolean) = | ||
arrayOf(statusCode, isFatal, isRetriable) | ||
} | ||
|
||
@Test | ||
fun `exception should be treated as fatal if its status code is between 400 and 499`() { | ||
// given | ||
val exception = createConnectionException(statusCode) | ||
|
||
// when | ||
val result = exception.isFatal() | ||
|
||
// then | ||
Assert.assertEquals(isFatal, result) | ||
} | ||
|
||
@Test | ||
fun `exception should be treated as retriable if its status code is between 500 and 504`() { | ||
// given | ||
val exception = createConnectionException(statusCode) | ||
|
||
// when | ||
val result = exception.isRetriable() | ||
|
||
// then | ||
Assert.assertEquals(isRetriable, result) | ||
} | ||
|
||
private fun createConnectionException(statusCode: Int): ConnectionException = | ||
ConnectionException(ErrorInformation(0, statusCode, "Test exception", null, null)) | ||
} |