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