test/jdk/java/util/logging/FileHandlerMaxLocksTest.java
author mikael
Mon, 29 Jul 2019 09:59:04 -0700
changeset 57584 9d82a35b6ff7
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39307
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
     1
/*
45286
cd809e28c082 8180887: move FileUtils to top level testlibrary
iignatyev
parents: 39307
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
39307
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
     4
 *
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
     7
 * published by the Free Software Foundation.
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
     8
 *
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    13
 * accompanied this code).
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    14
 *
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    18
 *
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    21
 * questions.
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    22
 */
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    23
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    24
/*
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    25
 * @test
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    26
 * @bug 8153955
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    27
 * @summary test the FileHandler's new property
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    28
 *  "java.util.logging.FileHandler.maxLocks" which will be present in
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    29
 *  "logging.properties" file with default value of 100. This property can be
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    30
 *  overriden by specifying this property in the custom config file.
45286
cd809e28c082 8180887: move FileUtils to top level testlibrary
iignatyev
parents: 39307
diff changeset
    31
 * @library /test/lib
45466
faf1c55d2046 8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents: 45286
diff changeset
    32
 * @build jdk.test.lib.Platform
faf1c55d2046 8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents: 45286
diff changeset
    33
 *        jdk.test.lib.util.FileUtils
39307
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    34
 * @author rpatil
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    35
 * @run main/othervm FileHandlerMaxLocksTest
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    36
 */
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    37
import java.io.File;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    38
import java.io.FileWriter;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    39
import java.io.IOException;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    40
import java.nio.file.Paths;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    41
import java.util.ArrayList;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    42
import java.util.List;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    43
import java.util.logging.FileHandler;
45286
cd809e28c082 8180887: move FileUtils to top level testlibrary
iignatyev
parents: 39307
diff changeset
    44
import jdk.test.lib.util.FileUtils;
39307
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    45
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    46
public class FileHandlerMaxLocksTest {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    47
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    48
    private static final String LOGGER_DIR = "logger-dir";
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    49
    private static final String MAX_LOCK_PROPERTY = "java.util.logging.FileHandler.maxLocks = 200";
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    50
    private static final String CONFIG_FILE_NAME = "logging.properties";
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    51
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    52
    public static void main(String[] args) throws Exception {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    53
        File loggerDir = createLoggerDir();
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    54
        String configFilePath = loggerDir.getPath() + File.separator + CONFIG_FILE_NAME;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    55
        File configFile = new File(configFilePath);
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    56
        createFile(configFile, false);
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    57
        System.setProperty("java.util.logging.config.file", configFilePath);
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    58
        List<FileHandler> fileHandlers = new ArrayList<>();
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    59
        try (FileWriter writer = new FileWriter(configFile)) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    60
            writer.write(MAX_LOCK_PROPERTY);
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    61
            writer.flush();
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    62
            // 200 raises the default limit of 100, we try 102 times
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    63
            for (int i = 0; i < 102; i++) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    64
                fileHandlers.add(new FileHandler(loggerDir.getPath() + File.separator + "test_%u.log"));
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    65
            }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    66
        } catch (IOException ie) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    67
            throw new RuntimeException("Test Failed: " + ie.getMessage());
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    68
        } finally {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    69
            for (FileHandler fh : fileHandlers) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    70
                fh.close();
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    71
            }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    72
            FileUtils.deleteFileTreeWithRetry(Paths.get(loggerDir.getPath()));
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    73
        }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    74
    }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    75
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    76
    /**
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    77
     * Create a writable directory in user directory for the test
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    78
     *
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    79
     * @return writable directory created that needs to be deleted when done
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    80
     * @throws RuntimeException
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    81
     */
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    82
    private static File createLoggerDir() throws RuntimeException {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    83
        String userDir = System.getProperty("user.dir", ".");
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    84
        File loggerDir = new File(userDir, LOGGER_DIR);
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    85
        if (!createFile(loggerDir, true)) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    86
            throw new RuntimeException("Test failed: unable to create"
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    87
                    + " writable working directory "
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    88
                    + loggerDir.getAbsolutePath());
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    89
        }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    90
        // System.out.println("Created Logger Directory: " + loggerDir.getPath());
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    91
        return loggerDir;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    92
    }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    93
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    94
    /**
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    95
     * @param newFile  File to be created
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    96
     * @param makeDirectory  is File to be created is directory
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    97
     * @return true if file already exists or creation succeeded
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    98
     */
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
    99
    private static boolean createFile(File newFile, boolean makeDirectory) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   100
        if (newFile.exists()) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   101
            return true;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   102
        }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   103
        if (makeDirectory) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   104
            return newFile.mkdir();
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   105
        } else {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   106
            try {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   107
                return newFile.createNewFile();
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   108
            } catch (IOException ie) {
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   109
                System.err.println("Not able to create file: " + newFile
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   110
                        + ", IOException: " + ie.getMessage());
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   111
                return false;
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   112
            }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   113
        }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   114
    }
35eab41d2df1 8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
rpatil
parents:
diff changeset
   115
}