-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a threadlocal to hold the NodeIterator
Ensures that an Evaluator query can be multi-threaded Fixes #2088
- Loading branch information
Showing
4 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.jsoup.select; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class SelectorIT { | ||
|
||
@Test | ||
public void multiThreadHas() throws InterruptedException { | ||
final String html = "<div id=1></div><div id=2><p>One</p><p>Two</p>"; | ||
final Evaluator eval = QueryParser.parse("div:has(p)"); | ||
|
||
int numThreads = 20; | ||
int numThreadLoops = 5; | ||
|
||
SelectorIT.ThreadCatcher catcher = new SelectorIT.ThreadCatcher(); | ||
|
||
Thread[] threads = new Thread[numThreads]; | ||
for (int threadNum = 0; threadNum < numThreads; threadNum++) { | ||
Thread thread = new Thread(() -> { | ||
Document doc = Jsoup.parse(html); | ||
for (int loop = 0; loop < numThreadLoops; loop++) { | ||
Elements els = doc.select(eval); | ||
assertEquals(1, els.size()); | ||
assertEquals("2", els.get(0).id()); | ||
} | ||
}); | ||
thread.setName("Runner-" + threadNum); | ||
thread.start(); | ||
thread.setUncaughtExceptionHandler(catcher); | ||
threads[threadNum] = thread; | ||
} | ||
|
||
// now join them all | ||
for (Thread thread : threads) { | ||
thread.join(); | ||
} | ||
|
||
assertEquals(0, catcher.exceptionCount.get()); | ||
} | ||
|
||
static class ThreadCatcher implements Thread.UncaughtExceptionHandler { | ||
AtomicInteger exceptionCount = new AtomicInteger(); | ||
|
||
@Override | ||
public void uncaughtException(Thread t, Throwable e) { | ||
|
||
e.printStackTrace(); | ||
exceptionCount.incrementAndGet(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters