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

Commit

Permalink
Fix concurrent modifcation exception when destroying scoops
Browse files Browse the repository at this point in the history
* Fix concurrent modifcation exception when destroying scoops

* Adding unit test
  • Loading branch information
Alan Chiu committed Apr 4, 2016
1 parent 72a054c commit 78ba41f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
7 changes: 6 additions & 1 deletion scoop/src/main/java/com/lyft/scoop/Scoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import android.view.View;
import android.view.ViewGroup;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public final class Scoop {

Expand All @@ -30,7 +32,10 @@ private Scoop(String name, Scoop parent, Map<String, Object> services) {
}

public void destroy() {
for (Map.Entry<String, Scoop> entry : this.children.entrySet()) {
final Set<Map.Entry<String, Scoop>> entries = this.children.entrySet();

final Set<Map.Entry<String, Scoop>> entriesCopy = new HashSet<>(entries);
for (Map.Entry<String, Scoop> entry : entriesCopy) {
entry.getValue().destroy();
}

Expand Down
46 changes: 45 additions & 1 deletion scoop/src/test/java/com/lyft/scoop/ScoopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class ScoopTest {

private static final Scoop TEST_SCOOP = new Scoop.Builder("root").build();

@Test
Expand Down Expand Up @@ -67,7 +68,7 @@ public void findServiceInParent() {
}

@Test
public void destroyRootScoop() {
public void destroyChildScoop() {
FooService service1 = new FooService();

Scoop rootScoop = new Scoop.Builder("root")
Expand Down Expand Up @@ -118,7 +119,50 @@ public void fromViewNoScoop() {
} catch (RuntimeException e) {
//Expected result
}
}

public void destroyRootScoop() {
FooService service1 = new FooService();

Scoop rootScoop = new Scoop.Builder("root")
.service("foo_service_1", service1)
.build();

FooService childService1 = new FooService();

Scoop childScoop1 = new Scoop.Builder("child1", rootScoop)
.service("child_service_1", childService1)
.build();

FooService childService2 = new FooService();

Scoop childScoop2 = new Scoop.Builder("child2", rootScoop)
.service("child_service_2", childService2)
.build();

FooService service3 = new FooService();

Scoop grandChildScoop = new Scoop.Builder("grand_child", childScoop1)
.service("foo_service_3", service3)
.build();

rootScoop.destroy();

Assert.assertTrue(rootScoop.isDestroyed());
Assert.assertNull(rootScoop.findChild("child"));
Assert.assertNotNull(rootScoop.findService("foo_service_1"));

Assert.assertTrue(childScoop1.isDestroyed());
Assert.assertNotNull(childScoop1.getParent());
Assert.assertNull(childScoop1.findChild("grand_child"));
Assert.assertNotNull(childScoop1.findService("foo_service_1"));
Assert.assertNotNull(childScoop1.findService("child_service_1"));

Assert.assertTrue(grandChildScoop.isDestroyed());
Assert.assertNotNull(grandChildScoop.getParent());
Assert.assertNotNull(grandChildScoop.findService("foo_service_1"));
Assert.assertNotNull(grandChildScoop.findService("child_service_1"));
Assert.assertNotNull(grandChildScoop.findService("foo_service_3"));
}

static class FooService {
Expand Down

0 comments on commit 78ba41f

Please sign in to comment.