blob: c56ad43561a8f906299ea4dab5c00a564d9ed4ac [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 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 * @summary Verify that concurrent class descriptor lookups function properly,
26 * even when class descriptor initialization is slow or throws an
27 * exception.
28 */
29
30import java.io.*;
31
32class Good implements Serializable {
33 static {
34 try { Thread.sleep(1000); } catch (InterruptedException ex) {}
35 }
36}
37
38class Bad implements Serializable {
39 // explicit suid triggers class initialization during classdesc lookup
40 private static final long serialVersionUID = 0xBAD;
41 static {
42 try { Thread.sleep(1000); } catch (InterruptedException ex) {}
43 if ("foo".equals("foo")) {
44 throw new RuntimeException();
45 }
46 }
47}
48
49class SuccessfulLookup extends Thread {
50 Class cl;
51 long suid;
52 Object barrier;
53 boolean ok;
54
55 SuccessfulLookup(Class cl, long suid, Object barrier) {
56 this.cl = cl;
57 this.suid = suid;
58 this.barrier = barrier;
59 }
60
61 public void run() {
62 synchronized (barrier) {
63 try { barrier.wait(); } catch (InterruptedException ex) {}
64 }
65 for (int i = 0; i < 100; i++) {
66 if (ObjectStreamClass.lookup(cl).getSerialVersionUID() != suid) {
67 return;
68 }
69 }
70 ok = true;
71 }
72}
73
74class FailingLookup extends Thread {
75 Class cl;
76 Object barrier;
77 boolean ok;
78
79 FailingLookup(Class cl, Object barrier) {
80 this.cl = cl;
81 this.barrier = barrier;
82 }
83
84 public void run() {
85 synchronized (barrier) {
86 try { barrier.wait(); } catch (InterruptedException ex) {}
87 }
88 for (int i = 0; i < 100; i++) {
89 try {
90 ObjectStreamClass.lookup(cl);
91 return;
92 } catch (Throwable th) {
93 }
94 }
95 ok = true;
96 }
97}
98
99public class ConcurrentClassDescLookup {
100 public static void main(String[] args) throws Exception {
101 ClassLoader loader = ConcurrentClassDescLookup.class.getClassLoader();
102 Class cl = Class.forName("Good", false, loader);
103 Object barrier = new Object();
104 SuccessfulLookup[] slookups = new SuccessfulLookup[50];
105 for (int i = 0; i < slookups.length; i++) {
106 slookups[i] =
107 new SuccessfulLookup(cl, 6319710844400051132L, barrier);
108 slookups[i].start();
109 }
110 Thread.sleep(1000);
111 synchronized (barrier) {
112 barrier.notifyAll();
113 }
114 for (int i = 0; i < slookups.length; i++) {
115 slookups[i].join();
116 if (!slookups[i].ok) {
117 throw new Error();
118 }
119 }
120
121 cl = Class.forName("Bad", false, loader);
122 FailingLookup[] flookups = new FailingLookup[50];
123 for (int i = 0; i < flookups.length; i++) {
124 flookups[i] = new FailingLookup(cl, barrier);
125 flookups[i].start();
126 }
127 Thread.sleep(1000);
128 synchronized (barrier) {
129 barrier.notifyAll();
130 }
131 for (int i = 0; i < slookups.length; i++) {
132 flookups[i].join();
133 if (!flookups[i].ok) {
134 throw new Error();
135 }
136 }
137 }
138}