Skip to content

Commit

Permalink
Merge pull request #326 from bugsnag/flaky-test-experiments
Browse files Browse the repository at this point in the history
Fix flaky CI tests
  • Loading branch information
fractalwrench committed Jun 18, 2018
2 parents 2dd5d83 + b7789f4 commit ba88ac4
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -33,6 +34,11 @@ public void setUp() throws Exception {
assertEquals(0, client.breadcrumbs.store.size());
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void noCallback() throws Exception {
client.leaveBreadcrumb("Hello");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,6 +33,11 @@ public void setUp() throws Exception {
breadcrumbs = new Breadcrumbs();
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void testSerialization() throws JSONException, IOException {
breadcrumbs.add(new Breadcrumb("Started app"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,6 +33,11 @@ public void setUp() throws Exception {
client = new Client(context, config);
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void testSetReleaseStage() throws Exception {
client.setReleaseStage("beta");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -32,6 +33,11 @@ public void setUp() throws Exception {
client.setErrorReportApiClient(apiClient);
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void testNotifyBlockingDefaultSeverity() {
client.notifyBlocking(new RuntimeException("Testing"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void setUp() throws Exception {
@After
public void tearDown() throws Exception {
clearSharedPrefs();
Async.cancelTasks();
}

private void clearSharedPrefs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -25,6 +26,11 @@ public void setUp() throws Exception {
client = BugsnagTestUtils.generateClient();
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void testClientNotifyModification() throws Exception {
final Collection<BeforeNotify> beforeNotifyTasks = client.config.getBeforeNotifyTasks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -29,6 +30,11 @@ public void setUp() throws Exception {
deliveryCompat = new DeliveryCompat();
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void deliverReport() throws Exception {
Report report = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -25,6 +26,11 @@ public void setUp() throws Exception {
Bugsnag.init(InstrumentationRegistry.getContext(), "123");
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test(expected = IllegalArgumentException.class)
public void testApiClientNullValidation() {
Bugsnag.setErrorReportApiClient(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,6 +33,11 @@ public void setUp() throws Exception {
Thread.setDefaultUncaughtExceptionHandler(null);
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void testEnableDisable() {
Client client = new Client(context, "api-key");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -35,6 +36,11 @@ public void setUp() throws Exception {
client = BugsnagTestUtils.generateClient();
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void testBasicSerialization() throws JSONException, IOException {
MetaData metaData = new MetaData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import android.support.test.InstrumentationRegistry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -44,6 +45,11 @@ public void postReport(String urlString,
throwable = new RuntimeException("Test");
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void testErrorDefaultMetaData() throws Exception {
Error error = new Error.Builder(config, throwable, null).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertNotEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -38,6 +39,11 @@ public void setUp() throws Exception {
user = new User();
}

@After
public void tearDown() throws Exception {
Async.cancelTasks();
}

@Test
public void startNewSession() throws Exception {
assertNotNull(sessionTracker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void setUp() throws Exception {
@After
public void tearDown() throws Exception {
callbackCount = 0;
Async.cancelTasks();
}

@Test
Expand Down
9 changes: 8 additions & 1 deletion sdk/src/main/java/com/bugsnag/android/Async.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ public Thread newThread(@NonNull Runnable runnable) {
return new Thread(runnable, "Bugsnag Thread #" + count.getAndIncrement());
}
};
private static final Executor EXECUTOR = new ThreadPoolExecutor(
private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
POOL_WORK_QUEUE, THREAD_FACTORY);

static void run(@NonNull Runnable task) throws RejectedExecutionException {
EXECUTOR.execute(task);
}

static void cancelTasks() throws InterruptedException {
Logger.info("Cancelling tasks");
EXECUTOR.shutdown();
EXECUTOR.awaitTermination(2000, TimeUnit.MILLISECONDS);
Logger.info("Finishing cancelling tasks");
}

}
21 changes: 13 additions & 8 deletions sdk/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,19 @@ public Client(@NonNull Context androidContext, @NonNull Configuration configurat

// register a receiver for automatic breadcrumbs

Async.run(new Runnable() {
@Override
public void run() {
appContext.registerReceiver(eventReceiver, EventReceiver.getIntentFilter());
appContext.registerReceiver(new ConnectivityChangeReceiver(),
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
});
try {
Async.run(new Runnable() {
@Override
public void run() {
appContext.registerReceiver(eventReceiver, EventReceiver.getIntentFilter());
appContext.registerReceiver(new ConnectivityChangeReceiver(),
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
});
} catch (RejectedExecutionException ex) {
Logger.warn("Failed to register for automatic breadcrumb broadcasts", ex);
}


config.addObserver(this);

Expand Down

0 comments on commit ba88ac4

Please sign in to comment.