Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/pack200/DefaultTimeZoneTest.java
38833 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*/2223import java.io.File;24import java.io.FileOutputStream;25import java.io.IOException;26import java.io.OutputStream;27import java.util.TimeZone;28import java.util.concurrent.CountDownLatch;29import java.util.jar.JarFile;30import java.util.jar.JarOutputStream;31import java.util.jar.Pack200;3233/*34* @test35* @bug 806698536* @summary multithreading packing/unpacking files can result in Timezone set to UTC37* @compile -XDignore.symbol.file Utils.java DefaultTimeZoneTest.java38* @run main/othervm DefaultTimeZoneTest39* @author mcherkas40*/414243public class DefaultTimeZoneTest {4445private static final TimeZone tz = TimeZone.getTimeZone("Europe/Moscow");46private static final int INIT_THREAD_COUNT = Math.min(4, Runtime.getRuntime().availableProcessors());47private static final int MAX_THREAD_COUNT = 4 * INIT_THREAD_COUNT;48private static final long MINUTE = 60 * 1000;4950private static class NoOpOutputStream extends OutputStream {51@Override52public void write(int b) throws IOException {53// no op54}55}5657static class PackAction implements Runnable {58private Pack200.Packer packer = Pack200.newPacker();59private JarFile jarFile;6061PackAction() throws IOException {62jarFile = new JarFile(new File("golden.jar"));63}6465@Override66public void run() {67try {68packer.pack(jarFile, new NoOpOutputStream());69} catch (IOException e) {70throw new RuntimeException(e);71}72}73}7475static class UnpackAction implements Runnable {76private Pack200.Unpacker unpacker = Pack200.newUnpacker();77private JarOutputStream jos;78private File packedJar = new File("golden.pack");7980UnpackAction() throws IOException {81jos = new JarOutputStream(new NoOpOutputStream());82}8384@Override85public void run() {86try {87unpacker.unpack(packedJar, jos);88} catch (IOException e) {89throw new RuntimeException(e);90} finally {91try {92jos.close();93} catch (IOException e) {94throw new RuntimeException(e);95}96}97}98};99100public static void test(final Class<? extends Runnable> runnableClass) throws InterruptedException {101for (int i = INIT_THREAD_COUNT; i <= MAX_THREAD_COUNT; i*=2) {102final CountDownLatch startLatch = new CountDownLatch(i);103final CountDownLatch doneLatch = new CountDownLatch(i);104for (int j = 0; j < i; j++) {105new Thread() {106@Override107public void run() {108try {109Runnable r = runnableClass.newInstance();110startLatch.countDown();111startLatch.await();112r.run();113} catch (Exception e) {114throw new RuntimeException(e);115} finally {116doneLatch.countDown();117}118}119}.start();120}121doneLatch.await();122123if(!TimeZone.getDefault().equals(tz)) {124throw new RuntimeException("FAIL: default time zone was changed");125}126}127}128129public static void main(String args[]) throws IOException, InterruptedException {130TimeZone.setDefault(tz);131132// make a local copy of our test file133File srcFile = Utils.locateJar("golden.jar");134final File goldenFile = new File("golden.jar");135Utils.copyFile(srcFile, goldenFile);136137// created packed file138final JarFile goldenJarFile = new JarFile(goldenFile);139final File packFile = new File("golden.pack");140Utils.pack(goldenJarFile, packFile);141142// before test let's unpack golden pack to warm up143// a native unpacker. That allow us to avoid JDK-8080438144UnpackAction unpackAction = new UnpackAction();145unpackAction.run();146147long startTime = System.currentTimeMillis();148while(System.currentTimeMillis() - startTime < MINUTE) {149// test packer150test(PackAction.class);151152// test unpacker153test(UnpackAction.class);154}155156Utils.cleanup();157}158}159160161