11package io.sentry.util
22
33import com.google.common.truth.Truth.assertThat
4+ import io.sentry.IScopes
5+ import io.sentry.Sentry
6+ import io.sentry.SentryOptions
47import java.util.TimeZone
58import java.util.concurrent.TimeUnit
69import kotlin.test.Test
10+ import org.mockito.Mockito
11+ import org.mockito.kotlin.mock
12+ import org.mockito.kotlin.whenever
713
814class MonitorConfigUtilsTest {
915
@@ -18,6 +24,50 @@ class MonitorConfigUtilsTest {
1824 assertThat(config.timezone).isNull()
1925 }
2026
27+ @Test
28+ fun `cron at second 0 keeps the default check-in margin` () {
29+ val config = MonitorConfigUtils .fromSchedule(" 0 * * * * *" , null , null )
30+
31+ assertThat(config!! .checkinMargin).isNull()
32+ }
33+
34+ @Test
35+ fun `cron with non-zero seconds gets a 2 minute check-in margin` () {
36+ for (cron in listOf (" 1 * * * * *" , " 59 * * * * *" , " 30 15 10 * * *" )) {
37+ assertThat(MonitorConfigUtils .fromSchedule(cron, null , null )!! .checkinMargin).isEqualTo(2L )
38+ }
39+ }
40+
41+ @Test
42+ fun `cron with non-zero seconds raises a smaller default check-in margin` () {
43+ withDefaultCheckinMargin(1L ) {
44+ assertThat(MonitorConfigUtils .fromSchedule(" 59 * * * * *" , null , null )!! .checkinMargin)
45+ .isEqualTo(2L )
46+ }
47+ }
48+
49+ @Test
50+ fun `cron with non-zero seconds keeps a larger default check-in margin` () {
51+ withDefaultCheckinMargin(5L ) {
52+ assertThat(MonitorConfigUtils .fromSchedule(" 59 * * * * *" , null , null )!! .checkinMargin)
53+ .isEqualTo(5L )
54+ }
55+ }
56+
57+ private fun withDefaultCheckinMargin (margin : Long , block : () -> Unit ) {
58+ Mockito .mockStatic(Sentry ::class .java).use { sentry ->
59+ val scopes = mock<IScopes >()
60+ sentry.`when `<Any > { Sentry .getCurrentScopes() }.thenReturn(scopes)
61+ whenever(scopes.options)
62+ .thenReturn(
63+ SentryOptions ().apply {
64+ cron = SentryOptions .Cron ().apply { defaultCheckinMargin = margin }
65+ }
66+ )
67+ block()
68+ }
69+ }
70+
2171 @Test
2272 fun `cron whitespace is normalized` () {
2373 val config = MonitorConfigUtils .fromSchedule(" 30 0 2 * * * " , null , null )
0 commit comments