test/jdk/java/util/zip/ZipFile/MultiThreadedReadTest.java
changeset 55442 12e8433e2581
parent 47216 71c04702a3d5
equal deleted inserted replaced
55441:eaf0a8de3450 55442:12e8433e2581
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26  * @summary Crash in ZipFile.read() when ZipFileInputStream is shared between threads
    26  * @summary Crash in ZipFile.read() when ZipFileInputStream is shared between threads
    27  * @library /test/lib
    27  * @library /test/lib
    28  * @build jdk.test.lib.Platform
    28  * @build jdk.test.lib.Platform
    29  *        jdk.test.lib.util.FileUtils
    29  *        jdk.test.lib.util.FileUtils
    30  * @run main MultiThreadedReadTest
    30  * @run main MultiThreadedReadTest
    31  * @key randomness
       
    32  */
    31  */
    33 
    32 
       
    33 import java.io.BufferedOutputStream;
    34 import java.io.File;
    34 import java.io.File;
    35 import java.io.FileOutputStream;
    35 import java.io.FileOutputStream;
    36 import java.io.InputStream;
    36 import java.io.InputStream;
    37 import java.nio.file.Paths;
    37 import java.nio.file.Paths;
    38 import java.util.Random;
    38 import java.util.zip.CRC32;
    39 import java.util.zip.ZipEntry;
    39 import java.util.zip.ZipEntry;
    40 import java.util.zip.ZipFile;
    40 import java.util.zip.ZipFile;
    41 import java.util.zip.ZipOutputStream;
    41 import java.util.zip.ZipOutputStream;
    42 import jdk.test.lib.util.FileUtils;
    42 import jdk.test.lib.util.FileUtils;
    43 
    43 
    44 public class MultiThreadedReadTest extends Thread {
    44 public class MultiThreadedReadTest extends Thread {
    45 
    45 
    46     private static final int NUM_THREADS = 10;
    46     private static final int NUM_THREADS = 10;
    47     private static final String ZIPFILE_NAME = "large.zip";
    47     private static final String ZIPFILE_NAME =
       
    48         System.currentTimeMillis() + "-bug8038491-tmp.large.zip";
    48     private static final String ZIPENTRY_NAME = "random.txt";
    49     private static final String ZIPENTRY_NAME = "random.txt";
    49     private static InputStream is = null;
    50     private static InputStream is = null;
    50 
    51 
    51     public static void main(String args[]) throws Exception {
    52     public static void main(String args[]) throws Exception {
    52         createZipFile();
    53         createZipFile();
    61             }
    62             }
    62             for (int i = 0; i < threadArray.length; i++) {
    63             for (int i = 0; i < threadArray.length; i++) {
    63                 threadArray[i].join();
    64                 threadArray[i].join();
    64             }
    65             }
    65         } finally {
    66         } finally {
       
    67             long t = System.currentTimeMillis();
    66             FileUtils.deleteFileIfExistsWithRetry(Paths.get(ZIPFILE_NAME));
    68             FileUtils.deleteFileIfExistsWithRetry(Paths.get(ZIPFILE_NAME));
       
    69             System.out.println("Deleting zip file took:" +
       
    70                     (System.currentTimeMillis() - t) + "ms");
    67         }
    71         }
    68     }
    72     }
    69 
    73 
    70     private static void createZipFile() throws Exception {
    74     private static void createZipFile() throws Exception {
    71         try (ZipOutputStream zos =
    75         CRC32 crc32 = new CRC32();
    72             new ZipOutputStream(new FileOutputStream(ZIPFILE_NAME))) {
    76         long t = System.currentTimeMillis();
    73 
    77         File zipFile = new File(ZIPFILE_NAME);
    74             zos.putNextEntry(new ZipEntry(ZIPENTRY_NAME));
    78         try (FileOutputStream fos = new FileOutputStream(zipFile);
    75             StringBuilder sb = new StringBuilder();
    79             BufferedOutputStream bos = new BufferedOutputStream(fos);
    76             Random rnd = new Random();
    80             ZipOutputStream zos = new ZipOutputStream(bos)) {
    77             for(int i = 0; i < 1000; i++) {
    81             ZipEntry e = new ZipEntry(ZIPENTRY_NAME);
    78                 // append some random string for ZipEntry
    82             e.setMethod(ZipEntry.STORED);
    79                 sb.append(Long.toString(rnd.nextLong()));
    83             byte[] toWrite = "BLAH".repeat(10_000).getBytes();
    80             }
    84             e.setTime(t);
    81             byte[] b = sb.toString().getBytes();
    85             e.setSize(toWrite.length);
    82             zos.write(b, 0, b.length);
    86             crc32.reset();
       
    87             crc32.update(toWrite);
       
    88             e.setCrc(crc32.getValue());
       
    89             zos.putNextEntry(e);
       
    90             zos.write(toWrite);
    83         }
    91         }
    84     }
    92     }
    85 
    93 
    86     @Override
    94     @Override
    87     public void run() {
    95     public void run() {
    88         try {
    96         try {
    89             while (is.read() != -1) { }
    97             while (is.read() != -1) { }
    90         } catch (Exception e) {
    98         } catch (Exception e) {
       
    99             System.out.println("read exception:" + e);
    91             // Swallow any Exceptions (which are expected) - we're only interested in the crash
   100             // Swallow any Exceptions (which are expected) - we're only interested in the crash
    92         }
   101         }
    93     }
   102     }
    94 }
   103 }