test/jdk/java/util/logging/LoggerResourceBundleRace.java
changeset 47216 71c04702a3d5
parent 32649 2ee9017c7597
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2011, 2013, 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 /*
       
    25  * @test
       
    26  * @bug     7045594 8002070
       
    27  * @summary ResourceBundle setting race in Logger.getLogger(name, rbName)
       
    28  * @author  Daniel D. Daugherty
       
    29  * @build RacingThreadsTest LoggerResourceBundleRace
       
    30  * @run main/othervm LoggerResourceBundleRace
       
    31  *
       
    32  * (In samevm mode, the bundle classes don't end up in the classpath.)
       
    33  */
       
    34 import java.util.ListResourceBundle;
       
    35 import java.util.MissingResourceException;
       
    36 import java.util.concurrent.atomic.AtomicInteger;
       
    37 import java.util.logging.Logger;
       
    38 
       
    39 
       
    40 public class LoggerResourceBundleRace extends RacingThreadsTest {
       
    41     private static final int N_LOOPS   = 500000;   // # of race loops
       
    42     private static final int N_SECS    = 15;       // # of secs to run test
       
    43     // # of parallel threads; must match number of MyResources inner classes
       
    44     private static final int N_THREADS = 3;
       
    45 
       
    46     private static final String LOGGER_PREFIX = "myLogger-";
       
    47     private static final String RESOURCE_PREFIX
       
    48         = "LoggerResourceBundleRace$MyResources";
       
    49     // these counters are AtomicInteger since any worker thread can increment
       
    50     private static final AtomicInteger iaeCnt = new AtomicInteger();
       
    51     private static final AtomicInteger worksCnt = new AtomicInteger();
       
    52 
       
    53     Logger dummy;   // dummy Logger
       
    54 
       
    55     LoggerResourceBundleRace(String name, int n_threads, int n_loops,
       
    56         int n_secs) {
       
    57         super(name, n_threads, n_loops, n_secs);
       
    58     }
       
    59 
       
    60 
       
    61     // Main test driver
       
    62     //
       
    63     public static void main(String[] args) {
       
    64         LoggerResourceBundleRace test
       
    65             = new LoggerResourceBundleRace("LoggerResourceBundleRace",
       
    66                                            N_THREADS, N_LOOPS, N_SECS);
       
    67         test.setVerbose(
       
    68             Boolean.getBoolean("LoggerResourceBundleRace.verbose"));
       
    69 
       
    70         DriverThread driver = new DriverThread(test);
       
    71         MyWorkerThread[] workers = new MyWorkerThread[N_THREADS];
       
    72         for (int i = 0; i < workers.length; i++) {
       
    73             workers[i] = new MyWorkerThread(i, test);
       
    74         }
       
    75         test.runTest(driver, workers);
       
    76     }
       
    77 
       
    78     public void oneTimeDriverInit(DriverThread dt) {
       
    79         super.oneTimeDriverInit(dt);
       
    80         dummy = null;
       
    81     }
       
    82 
       
    83     public void perRaceDriverInit(DriverThread dt) {
       
    84         super.perRaceDriverInit(dt);
       
    85 
       
    86         // - allocate a new dummy Logger without a ResourceBundle;
       
    87         //   this gives the racing threads less to do
       
    88         // - reset the counters
       
    89         dummy = Logger.getLogger(LOGGER_PREFIX + getLoopCnt());
       
    90         iaeCnt.set(0);
       
    91         worksCnt.set(0);
       
    92     }
       
    93 
       
    94     public void executeRace(WorkerThread wt) {
       
    95         super.executeRace(wt);
       
    96 
       
    97         Logger myLogger = null;
       
    98         try {
       
    99             MyWorkerThread mwt = (MyWorkerThread) wt;  // short hand
       
   100 
       
   101             // Here is the race:
       
   102             // - the target Logger object has already been created by
       
   103             //   the DriverThread without a ResourceBundle name
       
   104             // - in parallel, each WorkerThread calls Logger.getLogger()
       
   105             //   with a different ResourceBundle name
       
   106             // - Logger.getLogger() should only successfully set the
       
   107             //   ResourceBundle name for one WorkerThread; all other
       
   108             //   WorkerThread calls to Logger.getLogger() should throw
       
   109             //   IllegalArgumentException
       
   110             myLogger = Logger.getLogger(LOGGER_PREFIX + getLoopCnt(),
       
   111                                         mwt.rbName);
       
   112             if (myLogger.getResourceBundleName().equals(mwt.rbName)) {
       
   113                 // no exception and the ResourceBundle names match
       
   114                 worksCnt.incrementAndGet();  // ignore return
       
   115             } else {
       
   116                 System.err.println(wt.getName()
       
   117                     + ": ERROR: expected ResourceBundleName '"
       
   118                     + mwt.rbName + "' does not match actual '"
       
   119                     + myLogger.getResourceBundleName() + "'");
       
   120                 incAndGetFailCnt();  // ignore return
       
   121             }
       
   122         } catch (IllegalArgumentException iae) {
       
   123             iaeCnt.incrementAndGet();  // ignore return
       
   124         } catch (MissingResourceException mre) {
       
   125             // This exception happens when N_THREADS above does not
       
   126             // match the number of MyResources inner classes below.
       
   127             // We exit since this is a coding error.
       
   128             unexpectedException(wt, mre);
       
   129             System.exit(2);
       
   130         }
       
   131     }
       
   132 
       
   133     public void checkRaceResults(DriverThread dt) {
       
   134         super.checkRaceResults(dt);
       
   135 
       
   136         if (worksCnt.get() != 1) {
       
   137             System.err.println(dt.getName() + ": ERROR: worksCnt should be 1"
       
   138                 + ": loopCnt=" + getLoopCnt() + ", worksCnt=" + worksCnt.get());
       
   139             incAndGetFailCnt();  // ignore return
       
   140         } else if (iaeCnt.get() != N_THREADS - 1) {
       
   141             System.err.println(dt.getName() + ": ERROR: iaeCnt should be "
       
   142                 + (N_THREADS - 1) + ": loopCnt=" + getLoopCnt()
       
   143                 + ", iaeCnt=" + iaeCnt.get());
       
   144             incAndGetFailCnt();  // ignore return
       
   145         }
       
   146     }
       
   147 
       
   148     public void oneTimeDriverEpilog(DriverThread dt) {
       
   149         super.oneTimeDriverEpilog(dt);
       
   150 
       
   151         // Use the dummy Logger after the testing loop to make sure that
       
   152         // dummy doesn't get optimized away in the testing loop.
       
   153         dummy.info("This is a test message.");
       
   154     }
       
   155 
       
   156     // N_THREADS above must match number of MyResources inner classes
       
   157     //
       
   158     public static class MyResources0 extends ListResourceBundle {
       
   159         static final Object[][] contents = {
       
   160             {"sample1", "translation #1 for sample1"},
       
   161             {"sample2", "translation #1 for sample2"},
       
   162         };
       
   163 
       
   164         public Object[][] getContents() {
       
   165             return contents;
       
   166         }
       
   167     }
       
   168 
       
   169     public static class MyResources1 extends ListResourceBundle {
       
   170         static final Object[][] contents = {
       
   171             {"sample1", "translation #2 for sample1"},
       
   172             {"sample2", "translation #2 for sample2"},
       
   173         };
       
   174 
       
   175         public Object[][] getContents() {
       
   176             return contents;
       
   177         }
       
   178     }
       
   179 
       
   180     public static class MyResources2 extends ListResourceBundle {
       
   181         static final Object[][] contents = {
       
   182             {"sample1", "translation #3 for sample1"},
       
   183             {"sample2", "translation #3 for sample2"},
       
   184         };
       
   185 
       
   186         public Object[][] getContents() {
       
   187             return contents;
       
   188         }
       
   189     }
       
   190 
       
   191 
       
   192     // WorkerThread with a thread specific ResourceBundle name
       
   193     //
       
   194     public static class MyWorkerThread extends WorkerThread {
       
   195         public final String rbName;  // ResourceBundle name
       
   196 
       
   197         MyWorkerThread(int workerNum, RacingThreadsTest test) {
       
   198             super(workerNum, test);
       
   199 
       
   200             rbName = RESOURCE_PREFIX + workerNum;
       
   201         }
       
   202     }
       
   203 }