@@ -322,7 +322,7 @@ public void testParserCancellation() {
322322 final var parseFuture = executor .schedule (() -> {
323323
324324 // cancel the parsing after 200ms
325- executor .schedule (() -> assertThat (parser .requestCancellation ()).isTrue (), 200 ,
325+ executor .schedule (() -> assertThat (parser .requestCancellationAsync ()).isTrue (), 200 ,
326326 TimeUnit .MILLISECONDS );
327327
328328 // parsing the View.java.txt file takes 300-600ms
@@ -336,6 +336,8 @@ public void testParserCancellation() {
336336 parseFuture .get ();
337337 } catch (InterruptedException | ExecutionException e ) {
338338 throw new RuntimeException (e );
339+ } finally {
340+ executor .shutdownNow ();
339341 }
340342 }
341343 }
@@ -385,6 +387,8 @@ public void testParserParseCallShouldFailIfAnotherParseIsInProgress() {
385387 parseFuture2 .get ();
386388 } catch (Throwable e ) {
387389 throw new RuntimeException (e );
390+ } finally {
391+ executor .shutdownNow ();
388392 }
389393 }
390394 }
@@ -419,7 +423,7 @@ public void testParserParseCallShouldSucceedIfAnotherParseIsInProgressAndCancell
419423 assertThat (parser .isParsing ()).isTrue ();
420424
421425 // request the cancellation
422- assertThat (parser .requestCancellation ()).isTrue ();
426+ assertThat (parser .requestCancellationAsync ()).isTrue ();
423427
424428 // the next parse call should wait for the previous parse call to return
425429 try (var tree = parser .parseString (fileContent )) {
@@ -436,6 +440,8 @@ public void testParserParseCallShouldSucceedIfAnotherParseIsInProgressAndCancell
436440 parseFuture2 .get ();
437441 } catch (Throwable e ) {
438442 throw new RuntimeException (e );
443+ } finally {
444+ executor .shutdownNow ();
439445 }
440446 }
441447 }
@@ -488,6 +494,63 @@ public void testParserParseCallShouldFailIfAnotherParseIsInProgressAndCancellati
488494 parseFuture2 .get ();
489495 } catch (Throwable e ) {
490496 throw new RuntimeException (e );
497+ } finally {
498+ executor .shutdownNow ();
499+ }
500+ }
501+ }
502+
503+ @ Test
504+ public void testAwaitedCancellation () {
505+ try (final var parser = TSParser .create (); final var mainParseContent = UTF16StringFactory .newString ()) {
506+ parser .setLanguage (TSLanguageJava .getInstance ());
507+
508+ // Read the content before starting the threads
509+ final var fileContent = readResource ("View.java.txt" );
510+ mainParseContent .append (fileContent );
511+ mainParseContent .append (fileContent );
512+ mainParseContent .append (fileContent );
513+
514+ final var executor = Executors .newScheduledThreadPool (20 );
515+
516+ // start the main parse operation immediately
517+ final var parseFuture1 = executor .schedule (() -> {
518+ try (final var tree = parser .parseString (mainParseContent )) {
519+ // This parse was cancelled and another parse was requested
520+ // so this should fail
521+ assertThat (tree ).isNull ();
522+ }
523+ }, 0 , TimeUnit .MICROSECONDS );
524+
525+ // delay the second parse so that the parser is in the 'parsing' state when this is executed
526+ final var secondParseDelayMs = 100 ;
527+ final var parseFuture2 = executor .schedule (() -> {
528+
529+ // the parser should be in the 'parsing' state by now
530+ assertThat (parser .isParsing ()).isTrue ();
531+
532+ // request parse cancellation and wait till the parse returns
533+ final var start = System .currentTimeMillis ();
534+ parser .requestCancellationAndWait ();
535+ System .err .println ("cancelAndWait() waited for " + (System .currentTimeMillis () - start ) + "ms" );
536+
537+ // request another parse
538+ try (var tree = parser .parseString (fileContent )) {
539+ // A parse was already in progress when this parse was requested
540+ // however, we cancelled that parse and requested this one
541+ // so this parseString call should succeed
542+ assertThat (tree ).isNotNull ();
543+ assertThat (tree .canAccess ()).isTrue ();
544+ }
545+ }, secondParseDelayMs , TimeUnit .MILLISECONDS );
546+
547+ try {
548+ parseFuture1 .get ();
549+ parseFuture2 .get ();
550+ } catch (Throwable e ) {
551+ throw new RuntimeException (e );
552+ } finally {
553+ executor .shutdownNow ();
491554 }
492555 }
493556 }
0 commit comments