15
15
import java .util .concurrent .atomic .AtomicInteger ;
16
16
17
17
import static org .junit .jupiter .api .Assertions .assertEquals ;
18
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
18
19
19
20
/** Integration tests to test longer running Connection */
20
21
public class SessionIT {
@@ -82,11 +83,13 @@ public void multiThreadWithoutNewRequestBlowsUp() throws InterruptedException {
82
83
Connection session = Jsoup .newSession ();
83
84
84
85
Thread [] threads = new Thread [numThreads ];
86
+ AtomicInteger successful = new AtomicInteger ();
85
87
for (int threadNum = 0 ; threadNum < numThreads ; threadNum ++) {
86
88
Thread thread = new Thread (() -> {
87
89
try {
88
90
Document doc = session .url (url ).get ();
89
91
assertEquals (title , doc .title ());
92
+ successful .getAndIncrement ();
90
93
} catch (IOException e ) {
91
94
throw new UncheckedIOException (e );
92
95
}
@@ -103,8 +106,17 @@ public void multiThreadWithoutNewRequestBlowsUp() throws InterruptedException {
103
106
}
104
107
105
108
// only one should have passed, rest should have blown up (assuming the started whilst other was running)
106
- assertEquals (numThreads - 1 , catcher .multiThreadExceptions .get ());
107
- assertEquals (numThreads - 1 , catcher .exceptionCount .get ());
109
+ //assertEquals(numThreads - 1, catcher.multiThreadExceptions.get());
110
+ //assertEquals(numThreads - 1, catcher.exceptionCount.get());
111
+
112
+ /* The checks above work when all 20 threads are executed within 10 seconds. However, depending on how cloudy it
113
+ is when the CI jobs are run, they may not all complete in time. As of writing that appears most commonly on the
114
+ maxOS runners, which appear overtaxed. That makes this test flaky. So we relax the test conditions, and make
115
+ sure at least just one passed and one failed. That's OK in prod as well, because we are only concerned about
116
+ concurrent execution, which the impl does detect correctly. */
117
+ assertTrue (successful .get () > 0 );
118
+ assertTrue (catcher .multiThreadExceptions .get () > 0 );
119
+ assertEquals (catcher .multiThreadExceptions .get (), catcher .exceptionCount .get ()); // all exceptions are multi-threaded
108
120
}
109
121
110
122
@ Test
0 commit comments