Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
added hub adapter (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Jan 28, 2020
1 parent f01ab69 commit 1283118
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 15 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/main/java/io/sentry/core/Hub.java
Expand Up @@ -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);
}
}

Expand Down
132 changes: 132 additions & 0 deletions 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<String> 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();
}
}
6 changes: 3 additions & 3 deletions sentry-core/src/main/java/io/sentry/core/Sentry.java
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
26 changes: 17 additions & 9 deletions sentry-core/src/test/java/io/sentry/core/HubTest.kt
Expand Up @@ -34,6 +34,7 @@ class HubTest {
@AfterTest
fun shutdown() {
Files.delete(file.toPath())
Sentry.close()
}

@Test
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -510,17 +513,22 @@ class HubTest {
@Test
fun `when integration is registered, hub is enabled`() {
val mock = mock<Integration>()
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))
}

Expand Down
Expand Up @@ -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()
}
Expand Down

0 comments on commit 1283118

Please sign in to comment.