Skip to content

Commit 3ec41ea

Browse files
committed
feat: Add test for suspending functions with executeWithRetry
1 parent a6f8907 commit 3ec41ea

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

src/test/kotlin/com/haroldadmin/cnradapter/RetryTest.kt

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.kotlintest.Spec
44
import io.kotlintest.shouldBe
55
import io.kotlintest.shouldNotBe
66
import io.kotlintest.specs.DescribeSpec
7+
import kotlinx.coroutines.runBlocking
78
import okhttp3.mockwebserver.MockResponse
89
import okhttp3.mockwebserver.MockWebServer
910
import okhttp3.mockwebserver.SocketPolicy
@@ -23,11 +24,11 @@ class ExtensionsTest : DescribeSpec() {
2324
server = MockWebServer()
2425
executor = Executors.newSingleThreadExecutor()
2526
retrofit = Retrofit.Builder()
26-
.baseUrl(server.url("/"))
27-
.callbackExecutor(executor)
28-
.addCallAdapterFactory(NetworkResponseAdapterFactory())
29-
.addConverterFactory(StringConverterFactory())
30-
.build()
27+
.baseUrl(server.url("/"))
28+
.callbackExecutor(executor)
29+
.addCallAdapterFactory(NetworkResponseAdapterFactory())
30+
.addConverterFactory(StringConverterFactory())
31+
.build()
3132
service = retrofit.create(Service::class.java)
3233
}
3334

@@ -40,22 +41,45 @@ class ExtensionsTest : DescribeSpec() {
4041
init {
4142
describe("Execute with retry") {
4243

43-
repeat(9) {
44-
val mockResponse = MockResponse()
45-
server.enqueue(mockResponse.apply { mockResponse.socketPolicy = SocketPolicy.DISCONNECT_AFTER_REQUEST })
46-
}
44+
context("Deferred response") {
45+
repeat(9) {
46+
val mockResponse = MockResponse()
47+
server.enqueue(mockResponse.apply { mockResponse.socketPolicy = SocketPolicy.DISCONNECT_AFTER_REQUEST })
48+
}
4749

48-
server.enqueue(MockResponse().setBody("Hi!"))
50+
server.enqueue(MockResponse().setBody("Hi!"))
4951

50-
val response = executeWithRetry(times = 10) {
51-
service.getText().await()
52+
val response = executeWithRetry(times = 10) {
53+
service.getText().await()
54+
}
55+
56+
it("Should end up with NetworkResponse.Success after 10 retries") {
57+
(response is NetworkResponse.Success) shouldBe true
58+
with(response as NetworkResponse.Success) {
59+
body shouldBe "Hi!"
60+
headers shouldNotBe null
61+
}
62+
}
5263
}
5364

54-
it("Should end up with NetworkResponse.Success after 10 retries") {
55-
(response is NetworkResponse.Success) shouldBe true
56-
with(response as NetworkResponse.Success) {
57-
body shouldBe "Hi!"
58-
headers shouldNotBe null
65+
context("Suspending response") {
66+
repeat(9) {
67+
val mockResponse = MockResponse()
68+
server.enqueue(mockResponse.apply { mockResponse.socketPolicy = SocketPolicy.DISCONNECT_AFTER_REQUEST })
69+
}
70+
71+
server.enqueue(MockResponse().setBody("Hi!"))
72+
73+
val response = executeWithRetry(times = 10) {
74+
runBlocking { service.getTextSuspend() }
75+
}
76+
77+
it("Should end up with NetworkResponse.Success after 10 retries") {
78+
(response is NetworkResponse.Success) shouldBe true
79+
with(response as NetworkResponse.Success) {
80+
body shouldBe "Hi!"
81+
headers shouldNotBe null
82+
}
5983
}
6084
}
6185
}

0 commit comments

Comments
 (0)