Path: blob/master/test/jdk/java/io/PushbackReader/ReadCloseRaceNPE.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 814339426* @summary Check for NullPointerException in race between read() and close().27*/28import java.io.CharArrayReader;29import java.io.IOException;30import java.io.PushbackReader;31import java.io.Reader;32import java.io.StringReader;33import java.util.ArrayList;34import java.util.List;35import java.util.concurrent.atomic.AtomicBoolean;36import java.util.concurrent.atomic.AtomicReference;37import java.util.function.Supplier;3839public class ReadCloseRaceNPE {4041private static final int BUF_SIZE = 1000;42private static final long TIMEOUT_MS = 3000;4344private static final List<Exception> failures = new ArrayList<>();4546private static void testReader(final Supplier<Reader> readerSupplier)47throws InterruptedException {48AtomicReference<Reader> readerRef =49new AtomicReference<>(readerSupplier.get());5051AtomicBoolean isFinished = new AtomicBoolean();5253Runnable readTask = () -> {54long startTime = System.currentTimeMillis();55while (System.currentTimeMillis() - startTime < TIMEOUT_MS) {56try {57readerRef.get().read();58} catch (Exception e) {59if (!(e instanceof IOException)) {60failures.add(e);61break;62}63readerRef.set(readerSupplier.get());64}65}66isFinished.set(true);67};6869Runnable closeTask = () -> {70while (!isFinished.get()) {71try {72readerRef.get().close();73} catch (Exception e) {74if (!(e instanceof IOException)) {75e.printStackTrace();76}77}78}79};8081Thread readThread = new Thread(readTask);82Thread closeThread = new Thread(closeTask);8384readThread.start();85closeThread.start();86readThread.join();87closeThread.join();88}8990public static void main(String[] args) throws Throwable {91final String s = "Two riders were approaching.\\n";9293Supplier<Reader> charPushbackReaderSupplier = () -> {94char buf[] = new char[s.length()];95s.getChars(0, s.length(), buf, 0);96CharArrayReader in = new CharArrayReader(buf);97return new PushbackReader(in, BUF_SIZE);98};99100testReader(charPushbackReaderSupplier);101102Supplier<Reader> stringPushbackReaderSupplier = () -> {103StringReader in = new StringReader(s);104return new PushbackReader(in, BUF_SIZE);105};106107testReader(stringPushbackReaderSupplier);108109if (!failures.isEmpty()) {110failures.stream().forEach((x) -> ((Exception) x).printStackTrace());111throw new RuntimeException("PushbackReaderNPE failed");112}113}114}115116117