Skip to content

Commit f04bdbc

Browse files
committed
improve test cases
1 parent 4a52a0d commit f04bdbc

File tree

6 files changed

+222
-6
lines changed

6 files changed

+222
-6
lines changed

sentry-spring-boot-4/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ dependencies {
8585
testImplementation(libs.okhttp.mockwebserver)
8686
testImplementation(libs.otel)
8787
testImplementation(libs.otel.extension.autoconfigure.spi)
88+
testImplementation(projects.sentryAsyncProfiler)
8889
/**
8990
* Adding a version of opentelemetry-spring-boot-starter that doesn't support Spring Boot 4 causes
9091
* java.lang.IllegalArgumentException: Could not find class

sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryAutoConfigurationTest.kt

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import io.sentry.IProfileConverter
1212
import io.sentry.IScopes
1313
import io.sentry.ITransportFactory
1414
import io.sentry.Integration
15+
import io.sentry.NoOpContinuousProfiler
16+
import io.sentry.NoOpProfileConverter
1517
import io.sentry.NoOpTransportFactory
1618
import io.sentry.SamplingContext
1719
import io.sentry.Sentry
@@ -20,6 +22,8 @@ import io.sentry.SentryIntegrationPackageStorage
2022
import io.sentry.SentryLevel
2123
import io.sentry.SentryLogEvent
2224
import io.sentry.SentryOptions
25+
import io.sentry.asyncprofiler.profiling.JavaContinuousProfiler
26+
import io.sentry.asyncprofiler.provider.AsyncProfilerProfileConverterProvider
2327
import io.sentry.checkEvent
2428
import io.sentry.opentelemetry.SentryAutoConfigurationCustomizerProvider
2529
import io.sentry.opentelemetry.agent.AgentMarker
@@ -47,6 +51,7 @@ import kotlin.test.assertFalse
4751
import kotlin.test.assertTrue
4852
import org.aspectj.lang.ProceedingJoinPoint
4953
import org.assertj.core.api.Assertions.assertThat
54+
import org.mockito.internal.util.MockUtil.isMock
5055
import org.mockito.kotlin.any
5156
import org.mockito.kotlin.anyOrNull
5257
import org.mockito.kotlin.mock
@@ -1046,13 +1051,69 @@ class SentryAutoConfigurationTest {
10461051
contextRunner
10471052
.withPropertyValues(
10481053
"sentry.dsn=http://key@localhost/proj",
1049-
"sentry.traces-sample-rate=1.0",
1054+
"sentry.profile-session-sample-rate=1.0",
1055+
)
1056+
.run {
1057+
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
1058+
assertThat(it).hasSingleBean(IProfileConverter::class.java)
1059+
assertThat(it)
1060+
.getBean(IProfileConverter::class.java)
1061+
.isInstanceOf(
1062+
AsyncProfilerProfileConverterProvider.AsyncProfilerProfileConverter::class.java
1063+
)
1064+
assertThat(it)
1065+
.getBean(IContinuousProfiler::class.java)
1066+
.isInstanceOf(JavaContinuousProfiler::class.java)
1067+
assertThat(it)
1068+
.getBean(IProfileConverter::class.java)
1069+
.isSameAs(Sentry.getGlobalScope().options.profilerConverter)
1070+
assertThat(it)
1071+
.getBean(IContinuousProfiler::class.java)
1072+
.isSameAs(Sentry.getGlobalScope().options.continuousProfiler)
1073+
}
1074+
}
1075+
1076+
@Test
1077+
fun `when AgentMarker is on the classpath and ContinuousProfiling is enabled IContinuousProfiler and IProfileConverter exist beans are taken from options`() {
1078+
SentryIntegrationPackageStorage.getInstance().clearStorage()
1079+
1080+
contextRunner
1081+
.withPropertyValues(
1082+
"sentry.dsn=http://key@localhost/proj",
1083+
"sentry.profile-session-sample-rate=1.0",
10501084
"sentry.auto-init=false",
10511085
"debug=true",
10521086
)
1087+
.withUserConfiguration(CustomProfilerOptionsConfigurationConfiguration::class.java)
10531088
.run {
1089+
val profiler = it.getBean(IContinuousProfiler::class.java)
1090+
assertTrue(isMock(profiler))
10541091
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
10551092
assertThat(it).hasSingleBean(IProfileConverter::class.java)
1093+
assertThat(it)
1094+
.getBean(IProfileConverter::class.java)
1095+
.isSameAs(Sentry.getGlobalScope().options.profilerConverter)
1096+
assertThat(it)
1097+
.getBean(IContinuousProfiler::class.java)
1098+
.isSameAs(Sentry.getGlobalScope().options.continuousProfiler)
1099+
}
1100+
}
1101+
1102+
@Test
1103+
fun `when AgentMarker is on the classpath and ContinuousProfiling is disabled NoOp Beans are created`() {
1104+
SentryIntegrationPackageStorage.getInstance().clearStorage()
1105+
1106+
contextRunner
1107+
.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.auto-init=false")
1108+
.run {
1109+
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
1110+
assertThat(it).hasSingleBean(IProfileConverter::class.java)
1111+
assertThat(it)
1112+
.getBean(IProfileConverter::class.java)
1113+
.isInstanceOf(NoOpProfileConverter::class.java)
1114+
assertThat(it)
1115+
.getBean(IContinuousProfiler::class.java)
1116+
.isInstanceOf(NoOpContinuousProfiler::class.java)
10561117
}
10571118
}
10581119

@@ -1062,7 +1123,6 @@ class SentryAutoConfigurationTest {
10621123
contextRunner
10631124
.withPropertyValues(
10641125
"sentry.dsn=http://key@localhost/proj",
1065-
"sentry.traces-sample-rate=1.0",
10661126
"sentry.profile-session-sample-rate=1.0",
10671127
"debug=true",
10681128
)
@@ -1118,6 +1178,17 @@ class SentryAutoConfigurationTest {
11181178
@Bean open fun sentryOptionsConfiguration() = Sentry.OptionsConfiguration<SentryOptions> {}
11191179
}
11201180

1181+
@Configuration(proxyBeanMethods = false)
1182+
open class CustomProfilerOptionsConfigurationConfiguration {
1183+
private val profiler = mock<IContinuousProfiler>()
1184+
1185+
@Bean
1186+
open fun customOptionsConfiguration() =
1187+
Sentry.OptionsConfiguration<SentryOptions> { it.setContinuousProfiler(profiler) }
1188+
1189+
@Bean open fun beforeSendCallback() = CustomBeforeSendCallback()
1190+
}
1191+
11211192
@Configuration(proxyBeanMethods = false)
11221193
open class MockTransportConfiguration {
11231194

sentry-spring-boot-jakarta/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ dependencies {
8888
testImplementation(libs.springboot3.starter.test)
8989
testImplementation(libs.springboot3.starter.web)
9090
testImplementation(libs.springboot3.starter.webflux)
91+
testImplementation(projects.sentryAsyncProfiler)
9192
}
9293

9394
configure<SourceSetContainer> { test { java.srcDir("src/test/java") } }

sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import io.sentry.IProfileConverter
1313
import io.sentry.IScopes
1414
import io.sentry.ITransportFactory
1515
import io.sentry.Integration
16+
import io.sentry.NoOpContinuousProfiler
17+
import io.sentry.NoOpProfileConverter
1618
import io.sentry.NoOpTransportFactory
1719
import io.sentry.ProfileLifecycle
1820
import io.sentry.SamplingContext
@@ -22,6 +24,8 @@ import io.sentry.SentryIntegrationPackageStorage
2224
import io.sentry.SentryLevel
2325
import io.sentry.SentryLogEvent
2426
import io.sentry.SentryOptions
27+
import io.sentry.asyncprofiler.profiling.JavaContinuousProfiler
28+
import io.sentry.asyncprofiler.provider.AsyncProfilerProfileConverterProvider
2529
import io.sentry.checkEvent
2630
import io.sentry.clientreport.DiscardReason
2731
import io.sentry.opentelemetry.SentryAutoConfigurationCustomizerProvider
@@ -51,6 +55,7 @@ import kotlin.test.assertFalse
5155
import kotlin.test.assertTrue
5256
import org.aspectj.lang.ProceedingJoinPoint
5357
import org.assertj.core.api.Assertions.assertThat
58+
import org.mockito.internal.util.MockUtil.isMock
5459
import org.mockito.kotlin.any
5560
import org.mockito.kotlin.anyOrNull
5661
import org.mockito.kotlin.mock
@@ -1068,13 +1073,69 @@ class SentryAutoConfigurationTest {
10681073
contextRunner
10691074
.withPropertyValues(
10701075
"sentry.dsn=http://key@localhost/proj",
1071-
"sentry.traces-sample-rate=1.0",
1076+
"sentry.profile-session-sample-rate=1.0",
1077+
)
1078+
.run {
1079+
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
1080+
assertThat(it).hasSingleBean(IProfileConverter::class.java)
1081+
assertThat(it)
1082+
.getBean(IProfileConverter::class.java)
1083+
.isInstanceOf(
1084+
AsyncProfilerProfileConverterProvider.AsyncProfilerProfileConverter::class.java
1085+
)
1086+
assertThat(it)
1087+
.getBean(IContinuousProfiler::class.java)
1088+
.isInstanceOf(JavaContinuousProfiler::class.java)
1089+
assertThat(it)
1090+
.getBean(IProfileConverter::class.java)
1091+
.isSameAs(Sentry.getGlobalScope().options.profilerConverter)
1092+
assertThat(it)
1093+
.getBean(IContinuousProfiler::class.java)
1094+
.isSameAs(Sentry.getGlobalScope().options.continuousProfiler)
1095+
}
1096+
}
1097+
1098+
@Test
1099+
fun `when AgentMarker is on the classpath and ContinuousProfiling is enabled IContinuousProfiler and IProfileConverter exist beans are taken from options`() {
1100+
SentryIntegrationPackageStorage.getInstance().clearStorage()
1101+
1102+
contextRunner
1103+
.withPropertyValues(
1104+
"sentry.dsn=http://key@localhost/proj",
1105+
"sentry.profile-session-sample-rate=1.0",
10721106
"sentry.auto-init=false",
10731107
"debug=true",
10741108
)
1109+
.withUserConfiguration(CustomProfilerOptionsConfigurationConfiguration::class.java)
10751110
.run {
1111+
val profiler = it.getBean(IContinuousProfiler::class.java)
1112+
assertTrue(isMock(profiler))
10761113
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
10771114
assertThat(it).hasSingleBean(IProfileConverter::class.java)
1115+
assertThat(it)
1116+
.getBean(IProfileConverter::class.java)
1117+
.isSameAs(Sentry.getGlobalScope().options.profilerConverter)
1118+
assertThat(it)
1119+
.getBean(IContinuousProfiler::class.java)
1120+
.isSameAs(Sentry.getGlobalScope().options.continuousProfiler)
1121+
}
1122+
}
1123+
1124+
@Test
1125+
fun `when AgentMarker is on the classpath and ContinuousProfiling is disabled NoOp Beans are created`() {
1126+
SentryIntegrationPackageStorage.getInstance().clearStorage()
1127+
1128+
contextRunner
1129+
.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.auto-init=false")
1130+
.run {
1131+
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
1132+
assertThat(it).hasSingleBean(IProfileConverter::class.java)
1133+
assertThat(it)
1134+
.getBean(IProfileConverter::class.java)
1135+
.isInstanceOf(NoOpProfileConverter::class.java)
1136+
assertThat(it)
1137+
.getBean(IContinuousProfiler::class.java)
1138+
.isInstanceOf(NoOpContinuousProfiler::class.java)
10781139
}
10791140
}
10801141

@@ -1084,7 +1145,6 @@ class SentryAutoConfigurationTest {
10841145
contextRunner
10851146
.withPropertyValues(
10861147
"sentry.dsn=http://key@localhost/proj",
1087-
"sentry.traces-sample-rate=1.0",
10881148
"sentry.profile-session-sample-rate=1.0",
10891149
"debug=true",
10901150
)
@@ -1140,6 +1200,17 @@ class SentryAutoConfigurationTest {
11401200
@Bean open fun sentryOptionsConfiguration() = Sentry.OptionsConfiguration<SentryOptions> {}
11411201
}
11421202

1203+
@Configuration(proxyBeanMethods = false)
1204+
open class CustomProfilerOptionsConfigurationConfiguration {
1205+
private val profiler = mock<IContinuousProfiler>()
1206+
1207+
@Bean
1208+
open fun customOptionsConfiguration() =
1209+
Sentry.OptionsConfiguration<SentryOptions> { it.setContinuousProfiler(profiler) }
1210+
1211+
@Bean open fun beforeSendCallback() = CustomBeforeSendCallback()
1212+
}
1213+
11431214
@Configuration(proxyBeanMethods = false)
11441215
open class MockTransportConfiguration {
11451216

sentry-spring-boot/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies {
7272
testImplementation(projects.sentryOpentelemetry.sentryOpentelemetryAgent)
7373
testImplementation(projects.sentryOpentelemetry.sentryOpentelemetryAgentcustomization)
7474
testImplementation(projects.sentryOpentelemetry.sentryOpentelemetryBootstrap)
75+
testImplementation(projects.sentryAsyncProfiler)
7576
}
7677

7778
configure<SourceSetContainer> { test { java.srcDir("src/test/java") } }

sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import io.sentry.IProfileConverter
1313
import io.sentry.IScopes
1414
import io.sentry.ITransportFactory
1515
import io.sentry.Integration
16+
import io.sentry.NoOpContinuousProfiler
17+
import io.sentry.NoOpProfileConverter
1618
import io.sentry.NoOpTransportFactory
1719
import io.sentry.ProfileLifecycle
1820
import io.sentry.SamplingContext
@@ -22,6 +24,8 @@ import io.sentry.SentryIntegrationPackageStorage
2224
import io.sentry.SentryLevel
2325
import io.sentry.SentryLogEvent
2426
import io.sentry.SentryOptions
27+
import io.sentry.asyncprofiler.profiling.JavaContinuousProfiler
28+
import io.sentry.asyncprofiler.provider.AsyncProfilerProfileConverterProvider
2529
import io.sentry.checkEvent
2630
import io.sentry.clientreport.DiscardReason
2731
import io.sentry.opentelemetry.SentryAutoConfigurationCustomizerProvider
@@ -51,6 +55,7 @@ import kotlin.test.assertFalse
5155
import kotlin.test.assertTrue
5256
import org.aspectj.lang.ProceedingJoinPoint
5357
import org.assertj.core.api.Assertions.assertThat
58+
import org.mockito.internal.util.MockUtil.isMock
5459
import org.mockito.kotlin.any
5560
import org.mockito.kotlin.anyOrNull
5661
import org.mockito.kotlin.mock
@@ -919,13 +924,69 @@ class SentryAutoConfigurationTest {
919924
contextRunner
920925
.withPropertyValues(
921926
"sentry.dsn=http://key@localhost/proj",
922-
"sentry.traces-sample-rate=1.0",
927+
"sentry.profile-session-sample-rate=1.0",
928+
)
929+
.run {
930+
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
931+
assertThat(it).hasSingleBean(IProfileConverter::class.java)
932+
assertThat(it)
933+
.getBean(IProfileConverter::class.java)
934+
.isInstanceOf(
935+
AsyncProfilerProfileConverterProvider.AsyncProfilerProfileConverter::class.java
936+
)
937+
assertThat(it)
938+
.getBean(IContinuousProfiler::class.java)
939+
.isInstanceOf(JavaContinuousProfiler::class.java)
940+
assertThat(it)
941+
.getBean(IProfileConverter::class.java)
942+
.isSameAs(Sentry.getGlobalScope().options.profilerConverter)
943+
assertThat(it)
944+
.getBean(IContinuousProfiler::class.java)
945+
.isSameAs(Sentry.getGlobalScope().options.continuousProfiler)
946+
}
947+
}
948+
949+
@Test
950+
fun `when AgentMarker is on the classpath and ContinuousProfiling is enabled IContinuousProfiler and IProfileConverter exist beans are taken from options`() {
951+
SentryIntegrationPackageStorage.getInstance().clearStorage()
952+
953+
contextRunner
954+
.withPropertyValues(
955+
"sentry.dsn=http://key@localhost/proj",
956+
"sentry.profile-session-sample-rate=1.0",
923957
"sentry.auto-init=false",
924958
"debug=true",
925959
)
960+
.withUserConfiguration(CustomProfilerOptionsConfigurationConfiguration::class.java)
926961
.run {
962+
val profiler = it.getBean(IContinuousProfiler::class.java)
963+
assertTrue(isMock(profiler))
927964
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
928965
assertThat(it).hasSingleBean(IProfileConverter::class.java)
966+
assertThat(it)
967+
.getBean(IProfileConverter::class.java)
968+
.isSameAs(Sentry.getGlobalScope().options.profilerConverter)
969+
assertThat(it)
970+
.getBean(IContinuousProfiler::class.java)
971+
.isSameAs(Sentry.getGlobalScope().options.continuousProfiler)
972+
}
973+
}
974+
975+
@Test
976+
fun `when AgentMarker is on the classpath and ContinuousProfiling is disabled NoOp Beans are created`() {
977+
SentryIntegrationPackageStorage.getInstance().clearStorage()
978+
979+
contextRunner
980+
.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.auto-init=false")
981+
.run {
982+
assertThat(it).hasSingleBean(IContinuousProfiler::class.java)
983+
assertThat(it).hasSingleBean(IProfileConverter::class.java)
984+
assertThat(it)
985+
.getBean(IProfileConverter::class.java)
986+
.isInstanceOf(NoOpProfileConverter::class.java)
987+
assertThat(it)
988+
.getBean(IContinuousProfiler::class.java)
989+
.isInstanceOf(NoOpContinuousProfiler::class.java)
929990
}
930991
}
931992

@@ -935,7 +996,6 @@ class SentryAutoConfigurationTest {
935996
contextRunner
936997
.withPropertyValues(
937998
"sentry.dsn=http://key@localhost/proj",
938-
"sentry.traces-sample-rate=1.0",
939999
"sentry.profile-session-sample-rate=1.0",
9401000
"debug=true",
9411001
)
@@ -1066,6 +1126,17 @@ class SentryAutoConfigurationTest {
10661126
@Bean open fun sentryOptionsConfiguration() = Sentry.OptionsConfiguration<SentryOptions> {}
10671127
}
10681128

1129+
@Configuration(proxyBeanMethods = false)
1130+
open class CustomProfilerOptionsConfigurationConfiguration {
1131+
private val profiler = mock<IContinuousProfiler>()
1132+
1133+
@Bean
1134+
open fun customOptionsConfiguration() =
1135+
Sentry.OptionsConfiguration<SentryOptions> { it.setContinuousProfiler(profiler) }
1136+
1137+
@Bean open fun beforeSendCallback() = CustomBeforeSendCallback()
1138+
}
1139+
10691140
@Configuration(proxyBeanMethods = false)
10701141
open class MockTransportConfiguration {
10711142

0 commit comments

Comments
 (0)