3030import com .google .common .collect .Iterators ;
3131import com .google .common .collect .Lists ;
3232import com .google .common .collect .Maps ;
33+ import com .google .common .base .Throwables ;
3334import com .google .common .base .Optional ;
3435import com .google .common .base .Charsets ;
3536import com .google .common .io .Files ;
@@ -1306,21 +1307,6 @@ public void collectUnderlyingScalaRDD() {
13061307 Assert .assertEquals (data .size (), collected .length );
13071308 }
13081309
1309- private static final class IdentityWithDelay <T > implements Function <T , T > {
1310-
1311- final int delayMillis ;
1312-
1313- IdentityWithDelay (int delayMillis ) {
1314- this .delayMillis = delayMillis ;
1315- }
1316-
1317- @ Override
1318- public T call (T x ) throws Exception {
1319- Thread .sleep (delayMillis );
1320- return x ;
1321- }
1322- }
1323-
13241310 private static final class BuggyMapFunction <T > implements Function <T , T > {
13251311
13261312 @ Override
@@ -1333,62 +1319,59 @@ public T call(T x) throws Exception {
13331319 public void collectAsync () throws Exception {
13341320 List <Integer > data = Arrays .asList (1 , 2 , 3 , 4 , 5 );
13351321 JavaRDD <Integer > rdd = sc .parallelize (data , 1 );
1336- JavaFutureAction <List <Integer >> future =
1337- rdd .map (new IdentityWithDelay <Integer >(200 )).collectAsync ();
1338- Assert .assertFalse (future .isCancelled ());
1339- Assert .assertFalse (future .isDone ());
1322+ JavaFutureAction <List <Integer >> future = rdd .collectAsync ();
13401323 List <Integer > result = future .get ();
1341- Assert .assertEquals (result , data );
1324+ Assert .assertEquals (data , result );
13421325 Assert .assertFalse (future .isCancelled ());
13431326 Assert .assertTrue (future .isDone ());
1344- Assert .assertEquals (future .jobIds ().size (), 1 );
1327+ Assert .assertEquals (1 , future .jobIds ().size ());
13451328 }
13461329
13471330 @ Test
13481331 public void foreachAsync () throws Exception {
13491332 List <Integer > data = Arrays .asList (1 , 2 , 3 , 4 , 5 );
13501333 JavaRDD <Integer > rdd = sc .parallelize (data , 1 );
1351- JavaFutureAction <Void > future = rdd .map ( new IdentityWithDelay < Integer >( 200 )). foreachAsync (
1334+ JavaFutureAction <Void > future = rdd .foreachAsync (
13521335 new VoidFunction <Integer >() {
13531336 @ Override
13541337 public void call (Integer integer ) throws Exception {
13551338 // intentionally left blank.
13561339 }
13571340 }
13581341 );
1359- Assert .assertFalse (future .isCancelled ());
1360- Assert .assertFalse (future .isDone ());
13611342 future .get ();
13621343 Assert .assertFalse (future .isCancelled ());
13631344 Assert .assertTrue (future .isDone ());
1364- Assert .assertEquals (future .jobIds ().size (), 1 );
1345+ Assert .assertEquals (1 , future .jobIds ().size ());
13651346 }
13661347
13671348 @ Test
13681349 public void countAsync () throws Exception {
13691350 List <Integer > data = Arrays .asList (1 , 2 , 3 , 4 , 5 );
13701351 JavaRDD <Integer > rdd = sc .parallelize (data , 1 );
1371- JavaFutureAction <Long > future = rdd .map (new IdentityWithDelay <Integer >(200 )).countAsync ();
1372- Assert .assertFalse (future .isCancelled ());
1373- Assert .assertFalse (future .isDone ());
1352+ JavaFutureAction <Long > future = rdd .countAsync ();
13741353 long count = future .get ();
1375- Assert .assertEquals (count , data .size ());
1354+ Assert .assertEquals (data .size (), count );
13761355 Assert .assertFalse (future .isCancelled ());
13771356 Assert .assertTrue (future .isDone ());
1378- Assert .assertEquals (future .jobIds ().size (), 1 );
1357+ Assert .assertEquals (1 , future .jobIds ().size ());
13791358 }
13801359
13811360 @ Test
13821361 public void testAsyncActionCancellation () throws Exception {
13831362 List <Integer > data = Arrays .asList (1 , 2 , 3 , 4 , 5 );
13841363 JavaRDD <Integer > rdd = sc .parallelize (data , 1 );
1385- JavaFutureAction <Long > future = rdd .map (new IdentityWithDelay <Integer >(200 )).countAsync ();
1386- Thread .sleep (200 );
1364+ JavaFutureAction <Void > future = rdd .foreachAsync (new VoidFunction <Integer >() {
1365+ @ Override
1366+ public void call (Integer integer ) throws Exception {
1367+ Thread .sleep (10000 ); // To ensure that the job won't finish before it's cancelled.
1368+ }
1369+ });
13871370 future .cancel (true );
13881371 Assert .assertTrue (future .isCancelled ());
13891372 Assert .assertTrue (future .isDone ());
13901373 try {
1391- long count = future .get (2000 , TimeUnit .MILLISECONDS );
1374+ future .get (2000 , TimeUnit .MILLISECONDS );
13921375 Assert .fail ("Expected future.get() for cancelled job to throw CancellationException" );
13931376 } catch (CancellationException ignored ) {
13941377 // pass
@@ -1400,12 +1383,11 @@ public void testAsyncActionErrorWrapping() throws Exception {
14001383 List <Integer > data = Arrays .asList (1 , 2 , 3 , 4 , 5 );
14011384 JavaRDD <Integer > rdd = sc .parallelize (data , 1 );
14021385 JavaFutureAction <Long > future = rdd .map (new BuggyMapFunction <Integer >()).countAsync ();
1403- Thread .sleep (200 );
14041386 try {
1405- long count = future .get (2000 , TimeUnit .MILLISECONDS );
1387+ long count = future .get (2 , TimeUnit .SECONDS );
14061388 Assert .fail ("Expected future.get() for failed job to throw ExcecutionException" );
1407- } catch (ExecutionException ignored ) {
1408- // pass
1389+ } catch (ExecutionException ee ) {
1390+ Assert . assertTrue ( Throwables . getStackTraceAsString ( ee ). contains ( "Custom exception!" ));
14091391 }
14101392 Assert .assertTrue (future .isDone ());
14111393 }
0 commit comments