1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7045594 8002070
* @summary ResourceBundle setting race in Logger.getLogger(name, rbName)
* @author Daniel D. Daugherty
* @build RacingThreadsTest LoggerResourceBundleRace
* @run main/othervm LoggerResourceBundleRace
*
* (In samevm mode, the bundle classes don't end up in the classpath.)
*/
import java.util.ListResourceBundle;
import java.util.MissingResourceException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
public class LoggerResourceBundleRace extends RacingThreadsTest {
private static final int N_LOOPS = 500000; // # of race loops
private static final int N_SECS = 15; // # of secs to run test
// # of parallel threads; must match number of MyResources inner classes
private static final int N_THREADS = 3;
private static final String LOGGER_PREFIX = "myLogger-";
private static final String RESOURCE_PREFIX
= "LoggerResourceBundleRace$MyResources";
// these counters are AtomicInteger since any worker thread can increment
private static final AtomicInteger iaeCnt = new AtomicInteger();
private static final AtomicInteger worksCnt = new AtomicInteger();
Logger dummy; // dummy Logger
LoggerResourceBundleRace(String name, int n_threads, int n_loops,
int n_secs) {
super(name, n_threads, n_loops, n_secs);
}
// Main test driver
//
public static void main(String[] args) {
LoggerResourceBundleRace test
= new LoggerResourceBundleRace("LoggerResourceBundleRace",
N_THREADS, N_LOOPS, N_SECS);
test.setVerbose(
Boolean.getBoolean("LoggerResourceBundleRace.verbose"));
DriverThread driver = new DriverThread(test);
MyWorkerThread[] workers = new MyWorkerThread[N_THREADS];
for (int i = 0; i < workers.length; i++) {
workers[i] = new MyWorkerThread(i, test);
}
test.runTest(driver, workers);
}
public void oneTimeDriverInit(DriverThread dt) {
super.oneTimeDriverInit(dt);
dummy = null;
}
public void perRaceDriverInit(DriverThread dt) {
super.perRaceDriverInit(dt);
// - allocate a new dummy Logger without a ResourceBundle;
// this gives the racing threads less to do
// - reset the counters
dummy = Logger.getLogger(LOGGER_PREFIX + getLoopCnt());
iaeCnt.set(0);
worksCnt.set(0);
}
public void executeRace(WorkerThread wt) {
super.executeRace(wt);
Logger myLogger = null;
try {
MyWorkerThread mwt = (MyWorkerThread) wt; // short hand
// Here is the race:
// - the target Logger object has already been created by
// the DriverThread without a ResourceBundle name
// - in parallel, each WorkerThread calls Logger.getLogger()
// with a different ResourceBundle name
// - Logger.getLogger() should only successfully set the
// ResourceBundle name for one WorkerThread; all other
// WorkerThread calls to Logger.getLogger() should throw
// IllegalArgumentException
myLogger = Logger.getLogger(LOGGER_PREFIX + getLoopCnt(),
mwt.rbName);
if (myLogger.getResourceBundleName().equals(mwt.rbName)) {
// no exception and the ResourceBundle names match
worksCnt.incrementAndGet(); // ignore return
} else {
System.err.println(wt.getName()
+ ": ERROR: expected ResourceBundleName '"
+ mwt.rbName + "' does not match actual '"
+ myLogger.getResourceBundleName() + "'");
incAndGetFailCnt(); // ignore return
}
} catch (IllegalArgumentException iae) {
iaeCnt.incrementAndGet(); // ignore return
} catch (MissingResourceException mre) {
// This exception happens when N_THREADS above does not
// match the number of MyResources inner classes below.
// We exit since this is a coding error.
unexpectedException(wt, mre);
System.exit(2);
}
}
public void checkRaceResults(DriverThread dt) {
super.checkRaceResults(dt);
if (worksCnt.get() != 1) {
System.err.println(dt.getName() + ": ERROR: worksCnt should be 1"
+ ": loopCnt=" + getLoopCnt() + ", worksCnt=" + worksCnt.get());
incAndGetFailCnt(); // ignore return
} else if (iaeCnt.get() != N_THREADS - 1) {
System.err.println(dt.getName() + ": ERROR: iaeCnt should be "
+ (N_THREADS - 1) + ": loopCnt=" + getLoopCnt()
+ ", iaeCnt=" + iaeCnt.get());
incAndGetFailCnt(); // ignore return
}
}
public void oneTimeDriverEpilog(DriverThread dt) {
super.oneTimeDriverEpilog(dt);
// Use the dummy Logger after the testing loop to make sure that
// dummy doesn't get optimized away in the testing loop.
dummy.info("This is a test message.");
}
// N_THREADS above must match number of MyResources inner classes
//
public static class MyResources0 extends ListResourceBundle {
static final Object[][] contents = {
{"sample1", "translation #1 for sample1"},
{"sample2", "translation #1 for sample2"},
};
public Object[][] getContents() {
return contents;
}
}
public static class MyResources1 extends ListResourceBundle {
static final Object[][] contents = {
{"sample1", "translation #2 for sample1"},
{"sample2", "translation #2 for sample2"},
};
public Object[][] getContents() {
return contents;
}
}
public static class MyResources2 extends ListResourceBundle {
static final Object[][] contents = {
{"sample1", "translation #3 for sample1"},
{"sample2", "translation #3 for sample2"},
};
public Object[][] getContents() {
return contents;
}
}
// WorkerThread with a thread specific ResourceBundle name
//
public static class MyWorkerThread extends WorkerThread {
public final String rbName; // ResourceBundle name
MyWorkerThread(int workerNum, RacingThreadsTest test) {
super(workerNum, test);
rbName = RESOURCE_PREFIX + workerNum;
}
}
}
|