# HG changeset patch # User bpb # Date 1438722091 25200 # Tue Aug 04 14:01:31 2015 -0700 # Node ID 0a9eb013e9f940d55287d91e42fbb29ec51c80ff # Parent 9bce83952890d248de208851c21df9e5df7d9f2b 8133010: (fs) Create test for parallel access to Files.probeContentType() Summary: Add a regression test based on the test source included in JDK-8080115. Reviewed-by: XXX diff --git a/test/java/nio/file/Files/probeContentType/ParallelProbes.java b/test/java/nio/file/Files/probeContentType/ParallelProbes.java new file mode 100644 --- /dev/null +++ b/test/java/nio/file/Files/probeContentType/ParallelProbes.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +/* @test + * @summary Test probing content type simultaneously from multiple threads. + * @requires (os.family == "linux") | (os.family == "solaris") + * @run main/othervm ParallelProbes 10 + */ +public class ParallelProbes { + + private int threads = 0; + private static final int REPEATS = 1000; + + public ParallelProbes(int threads) { + this.threads = threads; + System.out.println("Using <" + threads + "> threads."); + } + + public void start() throws IOException { + for (int i = 0; i < threads; i++) { + final Path p = createTmpFile(); + Runnable r = createRunnable(p); + new Thread(r, "thread-" + i).start(); + } + } + + private Runnable createRunnable(final Path p) { + Runnable r = new Runnable() { + public void run() { + for (int i = 0; i < REPEATS; i++) { + try { + System.out.println(Thread.currentThread().getName() + + " -> " + Files.probeContentType(p)); + } catch (IOException ioException) { + ioException.printStackTrace(); + } + } + } + }; + return r; + } + + private Path createTmpFile() throws IOException { + final Path p = Files.createTempFile("prefix", ".json"); + Files.write(p, "{\"test\"}".getBytes()); + System.out.println("Write test file <" + p + ">"); + return p; + } + + public static void main(String[] args) throws Exception { + if (args != null && args.length != 1) { + new ParallelProbes(1).start(); + } else { + new ParallelProbes(Integer.parseInt(args[0])).start(); + } + } +}