package org.gbif.nameparser;

import org.apache.commons.lang3.time.StopWatch;
import org.gbif.nameparser.api.NameParser;
import org.gbif.nameparser.api.Rank;
import org.gbif.nameparser.api.UnparsableNameException;
import org.junit.Test;

import java.util.Set;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

/**
 *
 */
public class NameParserTimeoutTest {

  @Test
  public void timeoutLongNames() throws Exception {
    final int timeout = 5;
    final int extra = 10;

    NameParser parser = new NameParserGBIF(timeout);
    StopWatch watch = new StopWatch();
    // warm up parser
    try {
      parser.parse("Abies", Rank.GENUS);
    } catch (UnparsableNameException ex) {
      // too short on your machine? ignore
    }

    // this name takes 34ms on a new macbook pro
    String name = "Desmarestia ligulata subsp. muelleri (M.E.Ramirez & A.F.Peters) S.S.Y. Wong, A.H.Y. Ngan, Riggs, J.L.L. Teng, A.F.Peters, E.C.Yang, A.F.Peters, E.C.Yang, F.C.Küpper & van Reine, 2014";
    watch.start();
    try {
      parser.parse(name);
      fail("Expected to be unparsable, but only took " + watch.getTime() + "ms");

    } catch (UnparsableNameException ex) {

      final long duration = watch.getTime();
      System.out.println("Parsing took " + duration + "ms");

      // make sure no further parser threads are running. Give the JVM a few milliseconds to interrupt the task
      Thread.sleep(extra);
      Set<Thread> threads = Thread.getAllStackTraces().keySet();
      for (Thread t : threads) {
        if (t.getName().startsWith(NameParserGBIF.THREAD_NAME)) {
          System.out.println(t.getName() + "  -->  " + t.getState());
          assertFalse("Running parser thread detected", t.getState() == Thread.State.RUNNABLE);
        }
      }
    }
  }

}
@@ -473,9 +473,10 @@
 t = 
-5
+10
 ;%0A  
@@ -495,17 +495,18 @@
 extra = 
-1
+25
 0;%0A%0A    
@@ -786,10 +786,13 @@
 kes 
+1399
 3
-4
 ms o
@@ -810,16 +810,20 @@
 book pro
+ !!!
 %0A    Str
@@ -887,22 +887,255 @@
 irez
- & A.F.Peters)
+, A.F.Peters, S.S.Y. Wong, A.H.Y. Ngan, Riggs, J.L.L. Teng, A.F.Peters, E.C.Yang, A.F.Peters, E.C.Yang & F.C.K%C3%BCpper & van Reine, 2014) S.S.Y. Wong, A.H.Y. Ngan, Riggs, J.L.L. Teng, A.F.Peters, E.C.Yang, A.F.Peters, E.C.Yang, F.C.K%C3%BCpper, van Reine,
  S.S
@@ -1653,16 +1653,16 @@
 he task%0A
-
       Th
@@ -1680,16 +1680,17 @@
 extra);%0A
+%0A
       Se

Increase allowed timeout and use larger test name for real long timeouts