Skip to content

Commit e6ac171

Browse files
committed
Fix unit tests
1 parent d4c69b3 commit e6ac171

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@
2222

2323
### Metrics
2424
```text
25-
14991 number of properties
26-
10385 number of functions
27-
8856 number of classes
25+
14993 number of properties
26+
10384 number of functions
27+
8855 number of classes
2828
229 number of packages
2929
3486 number of kt files
3030
```
3131

3232

3333
### Complexity Report
3434
```text
35-
259507 lines of code (loc)
36-
158943 source lines of code (sloc)
37-
115975 logical lines of code (lloc)
38-
72503 comment lines of code (cloc)
39-
24705 cyclomatic complexity (mcc)
40-
20233 cognitive complexity
35+
259493 lines of code (loc)
36+
158946 source lines of code (sloc)
37+
115976 logical lines of code (lloc)
38+
72487 comment lines of code (cloc)
39+
24701 cyclomatic complexity (mcc)
40+
20232 cognitive complexity
4141
0 number of total code smells
4242
45 comment source ratio
43-
213 mcc per 1,000 lloc
43+
212 mcc per 1,000 lloc
4444
```

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ plugins {
3535
application
3636
jacoco
3737
id("com.github.johnrengelman.shadow") version "8.1.1"
38-
// id ("org.sonarqube") version "4.4.1.3373"
38+
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.17.0"
3939
idea
4040
alias(libs.plugins.kt.jvm)
4141
alias(libs.plugins.detekt)

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ truth = "1.4.4" # https://central.sonatype.com/artifact/com.google.t
3434
sandwich = "2.1.0" # https://github.com/skydoves/sandwich
3535
protobuf = "0.9.2"
3636
protoc = "4.30.2"
37+
binaryCompatibility = "0.17.0"
3738

3839
[libraries]
3940
kotlin-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8", version.ref = "coroutines" }
@@ -96,3 +97,4 @@ kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }
9697
diktat = { id = "com.saveourtool.diktat", version.ref = "diktat" }
9798
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
9899
protobuf = { id = "com.google.protobuf", version.ref = "protobuf" }
100+
kotlin-binary-compatibility = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibility" }

src/test/kotlin/dev/shtanko/retry/RetryTest.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import kotlinx.coroutines.CoroutineScope
88
import kotlinx.coroutines.Dispatchers
99
import kotlinx.coroutines.cancelAndJoin
1010
import kotlinx.coroutines.launch
11+
import kotlinx.coroutines.sync.Mutex
12+
import kotlinx.coroutines.sync.withLock
1113
import kotlinx.coroutines.test.runTest
1214
import okhttp3.ExperimentalOkHttpApi
1315
import okhttp3.ResponseBody
@@ -16,6 +18,7 @@ import okhttp3.mockwebserver.MockWebServer
1618
import org.junit.jupiter.api.AfterEach
1719
import org.junit.jupiter.api.Assertions.assertEquals
1820
import org.junit.jupiter.api.Assertions.assertTrue
21+
import org.junit.jupiter.api.RepeatedTest
1922
import org.junit.jupiter.api.Test
2023
import org.junit.jupiter.api.assertThrows
2124
import retrofit2.HttpException
@@ -76,36 +79,38 @@ class RetryTest {
7679
assertEquals(3, counter, "Should retry 3 times before failing")
7780
}
7881

79-
@Test
82+
@RepeatedTest(10)
8083
fun `should handle concurrency and correctly update attempt`() = runTest {
81-
// Set up a CountDownLatch to wait for all coroutines to finish
8284
val latch = CountDownLatch(5)
8385
val retryAttempts = mutableListOf<Int>()
8486
val expectedAttempts = 5
87+
val mutex = Mutex()
8588

89+
// Launch multiple coroutines to simulate concurrent retries
8690
val job = CoroutineScope(Dispatchers.Default).launch {
8791
repeat(expectedAttempts) {
8892
launch {
93+
var attempt = 0
8994
try {
90-
retry(policy = DefaultRetryPolicy(maxAttempts = 3)) {
95+
retry(policy = DefaultRetryPolicy(maxAttempts = 3)) { currentAttempt ->
96+
attempt = currentAttempt
9197
throw IOException("Simulated failure")
9298
}
93-
} catch (e: IOException) {
94-
// Capture the attempt number from AtomicInteger (using attempt.get())
95-
retryAttempts.add(e.message?.let { "Failed on attempt: $it" }?.length ?: -1)
99+
} catch (_: IOException) {
100+
mutex.withLock {
101+
retryAttempts.add(attempt)
102+
}
96103
} finally {
97104
latch.countDown()
98105
}
99106
}
100107
}
101108
}
102109

103-
latch.await() // Wait for all coroutines to finish
104-
105-
// Verify that all attempts are captured correctly
106-
assertTrue(retryAttempts.size == expectedAttempts)
107-
retryAttempts.forEach {
108-
assertTrue(it > 0) // Ensure retry attempts were recorded
110+
latch.await()
111+
assertEquals(expectedAttempts, retryAttempts.size)
112+
retryAttempts.forEach { attempt ->
113+
assertTrue(attempt in 1..3, "Retry attempt should be between 1 and 3")
109114
}
110115

111116
job.cancelAndJoin()

0 commit comments

Comments
 (0)