1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @summary Test probing content type simultaneously from multiple threads.
  26  * @requires os.family == "linux"
  27  * @build ParallelProbes SimpleFileTypeDetector
  28  * @run main/othervm ParallelProbes 10
  29  */
  30 
  31 import java.io.IOException;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 import java.util.ArrayList;
  36 
  37 public class ParallelProbes {
  38 
  39     private static final int REPEATS = 1000;
  40 
  41     private int numThreads = 0;
  42     private ArrayList<Thread> threads;
  43 
  44     public ParallelProbes(int numThreads) {
  45         System.out.println("Using <" + numThreads + "> threads.");
  46         this.numThreads = numThreads;
  47         this.threads = new ArrayList<Thread>(numThreads);
  48     }
  49 
  50     private Path createTmpFile() throws IOException {
  51         Path dir = Paths.get(System.getProperty("test.dir", "."));
  52         final Path p = Files.createTempFile(dir, "prefix", ".json");
  53         Files.write(p, "{\"test\"}".getBytes());
  54         System.out.println("Write test file <" + p + ">");
  55         return p;
  56     }
  57 
  58     private Runnable createRunnable(final Path p) {
  59         Runnable r = new Runnable() {
  60             public void run() {
  61                 for (int i = 0; i < REPEATS; i++) {
  62                     try {
  63                         System.out.println(Thread.currentThread().getName()
  64                             + " -> " + Files.probeContentType(p));
  65                     } catch (IOException ioException) {
  66                         ioException.printStackTrace();
  67                     }
  68                 }
  69             }
  70         };
  71         return r;
  72     }
  73 
  74     public void start() throws IOException {
  75         for (int i = 0; i < numThreads; i++) {
  76             final Path p = createTmpFile();
  77             Runnable r = createRunnable(p);
  78             Thread thread = new Thread(r, "thread-" + i);
  79             thread.start();
  80             threads.add(thread);
  81         }
  82     }
  83 
  84     public void join() {
  85         for (Thread thread : threads) {
  86             try {
  87                 thread.join();
  88             } catch (InterruptedException e) {
  89                 // ignore it and proceed to the next one
  90             }
  91         }
  92     }
  93 
  94     public static void main(String[] args) throws Exception {
  95         ParallelProbes probes =
  96             new ParallelProbes(args.length < 1 ? 1 : Integer.parseInt(args[0]));
  97         probes.start();
  98         probes.join();
  99     }
 100 }