|
| 1 | +package io.sentry |
| 2 | + |
| 3 | +import java.util.concurrent.CompletableFuture |
| 4 | +import java.util.concurrent.Executors |
| 5 | +import kotlin.test.AfterTest |
| 6 | +import kotlin.test.BeforeTest |
| 7 | +import kotlin.test.Test |
| 8 | +import kotlin.test.assertEquals |
| 9 | + |
| 10 | +class SentryWrapperTest { |
| 11 | + |
| 12 | + private val dsn = "http://key@localhost/proj" |
| 13 | + private val executor = Executors.newSingleThreadExecutor() |
| 14 | + |
| 15 | + @BeforeTest |
| 16 | + @AfterTest |
| 17 | + fun beforeTest() { |
| 18 | + Sentry.close() |
| 19 | + SentryCrashLastRunState.getInstance().reset() |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `wrapped supply async isolates Hubs`() { |
| 24 | + val capturedEvents = mutableListOf<SentryEvent>() |
| 25 | + |
| 26 | + Sentry.init { |
| 27 | + it.dsn = dsn |
| 28 | + it.beforeSend = SentryOptions.BeforeSendCallback { event, hint -> |
| 29 | + capturedEvents.add(event) |
| 30 | + event |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + Sentry.addBreadcrumb("MyOriginalBreadcrumbBefore") |
| 35 | + Sentry.captureMessage("OriginalMessageBefore") |
| 36 | + |
| 37 | + val callableFuture = |
| 38 | + CompletableFuture.supplyAsync( |
| 39 | + SentryWrapper.wrapSupplier { |
| 40 | + Sentry.addBreadcrumb("MyClonedBreadcrumb") |
| 41 | + Sentry.captureMessage("ClonedMessage") |
| 42 | + "Result 1" |
| 43 | + }, |
| 44 | + executor |
| 45 | + ) |
| 46 | + |
| 47 | + val callableFuture2 = |
| 48 | + CompletableFuture.supplyAsync( |
| 49 | + SentryWrapper.wrapSupplier { |
| 50 | + Sentry.addBreadcrumb("MyClonedBreadcrumb2") |
| 51 | + Sentry.captureMessage("ClonedMessage2") |
| 52 | + "Result 2" |
| 53 | + }, |
| 54 | + executor |
| 55 | + ) |
| 56 | + |
| 57 | + Sentry.addBreadcrumb("MyOriginalBreadcrumb") |
| 58 | + Sentry.captureMessage("OriginalMessage") |
| 59 | + |
| 60 | + callableFuture.join() |
| 61 | + callableFuture2.join() |
| 62 | + |
| 63 | + val mainEvent = capturedEvents.firstOrNull { it.message?.formatted == "OriginalMessage" } |
| 64 | + val clonedEvent = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage" } |
| 65 | + val clonedEvent2 = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage2" } |
| 66 | + |
| 67 | + assertEquals(2, mainEvent?.breadcrumbs?.size) |
| 68 | + assertEquals(2, clonedEvent?.breadcrumbs?.size) |
| 69 | + assertEquals(2, clonedEvent2?.breadcrumbs?.size) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `wrapped callable isolates Hubs`() { |
| 74 | + val capturedEvents = mutableListOf<SentryEvent>() |
| 75 | + |
| 76 | + Sentry.init { |
| 77 | + it.dsn = dsn |
| 78 | + it.beforeSend = SentryOptions.BeforeSendCallback { event, hint -> |
| 79 | + capturedEvents.add(event) |
| 80 | + event |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + Sentry.addBreadcrumb("MyOriginalBreadcrumbBefore") |
| 85 | + Sentry.captureMessage("OriginalMessageBefore") |
| 86 | + println(Thread.currentThread().name) |
| 87 | + |
| 88 | + val future1 = executor.submit( |
| 89 | + SentryWrapper.wrapCallable { |
| 90 | + Sentry.addBreadcrumb("MyClonedBreadcrumb") |
| 91 | + Sentry.captureMessage("ClonedMessage") |
| 92 | + "Result 1" |
| 93 | + } |
| 94 | + ) |
| 95 | + |
| 96 | + val future2 = executor.submit( |
| 97 | + SentryWrapper.wrapCallable { |
| 98 | + Sentry.addBreadcrumb("MyClonedBreadcrumb2") |
| 99 | + Sentry.captureMessage("ClonedMessage2") |
| 100 | + "Result 2" |
| 101 | + } |
| 102 | + ) |
| 103 | + |
| 104 | + Sentry.addBreadcrumb("MyOriginalBreadcrumb") |
| 105 | + Sentry.captureMessage("OriginalMessage") |
| 106 | + |
| 107 | + future1.get() |
| 108 | + future2.get() |
| 109 | + |
| 110 | + val mainEvent = capturedEvents.firstOrNull { it.message?.formatted == "OriginalMessage" } |
| 111 | + val clonedEvent = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage" } |
| 112 | + val clonedEvent2 = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage2" } |
| 113 | + |
| 114 | + assertEquals(2, mainEvent?.breadcrumbs?.size) |
| 115 | + assertEquals(2, clonedEvent?.breadcrumbs?.size) |
| 116 | + assertEquals(2, clonedEvent2?.breadcrumbs?.size) |
| 117 | + } |
| 118 | +} |
0 commit comments