1 /*
   2  * Copyright (c) 2010, 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 /* @test
  25  * @bug 4517279
  26  * @summary Stochastic test of thread-local coder caches
  27  * @key randomness
  28  */
  29 
  30 import java.nio.*;
  31 import java.nio.charset.*;
  32 import java.util.*;
  33 
  34 
  35 public class BashCache {
  36 
  37     private static final int THREADS = 10;
  38     private static final int TRIALS = 1000;
  39 
  40     private static final Charset[] charsets
  41         = new Charset[] {
  42             Charset.forName("US-ASCII"),
  43             Charset.forName("UTF-8"),
  44             Charset.forName("CP1252"),
  45             Charset.forName("UTF-16BE") };
  46 
  47     private static volatile boolean failed = false;
  48 
  49     private static class Basher extends Thread {
  50 
  51         Random rnd = new Random(System.identityHashCode(this));
  52 
  53         public void run() {
  54             for (int i = 0; i < TRIALS; i++) {
  55                 Charset cs = charsets[rnd.nextInt(4)];
  56                 try {
  57                     if (rnd.nextBoolean()) {
  58                         cs.encode("hi mom");
  59                     } else {
  60                         cs.decode(ByteBuffer.wrap(new byte[] {
  61                             (byte)'x', (byte)'y',
  62                             (byte)'z', (byte)'z',
  63                             (byte)'y' }));
  64                     }
  65                 } catch (Exception x) {
  66                     x.printStackTrace();
  67                     failed = true;
  68                     return;
  69                 }
  70                 if (rnd.nextBoolean())
  71                     Thread.yield();
  72             }
  73         }
  74 
  75     }
  76 
  77     public static void main(String[] args) throws Exception {
  78         Charset cs = Charset.forName("us-ascii");
  79         Basher[] bashers = new Basher[THREADS];
  80         for (int i = 0; i < THREADS; i++) {
  81             bashers[i] = new Basher();
  82             bashers[i].start();
  83         }
  84         for (int i = 0; i < THREADS; i++)
  85             bashers[i].join();
  86         if (failed)
  87             throw new Exception("Test failed");
  88     }
  89 
  90 }