git.iko.soy/opencode-android
on 8948962 help sign in
60 lines 1.9 kB history raw
1package soy.iko.opencode.data.repo
3import io.ktor.client.plugins.HttpRequestTimeoutException
4import io.ktor.client.request.HttpRequestBuilder
5import org.junit.Assert.assertEquals
6import org.junit.Test
7import java.io.IOException
8import java.net.ConnectException
9import java.net.SocketTimeoutException
10import java.net.UnknownHostException
12class ErrorKindTest {
14 @Test
15 fun unknownHostIsNotReachable() {
16 assertEquals(ErrorKind.NOT_REACHABLE, classifyError(UnknownHostException("nope")))
17 }
19 @Test
20 fun connectRefusedIsNotReachable() {
21 assertEquals(ErrorKind.NOT_REACHABLE, classifyError(ConnectException("refused")))
22 }
24 @Test
25 fun socketTimeoutIsTimeout() {
26 assertEquals(ErrorKind.TIMEOUT, classifyError(SocketTimeoutException("read timed out")))
27 }
29 @Test
30 fun httpRequestTimeoutIsTimeout() {
31 assertEquals(ErrorKind.TIMEOUT, classifyError(HttpRequestTimeoutException(HttpRequestBuilder())))
32 }
34 @Test
35 fun genericIoIsNetwork() {
36 assertEquals(ErrorKind.NETWORK, classifyError(IOException("broken pipe")))
37 }
39 @Test
40 fun unrelatedThrowableIsUnknown() {
41 assertEquals(ErrorKind.UNKNOWN, classifyError(IllegalStateException("boom")))
42 }
44 @Test
45 fun unwrapsCauseChainToClassifyRoot() {
46 // A library may surface a network failure wrapped in a non-IO exception.
47 val wrapped = RuntimeException("request failed", UnknownHostException("nested"))
48 assertEquals(ErrorKind.NOT_REACHABLE, classifyError(wrapped))
49 }
51 @Test
52 fun doesNotLoopInfinitelyOnCyclicCause() {
53 // A self-referential cause chain must be terminated by the bounded hop count,
54 // not followed into a stack overflow.
55 val cyclic: Throwable = object : Throwable("cyclic") {
56 override val cause: Throwable? get() = this
57 }
58 assertEquals(ErrorKind.UNKNOWN, classifyError(cyclic))
59 }