test/java/util/logging/TestLoggerBundleSync.java

Print this page




 173           if (thrown != null) {
 174               throw thrown;
 175           }
 176           System.out.println("Passed: " + (nextLong.longValue() - sNextLong)
 177                   + " unique loggers created");
 178           System.out.println("\t " +(getRBcount.get() - sGetRBCount)
 179                   + " loggers tested by " + READERS + " Thread(s),");
 180           System.out.println("\t " + (setRBcount.get() - sSetRBCount)
 181                   + " resource bundles set by " + classes.size() + " Thread(s),");
 182           System.out.println("\t " + (setRBNameCount.get() - sSetRBNameCount)
 183                   + " resource bundle names set by " + classes.size() + " Thread(s),");
 184           System.out.println("\t ThreadMXBean.findDeadlockedThreads called "
 185                   + (checkCount.get() -sCheckCount) + " times by 1 Thread.");
 186 
 187     }
 188 
 189     final static class GetRB extends Thread {
 190         final static class MyHandler extends Handler {
 191             volatile ResourceBundle rb;
 192             volatile String rbName;

 193             @Override
 194             public synchronized void publish(LogRecord record) {

 195                 rb = record.getResourceBundle();
 196                 rbName = record.getResourceBundleName();
 197             }
 198 
 199             @Override
 200             public void flush() {
 201             }
 202 
 203             @Override
 204             public void close() throws SecurityException {
 205             }
 206         };
 207         final MyHandler handler = new MyHandler();
 208         @Override
 209         public void run() {
 210             try {
 211                 handler.setLevel(Level.FINEST);
 212                 while (goOn) {
 213                     Logger l;
 214                     Logger foo = Logger.getLogger("foo");
 215                     Logger bar = Logger.getLogger("foo.bar");
 216                     for (long i=0; i < nextLong.longValue() + 100 ; i++) {
 217                         if (!goOn) break;
 218                         l = Logger.getLogger("foo.bar.l"+i);
 219                         final ResourceBundle b = l.getResourceBundle();
 220                         final String name = l.getResourceBundleName();
 221                         if (b != null) {
 222                             if (!name.equals(b.getBaseBundleName())) {
 223                                 throw new RuntimeException("Unexpected bundle name: "
 224                                         +b.getBaseBundleName());
 225                             }
 226                         }
 227                         Logger ll = Logger.getLogger(l.getName()+".bie.bye");
 228                         ResourceBundle hrb;
 229                         String hrbName;





 230                         ll.setLevel(Level.FINEST);
 231                         ll.addHandler(handler);
 232                         ll.fine("dummy");
 233                         ll.removeHandler(handler);





 234                         hrb = handler.rb;
 235                         hrbName = handler.rbName;
 236                         if (name != null) {







 237                             if (!name.equals(hrbName)) {
 238                                 throw new RuntimeException("Unexpected bundle name: "
 239                                         +hrb.getBaseBundleName());
 240                             }


 241                             if (!name.equals(hrb.getBaseBundleName())) {
 242                                 throw new RuntimeException("Unexpected bundle name: "
 243                                         +hrb.getBaseBundleName());
 244                             }
 245                         }
 246 









 247                         getRBcount.incrementAndGet();
 248                         if (!goOn) break;
 249                         Thread.sleep(1);
 250                     }
 251                 }
 252            } catch (Exception x) {
 253                fail(x);
 254            }
 255         }
 256     }
 257 
 258     final static class SetRB extends Thread {
 259         final Class<? extends ResourceBundle> type;
 260         final static ExecutorService executor = Executors.newSingleThreadExecutor();
 261         final static class CheckRBTask implements Callable<Exception> {
 262             final Logger logger;
 263             volatile String rbName;
 264             volatile ResourceBundle rb;
 265 
 266             public CheckRBTask(Logger logger) {




 173           if (thrown != null) {
 174               throw thrown;
 175           }
 176           System.out.println("Passed: " + (nextLong.longValue() - sNextLong)
 177                   + " unique loggers created");
 178           System.out.println("\t " +(getRBcount.get() - sGetRBCount)
 179                   + " loggers tested by " + READERS + " Thread(s),");
 180           System.out.println("\t " + (setRBcount.get() - sSetRBCount)
 181                   + " resource bundles set by " + classes.size() + " Thread(s),");
 182           System.out.println("\t " + (setRBNameCount.get() - sSetRBNameCount)
 183                   + " resource bundle names set by " + classes.size() + " Thread(s),");
 184           System.out.println("\t ThreadMXBean.findDeadlockedThreads called "
 185                   + (checkCount.get() -sCheckCount) + " times by 1 Thread.");
 186 
 187     }
 188 
 189     final static class GetRB extends Thread {
 190         final static class MyHandler extends Handler {
 191             volatile ResourceBundle rb;
 192             volatile String rbName;
 193             volatile int count = 0;
 194             @Override
 195             public synchronized void publish(LogRecord record) {
 196                 count++;
 197                 rb = record.getResourceBundle();
 198                 rbName = record.getResourceBundleName();
 199             }
 200 
 201             @Override
 202             public void flush() {
 203             }
 204 
 205             @Override
 206             public void close() throws SecurityException {
 207             }
 208         };
 209         final MyHandler handler = new MyHandler();
 210         @Override
 211         public void run() {
 212             try {
 213                 handler.setLevel(Level.FINEST);
 214                 while (goOn) {
 215                     Logger l;
 216                     Logger foo = Logger.getLogger("foo");
 217                     Logger bar = Logger.getLogger("foo.bar");
 218                     for (long i=0; i < nextLong.longValue() + 100 ; i++) {
 219                         if (!goOn) break;
 220                         l = Logger.getLogger("foo.bar.l"+i);
 221                         final ResourceBundle b = l.getResourceBundle();
 222                         final String name = l.getResourceBundleName();
 223                         if (b != null) {
 224                             if (!name.equals(b.getBaseBundleName())) {
 225                                 throw new RuntimeException("Unexpected bundle name: "
 226                                         +b.getBaseBundleName());
 227                             }
 228                         }
 229                         Logger ll = Logger.getLogger(l.getName()+".bie.bye");
 230                         ResourceBundle hrb;
 231                         String hrbName;
 232                         if (handler.getLevel() != Level.FINEST) {
 233                             throw new RuntimeException("Handler level is not finest: "
 234                                     + handler.getLevel());
 235                         }
 236                         final int countBefore = handler.count;
 237                         ll.setLevel(Level.FINEST);
 238                         ll.addHandler(handler);
 239                         ll.fine("dummy");
 240                         ll.removeHandler(handler);
 241                         final int countAfter = handler.count;
 242                         if (countBefore == countAfter) {
 243                             throw new RuntimeException("Handler not called for "
 244                                     + ll.getName() + "("+ countAfter +")");
 245                         }
 246                         hrb = handler.rb;
 247                         hrbName = handler.rbName;
 248                         if (name != null) {
 249                             // if name is not null, then it implies that it
 250                             // won't change, since setResourceBundle() cannot
 251                             // replace a non null name.
 252                             // Since we never set the resource bundle on 'll',
 253                             // then ll must inherit its resource bundle [name]
 254                             // from l - and therefor we should find it in
 255                             // handler.rb/handler.rbName
 256                             if (!name.equals(hrbName)) {
 257                                 throw new RuntimeException("Unexpected bundle name: "
 258                                         +hrbName);
 259                             }
 260                             // here we know that hrbName is not null so hrb
 261                             // should not be null either.
 262                             if (!name.equals(hrb.getBaseBundleName())) {
 263                                 throw new RuntimeException("Unexpected bundle name: "
 264                                         +hrb.getBaseBundleName());
 265                             }
 266                         }
 267 
 268                         // Make sure to refer to 'l' explicitely in order to
 269                         // prevent eager garbage collecting before the end of
 270                         // the test (JDK-8030192)
 271                         if (!ll.getName().startsWith(l.getName())) {
 272                             throw new RuntimeException("Logger " + ll.getName()
 273                                     + "does not start with expected prefix "
 274                                     + l.getName());
 275                         }
 276 
 277                         getRBcount.incrementAndGet();
 278                         if (!goOn) break;
 279                         Thread.sleep(1);
 280                     }
 281                 }
 282            } catch (Exception x) {
 283                fail(x);
 284            }
 285         }
 286     }
 287 
 288     final static class SetRB extends Thread {
 289         final Class<? extends ResourceBundle> type;
 290         final static ExecutorService executor = Executors.newSingleThreadExecutor();
 291         final static class CheckRBTask implements Callable<Exception> {
 292             final Logger logger;
 293             volatile String rbName;
 294             volatile ResourceBundle rb;
 295 
 296             public CheckRBTask(Logger logger) {