Skip to content

Commit b163ebd

Browse files
committed
add tests
1 parent 7beceef commit b163ebd

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/test/java/rx/internal/operators/OperatorGroupByTest.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,8 +1875,8 @@ public String call(Integer x) {
18751875
assertEquals(expected, ts.getOnNextEvents());
18761876
}
18771877

1878+
private static final Func1<Integer, Integer> EVICTING_MAP_ELEMENT_SELECTOR = UtilityFunctions.identity();
18781879

1879-
Func1<Integer, Integer> elementSelector = UtilityFunctions.identity();
18801880
private static final Func1<Integer, Integer> EVICTING_MAP_KEY_SELECTOR = new Func1<Integer, Integer> (){
18811881
@Override
18821882
public Integer call(Integer t) {
@@ -1885,38 +1885,30 @@ public Integer call(Integer t) {
18851885

18861886
@Test
18871887
public void testEvictingMapFactoryIfMapPutThrowsRuntimeExceptionThenErrorEmittedByStream() {
1888-
Func1<Integer, Integer> elementSelector = UtilityFunctions.identity();
1889-
Exceptions.throwIfFatal(null);
18901888
final RuntimeException exception = new RuntimeException("boo");
1891-
//normally we would use Guava CacheBuilder for instance but to save a test dependency
1892-
//we make something custom
18931889
Func1<Action1<Integer>, Map<Integer, Object>> mapFactory = createMapFactoryThatThrowsOnPut(exception);
18941890
TestSubscriber<Object> ts = TestSubscriber.create();
18951891
Observable
18961892
.range(1, 100)
1897-
.groupBy(EVICTING_MAP_KEY_SELECTOR, elementSelector, mapFactory)
1893+
.groupBy(EVICTING_MAP_KEY_SELECTOR, EVICTING_MAP_ELEMENT_SELECTOR, mapFactory)
18981894
.flatMap(UtilityFunctions.<Observable<Integer>>identity())
18991895
.subscribe(ts);
19001896
ts.assertError(exception);
19011897
}
19021898

19031899
@Test(expected = OnErrorNotImplementedException.class)
1904-
public void testEvictingMapFactoryIfMapPutThrowsOnErrorNotImplementedExceptionThenErrorEmittedByStream() {
1905-
1906-
Exceptions.throwIfFatal(null);
1900+
public void testEvictingMapFactoryIfMapPutThrowsFatalErrorThenErrorThrownBySubscribe() {
19071901
final RuntimeException exception = new OnErrorNotImplementedException("boo", new RuntimeException());
1908-
//normally we would use Guava CacheBuilder for instance but to save a test dependency
1909-
//we make something custom
19101902
Func1<Action1<Integer>, Map<Integer, Object>> mapFactory = createMapFactoryThatThrowsOnPut(exception);
19111903
TestSubscriber<Object> ts = TestSubscriber.create();
19121904
Observable
19131905
.range(1, 100)
1914-
.groupBy(EVICTING_MAP_KEY_SELECTOR, elementSelector, mapFactory)
1906+
.groupBy(EVICTING_MAP_KEY_SELECTOR, EVICTING_MAP_ELEMENT_SELECTOR, mapFactory)
19151907
.flatMap(UtilityFunctions.<Observable<Integer>>identity())
19161908
.subscribe(ts);
19171909
}
19181910

1919-
private Func1<Action1<Integer>, Map<Integer, Object>> createMapFactoryThatThrowsOnPut(
1911+
private static Func1<Action1<Integer>, Map<Integer, Object>> createMapFactoryThatThrowsOnPut(
19201912
final RuntimeException exception) {
19211913
return new Func1<Action1<Integer>, Map<Integer, Object>>() {
19221914
@SuppressWarnings("serial")
@@ -1954,11 +1946,10 @@ public void onRemoval(RemovalNotification<Integer, Object> notification) {
19541946
.build().asMap();
19551947
}
19561948
};
1957-
Func1<Integer, Integer> elementSelector = UtilityFunctions.identity();
19581949
TestSubscriber<String> ts = TestSubscriber.create();
19591950
Observable
19601951
.range(1, 100)
1961-
.groupBy(EVICTING_MAP_KEY_SELECTOR, elementSelector, mapFactory)
1952+
.groupBy(EVICTING_MAP_KEY_SELECTOR, EVICTING_MAP_ELEMENT_SELECTOR, mapFactory)
19621953
.flatMap(new Func1<GroupedObservable<Integer, Integer>, Observable<String>>() {
19631954
@Override
19641955
public Observable<String> call(final GroupedObservable<Integer, Integer> g) {
@@ -1988,6 +1979,41 @@ public String call(Integer x) {
19881979
public void testGroupByThrowsNpeIfEvictingMapFactoryNull() {
19891980
Observable
19901981
.range(1, 100)
1991-
.groupBy(EVICTING_MAP_KEY_SELECTOR, elementSelector, null);
1982+
.groupBy(EVICTING_MAP_KEY_SELECTOR, EVICTING_MAP_ELEMENT_SELECTOR, null);
1983+
}
1984+
1985+
@Test
1986+
public void testEvictingMapFactoryIfMapCreateThrowsRuntimeExceptionThenErrorEmittedByStream() {
1987+
final RuntimeException exception = new RuntimeException("boo");
1988+
Func1<Action1<Integer>, Map<Integer, Object>> mapFactory = createMapFactoryThatThrowsOnCreate(exception);
1989+
TestSubscriber<Object> ts = TestSubscriber.create();
1990+
Observable
1991+
.range(1, 100)
1992+
.groupBy(EVICTING_MAP_KEY_SELECTOR, EVICTING_MAP_ELEMENT_SELECTOR, mapFactory)
1993+
.flatMap(UtilityFunctions.<Observable<Integer>>identity())
1994+
.subscribe(ts);
1995+
ts.assertError(exception);
1996+
}
1997+
1998+
@Test(expected=OnErrorNotImplementedException.class)
1999+
public void testEvictingMapFactoryIfMapCreateThrowsFatalErrorThenSubscribeThrows() {
2000+
final OnErrorNotImplementedException exception = new OnErrorNotImplementedException("boo", new RuntimeException());
2001+
Func1<Action1<Integer>, Map<Integer, Object>> mapFactory = createMapFactoryThatThrowsOnCreate(exception);
2002+
TestSubscriber<Object> ts = TestSubscriber.create();
2003+
Observable
2004+
.range(1, 100)
2005+
.groupBy(EVICTING_MAP_KEY_SELECTOR, EVICTING_MAP_ELEMENT_SELECTOR, mapFactory)
2006+
.flatMap(UtilityFunctions.<Observable<Integer>>identity())
2007+
.subscribe(ts);
2008+
}
2009+
2010+
private static Func1<Action1<Integer>, Map<Integer, Object>> createMapFactoryThatThrowsOnCreate(
2011+
final RuntimeException exception) {
2012+
return new Func1<Action1<Integer>, Map<Integer, Object>>() {
2013+
2014+
@Override
2015+
public Map<Integer, Object> call(Action1<Integer> t) {
2016+
throw exception;
2017+
}};
19922018
}
19932019
}

0 commit comments

Comments
 (0)