Skip to content

Commit

Permalink
Merge ae74ec1 into f3753b3
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed May 8, 2018
2 parents f3753b3 + ae74ec1 commit e9b9d72
Show file tree
Hide file tree
Showing 24 changed files with 437 additions and 197 deletions.
62 changes: 41 additions & 21 deletions mazerunner/src/main/java/com/bugsnag/android/TestHarnessHooks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.bugsnag.android

import android.content.Context
import android.net.ConnectivityManager
import com.bugsnag.android.Bugsnag.client
import java.util.*

/**
* Accesses the session tracker and flushes all stored sessions
Expand All @@ -12,39 +10,61 @@ internal fun flushAllSessions() {
Bugsnag.getClient().sessionTracker.flushStoredSessions()
}

internal fun flushErrorStoreAsync(client: Client, apiClient: ErrorReportApiClient) {
client.errorStore.flushAsync(apiClient)
internal fun flushErrorStoreAsync(client: Client) {
client.errorStore.flushAsync()
}

internal fun flushErrorStoreOnLaunch(client: Client, apiClient: ErrorReportApiClient) {
client.errorStore.flushOnLaunch(apiClient)
internal fun flushErrorStoreOnLaunch(client: Client) {
client.errorStore.flushOnLaunch()
}

/**
* Creates an error API client with a 500ms delay, emulating poor network connectivity
* Creates a delivery API client with a 500ms delay, emulating poor network connectivity
*/
internal fun createSlowErrorApiClient(context: Context): ErrorReportApiClient {
internal fun createSlowDelivery(context: Context): Delivery {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
val defaultHttpClient = DefaultHttpClient(cm)

return ErrorReportApiClient({ url: String?,
report: Report?,
headers: MutableMap<String, String>? ->
Thread.sleep(500)
defaultHttpClient.postReport(url, report, headers)
})
val delivery = DefaultDelivery(cm)

return object : Delivery {
override fun deliver(payload: SessionTrackingPayload?, config: Configuration?) {
Thread.sleep(500)
delivery.deliver(payload, config)
}

override fun deliver(report: Report?, config: Configuration?) {
Thread.sleep(500)
delivery.deliver(report, config)
}
}
}

internal fun createDefaultErrorClient(context: Context): ErrorReportApiClient {
internal fun createDefaultDelivery(context: Context): DefaultDelivery {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
return DefaultHttpClient(cm)
return DefaultDelivery(cm)
}

internal fun createDefaultSessionClient(context: Context): SessionTrackingApiClient {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
return DefaultHttpClient(cm)
internal fun createCustomHeaderDelivery(context: Context): Delivery {
return object : Delivery {
val delivery: DefaultDelivery = createDefaultDelivery(context)

override fun deliver(payload: SessionTrackingPayload?, config: Configuration?) {
deliver(config?.sessionEndpoint, payload, config?.sessionApiHeaders)
}

override fun deliver(report: Report?, config: Configuration?) {
deliver(config?.endpoint, report, config?.errorApiHeaders)
}

fun deliver(endpoint: String?,
streamable: JsonStream.Streamable?,
headers: MutableMap<String, String>?) {
headers!!["Custom-Client"] = "Hello World"
delivery.deliver(endpoint, streamable, headers)
}
}
}


internal fun writeErrorToStore(client: Client) {
val error = Error.Builder(Configuration("api-key"), RuntimeException(), null).build()
client.errorStore.write(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ internal class AsyncErrorConnectivityScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
val delivery = createSlowDelivery(context)
config.delivery = delivery
super.run()
val apiClient = createSlowErrorApiClient(context)
Bugsnag.setErrorReportApiClient(apiClient)

writeErrorToStore(Bugsnag.getClient())
flushErrorStoreAsync(Bugsnag.getClient(), apiClient)
flushErrorStoreOnLaunch(Bugsnag.getClient(), apiClient)
flushErrorStoreAsync(Bugsnag.getClient())
flushErrorStoreOnLaunch(Bugsnag.getClient())
Thread.sleep(50)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ internal class AsyncErrorDoubleFlushScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
config.delivery = createSlowDelivery(context)
super.run()
val apiClient = createSlowErrorApiClient(context)
Bugsnag.setErrorReportApiClient(apiClient)

writeErrorToStore(Bugsnag.getClient())
flushErrorStoreAsync(Bugsnag.getClient(), apiClient)
flushErrorStoreAsync(Bugsnag.getClient(), apiClient)
flushErrorStoreAsync(Bugsnag.getClient())
flushErrorStoreAsync(Bugsnag.getClient())
Thread.sleep(50)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ internal class AsyncErrorLaunchScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
config.delivery = createSlowDelivery(context)
super.run()
val apiClient = createSlowErrorApiClient(context)
Bugsnag.setErrorReportApiClient(apiClient)

writeErrorToStore(Bugsnag.getClient())
flushErrorStoreOnLaunch(Bugsnag.getClient(), apiClient)
flushErrorStoreAsync(Bugsnag.getClient(), apiClient)
flushErrorStoreOnLaunch(Bugsnag.getClient())
flushErrorStoreAsync(Bugsnag.getClient())
Thread.sleep(50)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.bugsnag.android.mazerunner.scenarios
import android.content.Context
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.bugsnag.android.createDefaultErrorClient
import com.bugsnag.android.createCustomHeaderDelivery

/**
* Sends an unhandled exception which is cached on disk to Bugsnag, then sent on a separate launch,
Expand All @@ -13,14 +13,12 @@ internal class CustomClientErrorFlushScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
if ("DeliverReports" == eventMetaData) {
config.delivery = createCustomHeaderDelivery(context)
}
super.run()

if ("DeliverReports" == eventMetaData) {
Bugsnag.setErrorReportApiClient { urlString, report, headers ->
headers["Custom-Client"] = "Hello World"
createDefaultErrorClient(context).postReport(urlString, report, headers)
}
} else {
if ("DeliverReports" != eventMetaData) {
disableAllDelivery()
throw RuntimeException("ReportCacheScenario")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.bugsnag.android.mazerunner.scenarios
import android.content.Context
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.bugsnag.android.createDefaultErrorClient
import com.bugsnag.android.createCustomHeaderDelivery

/**
* Sends a handled exception to Bugsnag using a custom API client which modifies the request.
Expand All @@ -12,12 +12,8 @@ internal class CustomClientErrorScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
config.delivery = createCustomHeaderDelivery(context)
super.run()

Bugsnag.setErrorReportApiClient { urlString, report, headers ->
headers["Custom-Client"] = "Hello World"
createDefaultErrorClient(context).postReport(urlString, report, headers)
}
Bugsnag.notify(RuntimeException("Hello"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.bugsnag.android.mazerunner.scenarios
import android.content.Context
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.bugsnag.android.createDefaultSessionClient
import com.bugsnag.android.createCustomHeaderDelivery
import com.bugsnag.android.createDefaultDelivery

/**
* Sends a session which is cached on disk to Bugsnag, then sent on a separate launch,
Expand All @@ -18,12 +19,7 @@ internal class CustomClientSessionFlushScenario(config: Configuration,
if ("DeliverSessions" == eventMetaData) {
// simulate activity lifecycle callback occurring before api client can be set
Bugsnag.startSession()

Bugsnag.setSessionTrackingApiClient { urlString, report, headers ->
headers["Custom-Client"] = "Hello World"
val sessionClient = createDefaultSessionClient(context)
sessionClient.postSessionTrackingPayload(urlString, report, headers)
}
config.delivery = createCustomHeaderDelivery(context)
} else {
disableAllDelivery()
Bugsnag.startSession()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.bugsnag.android.mazerunner.scenarios
import android.content.Context
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.bugsnag.android.createDefaultSessionClient
import com.bugsnag.android.createCustomHeaderDelivery
import com.bugsnag.android.createDefaultDelivery

/**
* Sends a session using a custom API client which modifies the request.
Expand All @@ -12,14 +13,8 @@ internal class CustomClientSessionScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
config.delivery = createCustomHeaderDelivery(context)
super.run()

Bugsnag.setSessionTrackingApiClient { urlString, report, headers ->
headers["Custom-Client"] = "Hello World"
val sessionClient = createDefaultSessionClient(context)
sessionClient.postSessionTrackingPayload(urlString, report, headers)
}

Bugsnag.startSession()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ public class BreadcrumbLifecycleCrashTest {
*/
@Before
public void setUp() throws Exception {
Configuration configuration = new Configuration("api-key");
Configuration configuration = BugsnagTestUtils.generateConfiguration();
Context context = InstrumentationRegistry.getContext();
SessionStore sessionStore = new SessionStore(configuration, context);
SessionTrackingApiClient apiClient = BugsnagTestUtils.generateSessionTrackingApiClient();
sessionTracker = new SessionTracker(configuration, null, sessionStore, apiClient);
sessionTracker = new SessionTracker(configuration, null, sessionStore);
}

@Test
Expand Down
21 changes: 17 additions & 4 deletions sdk/src/androidTest/java/com/bugsnag/android/BugsnagTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ static SharedPreferences getSharedPrefs(Context context) {

static Client generateClient() {
Client client = new Client(InstrumentationRegistry.getContext(), "api-key");
client.setErrorReportApiClient(generateErrorReportApiClient());
client.setSessionTrackingApiClient(generateSessionTrackingApiClient());
client.config.setDelivery(generateDelivery());
return client;
}

Expand All @@ -52,12 +51,14 @@ static Session generateSession() {
}

static Configuration generateConfiguration() {
return new Configuration("test");
Configuration configuration = new Configuration("test");
configuration.setDelivery(generateDelivery());
return configuration;
}

static SessionTracker generateSessionTracker() {
return new SessionTracker(generateConfiguration(), BugsnagTestUtils.generateClient(),
generateSessionStore(), generateSessionTrackingApiClient());
generateSessionStore());
}

@NonNull
Expand Down Expand Up @@ -89,4 +90,16 @@ public void postReport(String urlString,
}
};
}

public static Delivery generateDelivery() {
return new Delivery() {
@Override
public void deliver(SessionTrackingPayload payload,
Configuration config) throws DeliveryFailureException {}

@Override
public void deliver(Report report,
Configuration config) throws DeliveryFailureException {}
};
}
}
23 changes: 23 additions & 0 deletions sdk/src/androidTest/java/com/bugsnag/android/ClientConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
Expand Down Expand Up @@ -86,4 +87,26 @@ public void testSetSendThreads() throws Exception {
assertFalse(config.getSendThreads());
}

@Test
public void testDefaultClientDelivery() {
assertFalse(client.config.getDelivery() instanceof DeliveryCompat);
}

@Test
public void testCustomDeliveryOverride() {
Context context = InstrumentationRegistry.getContext();
config = BugsnagTestUtils.generateConfiguration();
Delivery customDelivery = new Delivery() {
@Override
public void deliver(SessionTrackingPayload payload,
Configuration config) throws DeliveryFailureException {}

@Override
public void deliver(Report report,
Configuration config) throws DeliveryFailureException {}
};
config.setDelivery(customDelivery);
client = new Client(context, config);
assertEquals(customDelivery, client.config.getDelivery());
}
}
19 changes: 0 additions & 19 deletions sdk/src/androidTest/java/com/bugsnag/android/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,25 +219,6 @@ public void testFullManifestConfig() {
assertEquals(true, newConfig.shouldAutoCaptureSessions());
}

@Test
public void testSessionTrackerApiClient() throws Exception {
Client client = new Client(InstrumentationRegistry.getContext(), "api-key");
assertTrue(client.sessionTracker.getApiClient() instanceof DefaultHttpClient);

SessionTrackingApiClient customClient = new SessionTrackingApiClient() {
@Override
public void postSessionTrackingPayload(String urlString,
SessionTrackingPayload payload,
Map<String, String> headers)
throws NetworkException, BadResponseException {

}
};
client.setSessionTrackingApiClient(customClient);
assertFalse(client.sessionTracker.getApiClient() instanceof DefaultHttpClient);
assertEquals(customClient, client.sessionTracker.getApiClient());
}

@Test
public void testClientAddToTab() {
Client client = generateClient();
Expand Down

0 comments on commit e9b9d72

Please sign in to comment.