src/test/java/org/jsoup/integration/SessionIT.java
// test that we throw a nice clear exception if you try to multi-thread by forget .newRequest()
test that we throw a nice clear exception if you try to multi-thread by forget .newRequest()
test that we throw a nice clear exception if you try to multi - thread by forget . newRequest ()
@Test
    public void multiThreadWithoutNewRequestBlowsUp() throws InterruptedException {
        int numThreads = 20;
        String url = SlowRider.Url + "?" + SlowRider.MaxTimeParam + "=10000"; 
        String title = "Slow Rider";
        ThreadCatcher catcher = new ThreadCatcher();
        Connection session = Jsoup.newSession();
        Thread[] threads = new Thread[numThreads];
        for (int threadNum = 0; threadNum < numThreads; threadNum++) {
            Thread thread = new Thread(() -> {
                try {
                    Document doc = session.url(url).get();
                    assertEquals(title, doc.title());
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            });
            thread.setName("Runner-" + threadNum);
            thread.start();
            thread.setUncaughtExceptionHandler(catcher);
            threads[threadNum] = thread;
        }
        for (Thread thread : threads) {
            thread.join();
        }
        assertEquals(numThreads - 1, catcher.multiThreadExceptions.get());
        assertEquals(numThreads - 1, catcher.exceptionCount.get());
    }
@ Test public void multiThreadWithoutNewRequestBlowsUp ( ) throws InterruptedException { int numThreads = 20 ; String url = SlowRider . Url + "?" + SlowRider . MaxTimeParam + "=10000" ; String title = "Slow Rider" ; ThreadCatcher catcher = new ThreadCatcher ( ) ; Connection session = Jsoup . newSession ( ) ; Thread [ ] threads = new Thread [ numThreads ] ; for ( int threadNum = 0 ; threadNum < numThreads ; threadNum ++ ) { Thread thread = new Thread ( ( ) -> { try { Document doc = session . url ( url ) . get ( ) ; assertEquals ( title , doc . title ( ) ) ; } catch ( IOException e ) { throw new UncheckedIOException ( e ) ; } } ) ; thread . setName ( "Runner-" + threadNum ) ; thread . start ( ) ; thread . setUncaughtExceptionHandler ( catcher ) ; threads [ threadNum ] = thread ; } for ( Thread thread : threads ) { thread . join ( ) ; } assertEquals ( numThreads - 1 , catcher . multiThreadExceptions . get ( ) ) ; assertEquals ( numThreads - 1 , catcher . exceptionCount . get ( ) ) ; }
test that we throw a nice clear exception if you try to multi-thread by forget .newRequest()
test that we throw a nice clear exception if you try to multi - thread by forget . newRequest ()
// this makes sure that the first req is still executing whilst the others run // now join them all // only one should have passed, rest should have blown up (assuming the started whilst other was running)