Skip to content

Commit

Permalink
Allow Sentry to recover from having been called before init
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed Jun 2, 2022
1 parent 77fd2f2 commit 2d41bbb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private Sentry() {}
return mainHub;
}
IHub hub = currentHub.get();
if (hub == null) {
if (hub == null || hub instanceof NoOpHub) {
hub = mainHub.clone();
currentHub.set(hub);
}
Expand Down
26 changes: 25 additions & 1 deletion sentry/src/test/java/io/sentry/SentryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class SentryTest {

private val dsn = "http://key@localhost/proj"
Expand Down Expand Up @@ -209,6 +209,30 @@ class SentryTest {
assertFalse(File(sentryOptions?.profilingTracesDirPath!!).exists())
}

@Test
fun `using sentry before calling init creates NoOpHub but after init Sentry uses a new clone`() {
// noop as not yet initialized, caches NoOpHub in ThreadLocal
Sentry.captureMessage("noop caused")

assertTrue(Sentry.getCurrentHub() is NoOpHub)

// init Sentry in another thread
val thread = Thread() {
Sentry.init {
it.dsn = dsn
it.isDebug = true
}
}
thread.start()
thread.join()

Sentry.captureMessage("should work now")

val hub = Sentry.getCurrentHub()
assertNotNull(hub)
assertFalse(hub is NoOpHub)
}

private fun getTempPath(): String {
val tempFile = Files.createTempDirectory("cache").toFile()
tempFile.delete()
Expand Down

0 comments on commit 2d41bbb

Please sign in to comment.