22
33import com .auth0 .jwt .algorithms .Algorithm ;
44import net .jodah .concurrentunit .Waiter ;
5- import org .junit .*;
5+ import org .junit .AfterClass ;
6+ import org .junit .BeforeClass ;
7+ import org .junit .Rule ;
8+ import org .junit .Test ;
69import org .junit .rules .ExpectedException ;
710
811import java .security .interfaces .ECKey ;
912import java .security .interfaces .RSAKey ;
10- import java .util .concurrent . ExecutorService ;
11- import java .util .concurrent . Executors ;
12- import java .util .concurrent .TimeoutException ;
13+ import java .util .Collections ;
14+ import java .util .List ;
15+ import java .util .concurrent .* ;
1316
1417import static com .auth0 .jwt .PemUtils .readPublicKeyFromFile ;
1518
1619//@Ignore("Skipping concurrency tests")
1720public class ConcurrentVerifyTest {
1821
19- private static final long TIMEOUT = 60 * 1000 * 1000 ; //1 min
20- private static final int THREAD_COUNT = 1500 ;
21- private static final int REPEAT_COUNT = 10000 ;
22+ private static final long TIMEOUT = 10 * 1000 * 1000 ; //1 min
23+ private static final int THREAD_COUNT = 100 ;
24+ private static final int REPEAT_COUNT = 2000 ;
2225 private static final String PUBLIC_KEY_FILE = "src/test/resources/rsa-public.pem" ;
2326 private static final String PUBLIC_KEY_FILE_256 = "src/test/resources/ec256-key-public.pem" ;
2427 private static final String PUBLIC_KEY_FILE_384 = "src/test/resources/ec384-key-public.pem" ;
@@ -39,25 +42,37 @@ public static void afterAll() throws Exception {
3942 }
4043
4144 @ SuppressWarnings ("Convert2Lambda" )
42- private void concurrentVerify (final JWTVerifier verifier , final String token ) throws TimeoutException {
45+ private void concurrentVerify (final JWTVerifier verifier , final String token ) throws TimeoutException , InterruptedException {
4346 final Waiter waiter = new Waiter ();
44- for (int i = 0 ; i < REPEAT_COUNT ; i ++) {
45- executor .execute (new Runnable () {
46- @ Override
47- public void run () {
48- JWT result = null ;
49- try {
50- result = verifier .verify (token );
51- } catch (Exception e ) {
52- waiter .fail (e );
53- }
54- waiter .assertNotNull (result );
55- waiter .resume ();
56- }
57- });
47+ List <VerifyTask > tasks = Collections .nCopies (REPEAT_COUNT , new VerifyTask (waiter , verifier , token ));
48+ executor .invokeAll (tasks , TIMEOUT , TimeUnit .MILLISECONDS );
49+ waiter .await (TIMEOUT , REPEAT_COUNT );
50+ }
51+
52+ private static class VerifyTask implements Callable <JWT > {
53+
54+ private final Waiter waiter ;
55+ private final JWTVerifier verifier ;
56+ private final String token ;
57+
58+ public VerifyTask (Waiter waiter , final JWTVerifier verifier , final String token ) {
59+ this .waiter = waiter ;
60+ this .verifier = verifier ;
61+ this .token = token ;
5862 }
5963
60- waiter .await (TIMEOUT , REPEAT_COUNT );
64+ @ Override
65+ public JWT call () throws Exception {
66+ JWT jwt = null ;
67+ try {
68+ jwt = verifier .verify (token );
69+ waiter .assertNotNull (jwt );
70+ } catch (Exception e ) {
71+ waiter .fail (e );
72+ }
73+ waiter .resume ();
74+ return jwt ;
75+ }
6176 }
6277
6378 @ Test
0 commit comments