|
| 1 | +@file:OptIn(ExperimentalForeignApi::class) |
| 2 | + |
| 3 | +package co.touchlab.crashkios.bugsnag |
| 4 | + |
| 5 | +import co.touchlab.crashkios.bugsnag.objc.CrashKiOSBugsnagSinkProtocol |
| 6 | +import kotlin.test.Test |
| 7 | +import kotlin.test.assertEquals |
| 8 | +import kotlin.test.assertTrue |
| 9 | +import kotlinx.cinterop.ExperimentalForeignApi |
| 10 | +import platform.Foundation.NSException |
| 11 | +import platform.darwin.NSObject |
| 12 | + |
| 13 | +// Kotlin stand-in for the Swift sink: exercises the same ObjC-protocol boundary and |
| 14 | +// proves the module links with zero linker flags (the old -U / RND-91 failure mode). |
| 15 | +private class FakeBugsnagSink : |
| 16 | + NSObject(), |
| 17 | + CrashKiOSBugsnagSinkProtocol { |
| 18 | + val breadcrumbs = mutableListOf<String>() |
| 19 | + val notifications = mutableListOf<Pair<List<*>, Boolean>>() |
| 20 | + val metadata = mutableListOf<Triple<Any?, String, String>>() |
| 21 | + var fatalMarks = 0 |
| 22 | + |
| 23 | + // Call order across notify/mark — proves the hook sequences them correctly, |
| 24 | + // not just that both eventually happened. |
| 25 | + val events = mutableListOf<String>() |
| 26 | + |
| 27 | + override fun leaveBreadcrumb(message: String) { |
| 28 | + breadcrumbs += message |
| 29 | + } |
| 30 | + |
| 31 | + override fun markFatalCrashRecorded() { |
| 32 | + fatalMarks++ |
| 33 | + events += "mark" |
| 34 | + } |
| 35 | + |
| 36 | + override fun notifyWithExceptions(exceptions: List<*>, handled: Boolean) { |
| 37 | + notifications += exceptions to handled |
| 38 | + events += "notify" |
| 39 | + } |
| 40 | + |
| 41 | + override fun addMetadata(value: Any, key: String, section: String) { |
| 42 | + metadata += Triple(value, key, section) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class BugsnagSinkTest { |
| 47 | + // registerBugsnagSink() mutates the module-level `bugsnagRegistry` singleton. Safe |
| 48 | + // to call from every test that needs BugsnagKotlin's facade routed to its own sink |
| 49 | + // (sinkRef always overwrites) — NOT safe to rely on for the real unhandled-exception |
| 50 | + // hook actually firing with a given test's sink: hook-install is a once-ever guard, |
| 51 | + // permanently bound to whichever sink registered first. That's why fatalHookNotifiesThenMarksSession |
| 52 | + // below calls fatalHook() directly instead of triggering the real hook. |
| 53 | + @Test |
| 54 | + fun facadeForwardsToRegisteredSink() { |
| 55 | + val sink = FakeBugsnagSink() |
| 56 | + registerBugsnagSink(sink) |
| 57 | + |
| 58 | + BugsnagKotlin.logMessage("crumb") |
| 59 | + BugsnagKotlin.setCustomValue("section1", "key1", "value1") |
| 60 | + BugsnagKotlin.sendHandledException(RuntimeException("handled boom")) |
| 61 | + BugsnagKotlin.sendFatalException(RuntimeException("fatal boom", IllegalStateException("root cause"))) |
| 62 | + |
| 63 | + assertEquals(listOf("crumb"), sink.breadcrumbs) |
| 64 | + |
| 65 | + val (value, key, section) = sink.metadata.single() |
| 66 | + assertEquals("value1", value?.toString()) |
| 67 | + assertEquals("key1", key) |
| 68 | + assertEquals("section1", section) |
| 69 | + |
| 70 | + assertEquals(2, sink.notifications.size) |
| 71 | + val (handledExceptions, handledFlag) = sink.notifications[0] |
| 72 | + assertTrue(handledFlag) |
| 73 | + assertEquals(1, handledExceptions.size) |
| 74 | + assertEquals("kotlin.RuntimeException", (handledExceptions.single() as NSException).name) |
| 75 | + |
| 76 | + val (fatalExceptions, fatalFlag) = sink.notifications[1] |
| 77 | + assertEquals(false, fatalFlag) |
| 78 | + assertEquals(2, fatalExceptions.size, "fatal should carry the cause chain") |
| 79 | + assertEquals("kotlin.IllegalStateException", (fatalExceptions[1] as NSException).name) |
| 80 | + |
| 81 | + // Only the terminating unhandled-exception hook may mark the session — |
| 82 | + // a direct sendFatalException() must not (it would poison the OnSendError filter). |
| 83 | + assertEquals(0, sink.fatalMarks) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun fatalHookNotifiesThenMarksSession() { |
| 88 | + // fatalHook() is the terminating hook's actual body (see Bugsnag.kt), exercised |
| 89 | + // directly rather than through wrapUnhandledExceptionHook — invoking the real |
| 90 | + // hook would terminate the process. |
| 91 | + val sink = FakeBugsnagSink() |
| 92 | + // Rebinds BugsnagKotlin.implementation to this sink (register() always |
| 93 | + // overwrites sinkRef) so fatalHook's sendFatalException() call routes here |
| 94 | + // rather than to whatever sink an earlier test left registered. Does NOT wire |
| 95 | + // this sink into the real unhandled-exception hook — that binds once, to |
| 96 | + // whichever sink registers first — which is exactly why fatalHook is called |
| 97 | + // directly below instead of relying on the real hook firing. |
| 98 | + registerBugsnagSink(sink) |
| 99 | + |
| 100 | + fatalHook(sink)(RuntimeException("terminating boom")) |
| 101 | + |
| 102 | + assertEquals(1, sink.fatalMarks) |
| 103 | + assertEquals(1, sink.notifications.size) |
| 104 | + // Order matters: markFatalCrashRecorded() must run AFTER notify, never before — |
| 105 | + // marking first would let the OnSendError filter drop a report that was never sent. |
| 106 | + assertEquals(listOf("notify", "mark"), sink.events) |
| 107 | + } |
| 108 | +} |
0 commit comments