diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt index 54b554ee8..68872c4ec 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/EnvelopeFileObserverIntegrationTest.kt @@ -4,6 +4,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.verify import io.sentry.core.Hub +import io.sentry.core.HubAdapter import io.sentry.core.SentryOptions import java.io.File import java.nio.file.Files @@ -45,8 +46,9 @@ class EnvelopeFileObserverIntegrationTest { options.cacheDirPath = file.absolutePath options.addIntegration(integrationMock) options.setSerializer(mock()) + val expected = HubAdapter.getInstance() val hub = Hub(options) - verify(integrationMock).register(hub, options) + verify(integrationMock).register(expected, options) hub.close() verify(integrationMock).close() } diff --git a/sentry-core/src/main/java/io/sentry/core/Hub.java b/sentry-core/src/main/java/io/sentry/core/Hub.java index 5a08c86ca..d57bd8079 100644 --- a/sentry-core/src/main/java/io/sentry/core/Hub.java +++ b/sentry-core/src/main/java/io/sentry/core/Hub.java @@ -32,7 +32,7 @@ public Hub(@NotNull SentryOptions options) { // Register integrations against a root Hub for (Integration integration : options.getIntegrations()) { - integration.register(this, options); + integration.register(HubAdapter.getInstance(), options); } } diff --git a/sentry-core/src/main/java/io/sentry/core/HubAdapter.java b/sentry-core/src/main/java/io/sentry/core/HubAdapter.java new file mode 100644 index 000000000..942f0fea2 --- /dev/null +++ b/sentry-core/src/main/java/io/sentry/core/HubAdapter.java @@ -0,0 +1,132 @@ +package io.sentry.core; + +import io.sentry.core.protocol.SentryId; +import io.sentry.core.protocol.User; +import java.util.List; +import org.jetbrains.annotations.Nullable; + +public final class HubAdapter implements IHub { + + private static final HubAdapter INSTANCE = new HubAdapter(); + + private HubAdapter() {} + + public static HubAdapter getInstance() { + return INSTANCE; + } + + @Override + public boolean isEnabled() { + return Sentry.isEnabled(); + } + + @Override + public SentryId captureEvent(SentryEvent event, @Nullable Object hint) { + return Sentry.captureEvent(event, hint); + } + + @Override + public SentryId captureMessage(String message, SentryLevel level) { + return Sentry.captureMessage(message, level); + } + + @Override + public SentryId captureException(Throwable throwable, @Nullable Object hint) { + return Sentry.captureException(throwable, hint); + } + + @Override + public void close() { + Sentry.close(); + } + + @Override + public void addBreadcrumb(Breadcrumb breadcrumb, @Nullable Object hint) { + Sentry.addBreadcrumb(breadcrumb, hint); + } + + @Override + public void setLevel(SentryLevel level) { + Sentry.setLevel(level); + } + + @Override + public void setTransaction(String transaction) { + Sentry.setTransaction(transaction); + } + + @Override + public void setUser(User user) { + Sentry.setUser(user); + } + + @Override + public void setFingerprint(List fingerprint) { + Sentry.setFingerprint(fingerprint); + } + + @Override + public void clearBreadcrumbs() { + Sentry.clearBreadcrumbs(); + } + + @Override + public void setTag(String key, String value) { + Sentry.setTag(key, value); + } + + @Override + public void removeTag(String key) { + Sentry.removeTag(key); + } + + @Override + public void setExtra(String key, String value) { + Sentry.setExtra(key, value); + } + + @Override + public void removeExtra(String key) { + Sentry.removeExtra(key); + } + + @Override + public SentryId getLastEventId() { + return Sentry.getLastEventId(); + } + + @Override + public void pushScope() { + Sentry.pushScope(); + } + + @Override + public void popScope() { + Sentry.popScope(); + } + + @Override + public void withScope(ScopeCallback callback) { + Sentry.withScope(callback); + } + + @Override + public void configureScope(ScopeCallback callback) { + Sentry.configureScope(callback); + } + + @Override + public void bindClient(ISentryClient client) { + Sentry.bindClient(client); + } + + @Override + public void flush(long timeoutMills) { + Sentry.flush(timeoutMills); + } + + @Override + public IHub clone() { + return Sentry.getCurrentHub().clone(); + } +} diff --git a/sentry-core/src/main/java/io/sentry/core/Sentry.java b/sentry-core/src/main/java/io/sentry/core/Sentry.java index ef0a32e6c..03b0cb3b5 100644 --- a/sentry-core/src/main/java/io/sentry/core/Sentry.java +++ b/sentry-core/src/main/java/io/sentry/core/Sentry.java @@ -21,7 +21,7 @@ private Sentry() {} * * @return the hub */ - private static @NotNull IHub getCurrentHub() { + static @NotNull IHub getCurrentHub() { IHub hub = currentHub.get(); if (hub == null) { currentHub.set(mainHub.clone()); @@ -280,7 +280,7 @@ public static void setExtra(@NotNull String key, @NotNull String value) { * * @param key the key */ - public void removeExtra(@NotNull String key) { + public static void removeExtra(@NotNull String key) { getCurrentHub().removeExtra(key); } @@ -335,7 +335,7 @@ public static void bindClient(@NotNull ISentryClient client) { * * @param timeoutMills time in milliseconds */ - public static void flush(int timeoutMills) { + public static void flush(long timeoutMills) { getCurrentHub().flush(timeoutMills); } diff --git a/sentry-core/src/test/java/io/sentry/core/HubTest.kt b/sentry-core/src/test/java/io/sentry/core/HubTest.kt index 10e049e63..6a7ebb834 100644 --- a/sentry-core/src/test/java/io/sentry/core/HubTest.kt +++ b/sentry-core/src/test/java/io/sentry/core/HubTest.kt @@ -34,6 +34,7 @@ class HubTest { @AfterTest fun shutdown() { Files.delete(file.toPath()) + Sentry.close() } @Test @@ -50,7 +51,8 @@ class HubTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) options.addIntegration(integrationMock) - val expected = Hub(options) + val expected = HubAdapter.getInstance() + Hub(options) verify(integrationMock).register(expected, options) } @@ -62,9 +64,10 @@ class HubTest { options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) options.addIntegration(integrationMock) - val expected = Hub(options) + val expected = HubAdapter.getInstance() + val hub = Hub(options) verify(integrationMock).register(expected, options) - expected.clone() + hub.clone() verifyNoMoreInteractions(integrationMock) } @@ -510,17 +513,22 @@ class HubTest { @Test fun `when integration is registered, hub is enabled`() { val mock = mock() - val options = SentryOptions().apply { - addIntegration(mock) - dsn = "https://key@sentry.io/proj" - cacheDirPath = file.absolutePath - setSerializer(mock()) + + var options: SentryOptions? = null + // init main hub and make it enabled + Sentry.init { + it.addIntegration(mock) + it.dsn = "https://key@sentry.io/proj" + it.cacheDirPath = file.absolutePath + it.setSerializer(mock()) + options = it } + doAnswer { val hub = it.arguments[0] as IHub assertTrue(hub.isEnabled) }.whenever(mock).register(any(), eq(options)) - Hub(options) + verify(mock).register(any(), eq(options)) } diff --git a/sentry-core/src/test/java/io/sentry/core/UncaughtExceptionHandlerIntegrationTest.kt b/sentry-core/src/test/java/io/sentry/core/UncaughtExceptionHandlerIntegrationTest.kt index 0883b7597..82af5de93 100644 --- a/sentry-core/src/test/java/io/sentry/core/UncaughtExceptionHandlerIntegrationTest.kt +++ b/sentry-core/src/test/java/io/sentry/core/UncaughtExceptionHandlerIntegrationTest.kt @@ -92,8 +92,9 @@ class UncaughtExceptionHandlerIntegrationTest { options.addIntegration(integrationMock) options.cacheDirPath = file.absolutePath options.setSerializer(mock()) + val expected = HubAdapter.getInstance() val hub = Hub(options) - verify(integrationMock).register(hub, options) + verify(integrationMock).register(expected, options) hub.close() verify(integrationMock).close() }