Skip to content

Commit 3fce2f7

Browse files
fix: Add retry and silent crash to mixpanel send method (#2142)
1 parent b112436 commit 3fce2f7

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

flank_wrapper/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33

44
# Ignore Gradle build output directory
55
build
6+
7+
src/main/resources/version.txt

flank_wrapper/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ shadowJar.apply {
2424
}
2525
}
2626
// <breaking change>.<feature added>.<fix/minor change>
27-
version = "1.2.3"
27+
version = "1.2.4"
2828
group = "com.github.flank"
2929

3030
repositories {

tool/analytics/mixpanel/build.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ repositories {
88
mavenCentral()
99
}
1010

11+
tasks.test {
12+
maxHeapSize = "2048m"
13+
minHeapSize = "512m"
14+
}
15+
1116
tasks.withType<KotlinCompile> { kotlinOptions.jvmTarget = "1.8" }
1217

1318
dependencies {
@@ -20,4 +25,5 @@ dependencies {
2025
implementation(project(Modules.ANALYTICS))
2126

2227
testImplementation(Dependencies.JUNIT)
28+
testImplementation(Dependencies.MOCKK)
2329
}

tool/analytics/mixpanel/src/main/kotlin/flank/tool/analytics/mixpanel/internal/Send.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal fun sendReport(
1616
listOf(
1717
createUser(Report.projectName),
1818
createEvent(Report.projectName, eventName, Report.data),
19-
).forEach(apiClient::sendMessage)
19+
).forEach(::send)
2020
}
2121

2222
private fun createUser(
@@ -43,6 +43,10 @@ private fun createEvent(
4343
JSONObject(data + (SESSION_ID to sessionId))
4444
)
4545

46+
private fun send(message: JSONObject) {
47+
retryWithSilentFailure { apiClient.sendMessage(message) }
48+
}
49+
4650
private val messageBuilder by lazy { MessageBuilder(MIXPANEL_API_TOKEN) }
4751
private val apiClient by lazy { MixpanelAPI() }
4852

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package flank.tool.analytics.mixpanel.internal
2+
3+
import kotlin.math.max
4+
5+
internal fun retryWithSilentFailure(times: Int = 1, block: () -> Unit) {
6+
(0..max(0, times)).onEach { runCatching { block() }.onSuccess { return } }
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package flank.tool.analytics.mixpanel.internal
2+
3+
import io.mockk.every
4+
import io.mockk.mockk
5+
import io.mockk.verify
6+
import org.junit.Test
7+
8+
class UtilsTest {
9+
10+
@Test
11+
fun `should not retry succeed code`() {
12+
// given
13+
val mockedRun = mockk<Runnable>()
14+
every { mockedRun.run() } returns Unit
15+
16+
// when
17+
retryWithSilentFailure(5) {
18+
mockedRun.run()
19+
}
20+
21+
// then
22+
verify(exactly = 1) { mockedRun.run() }
23+
}
24+
25+
@Test
26+
fun `should retry given times`() {
27+
// given
28+
val mockedRun = mockk<Runnable>()
29+
every { mockedRun.run() } throws IllegalStateException("test")
30+
31+
// when
32+
retryWithSilentFailure(5) {
33+
mockedRun.run()
34+
}
35+
36+
// then
37+
verify(exactly = 5 + 1) { mockedRun.run() }
38+
}
39+
}

0 commit comments

Comments
 (0)