26
26
import org .junit .*;
27
27
import org .mockito .*;
28
28
29
+ import com .google .common .cache .CacheBuilder ;
30
+ import com .google .common .cache .RemovalListener ;
31
+ import com .google .common .cache .RemovalNotification ;
32
+
29
33
import rx .*;
30
34
import rx .Observable ;
31
35
import rx .Observable .OnSubscribe ;
32
36
import rx .Observer ;
33
37
import rx .exceptions .Exceptions ;
38
+ import rx .exceptions .OnErrorNotImplementedException ;
34
39
import rx .exceptions .TestException ;
35
40
import rx .functions .*;
36
41
import rx .internal .util .*;
@@ -1858,29 +1863,62 @@ public String call(Integer x) {
1858
1863
})
1859
1864
.subscribe (ts );
1860
1865
assertEquals (Arrays .asList (4 , 5 , 6 , 7 , 8 , 9 ), evictedKeys );
1861
- List <String > expected = Observable .range (1 , 100 ).map (new Func1 <Integer , String >() {
1862
- @ Override
1863
- public String call (Integer x ) {
1864
- return (x /10 ) + ":" + x ;
1865
- }
1866
- }).toList ().toBlocking ().single ();
1866
+ List <String > expected = Observable
1867
+ .range (1 , 100 )
1868
+ .map (new Func1 <Integer , String >() {
1869
+ @ Override
1870
+ public String call (Integer x ) {
1871
+ return (x /10 ) + ":" + x ;
1872
+ }
1873
+ })
1874
+ .toList ().toBlocking ().single ();
1867
1875
assertEquals (expected , ts .getOnNextEvents ());
1868
1876
}
1869
1877
1878
+
1879
+ Func1 <Integer , Integer > elementSelector = UtilityFunctions .identity ();
1870
1880
private static final Func1 <Integer , Integer > EVICTING_MAP_KEY_SELECTOR = new Func1 <Integer , Integer > (){
1871
1881
@ Override
1872
1882
public Integer call (Integer t ) {
1873
1883
return t /10 ;
1874
1884
}};
1875
1885
1876
1886
@ Test
1877
- public void testEvictingMapFactoryIfMapPutThrowsThen () {
1887
+ public void testEvictingMapFactoryIfMapPutThrowsRuntimeExceptionThenErrorEmittedByStream () {
1878
1888
Func1 <Integer , Integer > elementSelector = UtilityFunctions .identity ();
1879
1889
Exceptions .throwIfFatal (null );
1880
1890
final RuntimeException exception = new RuntimeException ("boo" );
1881
1891
//normally we would use Guava CacheBuilder for instance but to save a test dependency
1882
1892
//we make something custom
1883
- Func1 <Action1 <Integer >, Map <Integer , Object >> mapFactory = new Func1 <Action1 <Integer >, Map <Integer , Object >>() {
1893
+ Func1 <Action1 <Integer >, Map <Integer , Object >> mapFactory = createMapFactoryThatThrowsOnPut (exception );
1894
+ TestSubscriber <Object > ts = TestSubscriber .create ();
1895
+ Observable
1896
+ .range (1 , 100 )
1897
+ .groupBy (EVICTING_MAP_KEY_SELECTOR , elementSelector , mapFactory )
1898
+ .flatMap (UtilityFunctions .<Observable <Integer >>identity ())
1899
+ .subscribe (ts );
1900
+ ts .assertError (exception );
1901
+ }
1902
+
1903
+ @ Test (expected = OnErrorNotImplementedException .class )
1904
+ public void testEvictingMapFactoryIfMapPutThrowsOnErrorNotImplementedExceptionThenErrorEmittedByStream () {
1905
+
1906
+ Exceptions .throwIfFatal (null );
1907
+ 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
1910
+ Func1 <Action1 <Integer >, Map <Integer , Object >> mapFactory = createMapFactoryThatThrowsOnPut (exception );
1911
+ TestSubscriber <Object > ts = TestSubscriber .create ();
1912
+ Observable
1913
+ .range (1 , 100 )
1914
+ .groupBy (EVICTING_MAP_KEY_SELECTOR , elementSelector , mapFactory )
1915
+ .flatMap (UtilityFunctions .<Observable <Integer >>identity ())
1916
+ .subscribe (ts );
1917
+ }
1918
+
1919
+ private Func1 <Action1 <Integer >, Map <Integer , Object >> createMapFactoryThatThrowsOnPut (
1920
+ final RuntimeException exception ) {
1921
+ return new Func1 <Action1 <Integer >, Map <Integer , Object >>() {
1884
1922
@ SuppressWarnings ("serial" )
1885
1923
@ Override
1886
1924
public Map <Integer , Object > call (final Action1 <Integer > evicted ) {
@@ -1895,17 +1933,61 @@ public Object put(Integer key, Object value) {
1895
1933
throw exception ;
1896
1934
}};
1897
1935
}};
1898
- TestSubscriber <Object > ts = TestSubscriber .create ();
1936
+ }
1937
+
1938
+ @ Test
1939
+ public void mapFactoryEvictionWorksWithGuavaCache () {
1940
+ final List <Integer > evictedKeys = new ArrayList <Integer >();
1941
+ Func1 <Action1 <Integer >, Map <Integer , Object >> mapFactory =
1942
+ new Func1 <Action1 <Integer >, Map <Integer , Object >>() {
1943
+ @ Override
1944
+ public Map <Integer , Object > call (final Action1 <Integer > action ) {
1945
+ return CacheBuilder .newBuilder ()
1946
+ .maximumSize (5 )
1947
+ .removalListener (new RemovalListener <Integer , Object >() {
1948
+ @ Override
1949
+ public void onRemoval (RemovalNotification <Integer , Object > notification ) {
1950
+ action .call (notification .getKey ());
1951
+ evictedKeys .add (notification .getKey ());
1952
+ }
1953
+ })
1954
+ .build ().asMap ();
1955
+ }
1956
+ };
1957
+ Func1 <Integer , Integer > elementSelector = UtilityFunctions .identity ();
1958
+ TestSubscriber <String > ts = TestSubscriber .create ();
1899
1959
Observable
1900
1960
.range (1 , 100 )
1901
1961
.groupBy (EVICTING_MAP_KEY_SELECTOR , elementSelector , mapFactory )
1902
- .flatMap (UtilityFunctions .<Observable <Integer >>identity ())
1962
+ .flatMap (new Func1 <GroupedObservable <Integer , Integer >, Observable <String >>() {
1963
+ @ Override
1964
+ public Observable <String > call (final GroupedObservable <Integer , Integer > g ) {
1965
+ return g .map (new Func1 <Integer , String >() {
1966
+ @ Override
1967
+ public String call (Integer x ) {
1968
+ return g .getKey () + ":" + x ;
1969
+ }
1970
+ });
1971
+ }
1972
+ })
1903
1973
.subscribe (ts );
1904
- ts .assertError (exception );
1974
+ assertEquals (Arrays .asList (0 , 1 , 2 , 3 , 4 ), evictedKeys .subList (0 , 5 ));
1975
+ List <String > expected = Observable
1976
+ .range (1 , 100 )
1977
+ .map (new Func1 <Integer , String >() {
1978
+ @ Override
1979
+ public String call (Integer x ) {
1980
+ return (x /10 ) + ":" + x ;
1981
+ }
1982
+ })
1983
+ .toList ().toBlocking ().single ();
1984
+ assertEquals (expected , ts .getOnNextEvents ());
1905
1985
}
1906
1986
1907
- @ Test
1908
- public void mapFactoryEvictionWorksWithGuavaCache () {
1909
-
1987
+ @ Test (expected = NullPointerException .class )
1988
+ public void testGroupByThrowsNpeIfEvictingMapFactoryNull () {
1989
+ Observable
1990
+ .range (1 , 100 )
1991
+ .groupBy (EVICTING_MAP_KEY_SELECTOR , elementSelector , null );
1910
1992
}
1911
1993
}
0 commit comments