-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent ConcurrentModificationException in callbacks (#266)
- Loading branch information
Showing
2 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
sdk/src/androidTest/java/com/bugsnag/android/ConcurrentCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.bugsnag.android; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.test.filters.SmallTest; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Ensures that if a callback is added or removed during iteration, a | ||
* {@link java.util.ConcurrentModificationException} is not thrown | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
@SmallTest | ||
public class ConcurrentCallbackTest { | ||
|
||
private Client client; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
client = BugsnagTestUtils.generateClient(); | ||
} | ||
|
||
@Test | ||
public void testClientNotifyModification() throws Exception { | ||
final Collection<BeforeNotify> beforeNotifyTasks = client.config.getBeforeNotifyTasks(); | ||
client.beforeNotify(new BeforeNotify() { | ||
@Override | ||
public boolean run(Error error) { | ||
beforeNotifyTasks.add(new BeforeNotifySkeleton()); | ||
// modify the Set, when iterating to the next callback this should not crash | ||
return true; | ||
} | ||
}); | ||
client.beforeNotify(new BeforeNotifySkeleton()); | ||
client.notify(new RuntimeException()); | ||
} | ||
|
||
@Test | ||
public void testClientBreadcrumbModification() throws Exception { | ||
final Collection<BeforeRecordBreadcrumb> breadcrumbTasks = client.config.getBeforeRecordBreadcrumbTasks(); | ||
|
||
client.beforeRecordBreadcrumb(new BeforeRecordBreadcrumb() { | ||
@Override | ||
public boolean shouldRecord(@NonNull Breadcrumb breadcrumb) { | ||
breadcrumbTasks.add(new BeforeRecordBreadcrumbSkeleton()); | ||
// modify the Set, when iterating to the next callback this should not crash | ||
return true; | ||
} | ||
}); | ||
client.beforeRecordBreadcrumb(new BeforeRecordBreadcrumbSkeleton()); | ||
client.leaveBreadcrumb("Whoops"); | ||
client.notify(new RuntimeException()); | ||
} | ||
|
||
static class BeforeNotifySkeleton implements BeforeNotify { | ||
@Override | ||
public boolean run(Error error) { | ||
return true; | ||
} | ||
} | ||
|
||
static class BeforeRecordBreadcrumbSkeleton implements BeforeRecordBreadcrumb { | ||
@Override | ||
public boolean shouldRecord(@NonNull Breadcrumb breadcrumb) { | ||
return true; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters