2020import static org .assertj .core .api .Assertions .assertThat ;
2121
2222import java .io .StringReader ;
23+ import java .time .Duration ;
2324import java .util .ArrayList ;
2425import java .util .List ;
2526import java .util .Map ;
2829import java .util .concurrent .CountDownLatch ;
2930import java .util .concurrent .ExecutorService ;
3031import java .util .concurrent .Executors ;
31- import java .util .concurrent .Future ;
3232import java .util .concurrent .TimeUnit ;
3333import java .util .function .Supplier ;
3434import org .assertj .core .api .Assertions ;
@@ -78,20 +78,21 @@ void nodeDoesNotTakeTooManySessions() throws Exception {
7878 ExecutorService executor = Executors .newFixedThreadPool (nThreads );
7979
8080 try {
81- List <Future <WebDriver >> pendingSessions = new ArrayList <>();
81+ List <CompletableFuture <WebDriver >> pendingSessions = new ArrayList <>();
8282 CountDownLatch allPending = new CountDownLatch (nThreads );
8383
8484 for (int i = 0 ; i < nThreads ; i ++) {
85- Future <WebDriver > future =
86- executor . submit (
85+ CompletableFuture <WebDriver > future =
86+ CompletableFuture . supplyAsync (
8787 () -> {
8888 allPending .countDown ();
8989
9090 return RemoteWebDriver .builder ()
9191 .oneOf (browser .getCapabilities ())
9292 .address (hub .getUrl ())
9393 .build ();
94- });
94+ },
95+ executor );
9596
9697 pendingSessions .add (future );
9798 }
@@ -101,21 +102,27 @@ void nodeDoesNotTakeTooManySessions() throws Exception {
101102
102103 for (int i = 0 ; i < nThreads ; i += 3 ) {
103104 // remove all completed futures
104- assertThat (pendingSessions .removeIf (Future ::isDone )).isEqualTo (i != 0 );
105+ assertThat (pendingSessions .removeIf (CompletableFuture ::isDone )).isEqualTo (i != 0 );
105106
106107 // start a node draining after 3 sessions
107108 Server <?> node = startNode (baseConfig , hub , 6 , 3 );
108109
109110 urlChecker .waitUntilAvailable (
110- 20 , TimeUnit .SECONDS , node .getUrl ().toURI ().resolve ("readyz" ).toURL ());
111+ 60 , TimeUnit .SECONDS , node .getUrl ().toURI ().resolve ("readyz" ).toURL ());
112+
113+ // use nano time to avoid issues with a jumping clock e.g. on WSL2 or due to time-sync
114+ long started = System .nanoTime ();
115+
116+ // wait for the first to start
117+ CompletableFuture .anyOf (pendingSessions .toArray (CompletableFuture <?>[]::new ))
118+ .get (120 , TimeUnit .SECONDS );
111119
112120 // we want to check not more than 3 are started, polling won't help here
113- Thread .sleep (20_000 );
114- int stopped = 0 ;
121+ Thread .sleep (Duration .ofNanos (System .nanoTime () - started ).multipliedBy (2 ).toMillis ());
115122
116- for (int j = 0 ; j < pendingSessions .size (); j ++) {
117- Future <WebDriver > future = pendingSessions .get (j );
123+ int stopped = 0 ;
118124
125+ for (CompletableFuture <WebDriver > future : pendingSessions ) {
119126 if (future .isDone ()) {
120127 stopped ++;
121128 future .get ().quit ();
@@ -127,7 +134,7 @@ void nodeDoesNotTakeTooManySessions() throws Exception {
127134
128135 // check the node stopped
129136 urlChecker .waitUntilUnavailable (
130- 10 , TimeUnit .SECONDS , node .getUrl ().toURI ().resolve ("readyz" ).toURL ());
137+ 40 , TimeUnit .SECONDS , node .getUrl ().toURI ().resolve ("readyz" ).toURL ());
131138 }
132139 } finally {
133140 executor .shutdownNow ();
0 commit comments