3577N/A/*
4248N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3577N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3577N/A *
3577N/A * This code is free software; you can redistribute it and/or modify it
3577N/A * under the terms of the GNU General Public License version 2 only, as
3577N/A * published by the Free Software Foundation.
3577N/A *
3577N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3577N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3577N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3577N/A * version 2 for more details (a copy is included in the LICENSE file that
3577N/A * accompanied this code).
3577N/A *
3577N/A * You should have received a copy of the GNU General Public License version
3577N/A * 2 along with this work; if not, write to the Free Software Foundation,
3577N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3577N/A *
3577N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3577N/A * or visit www.oracle.com if you need additional information or have any
3577N/A * questions.
3577N/A *
3577N/A * -------------------------------------------
3577N/A *
3577N/A * Portions Copyright (c) 2010, 2011 IBM Corporation
3577N/A */
3577N/A
3577N/A/*
3577N/A * @test
3577N/A * @bug 6927486
3577N/A * @summary Serializing Hashtable objects which refer to each other should not be able to deadlock.
3577N/A * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
3577N/A */
3577N/A
3577N/Aimport java.io.ByteArrayOutputStream;
3577N/Aimport java.io.IOException;
3577N/Aimport java.io.ObjectOutputStream;
3577N/Aimport java.io.PrintWriter;
3577N/Aimport java.io.Serializable;
3577N/Aimport java.io.StringWriter;
3577N/Aimport java.util.ArrayList;
3577N/Aimport java.util.Hashtable;
3577N/Aimport java.util.List;
3577N/Aimport java.util.concurrent.CyclicBarrier;
3577N/A
3577N/Apublic class SerializationDeadlock {
3577N/A public static void main(final String[] args) throws Exception {
3577N/A // Test for Hashtable serialization deadlock
3577N/A final Hashtable<Object, Object> h1 = new Hashtable<>();
3577N/A final Hashtable<Object, Object> h2 = new Hashtable<>();
3577N/A final TestBarrier testStart = new TestBarrier(3);
3577N/A
3577N/A // Populate the hashtables so that they refer to each other
3577N/A h1.put(testStart, h2);
3577N/A h2.put(testStart, h1);
3577N/A
3577N/A final CyclicBarrier testEnd = new CyclicBarrier(3);
3577N/A final TestThread t1 = new TestThread(h1, testEnd);
3577N/A final TestThread t2 = new TestThread(h2, testEnd);
3577N/A
3577N/A t1.start();
3577N/A t2.start();
3577N/A
3577N/A // Wait for both test threads to have initiated serialization
3577N/A // of the 'testStart' object (and hence of both 'h1' and 'h2')
3577N/A testStart.await();
3577N/A
3577N/A // Wait for both test threads to successfully finish serialization
3577N/A // of 'h1' and 'h2'.
3577N/A System.out.println("Waiting for Hashtable serialization to complete ...");
3577N/A System.out.println("(This test will hang if serialization deadlocks)");
3577N/A testEnd.await();
3577N/A System.out.println("Test PASSED: serialization completed successfully");
3577N/A
3577N/A TestThread.handleExceptions();
3577N/A }
3577N/A
3577N/A static final class TestBarrier extends CyclicBarrier
3577N/A implements Serializable {
3577N/A public TestBarrier(final int count) {
3577N/A super(count);
3577N/A }
3577N/A
3577N/A private void writeObject(final ObjectOutputStream oos)
3577N/A throws IOException {
3577N/A oos.defaultWriteObject();
3577N/A // Wait until all test threads have started serializing data
3577N/A try {
3577N/A await();
3577N/A } catch (final Exception e) {
3577N/A throw new IOException("Test ERROR: Unexpected exception caught", e);
3577N/A }
3577N/A }
3577N/A }
3577N/A
3577N/A static final class TestThread extends Thread {
3577N/A private static final List<Exception> exceptions = new ArrayList<>();
3577N/A
3577N/A private final Hashtable<Object, Object> hashtable;
3577N/A private final CyclicBarrier testEnd;
3577N/A
3577N/A public TestThread(final Hashtable<Object, Object> hashtable,
3577N/A final CyclicBarrier testEnd) {
3577N/A this.hashtable = hashtable;
3577N/A this.testEnd = testEnd;
3577N/A setDaemon(true);
3577N/A }
3577N/A
3577N/A public void run() {
3577N/A try {
3577N/A final ByteArrayOutputStream baos = new ByteArrayOutputStream();
3577N/A final ObjectOutputStream oos = new ObjectOutputStream(baos);
3577N/A
3577N/A oos.writeObject(hashtable);
3577N/A oos.close();
3577N/A } catch (final IOException ioe) {
3577N/A addException(ioe);
3577N/A } finally {
3577N/A try {
3577N/A testEnd.await();
3577N/A } catch (Exception e) {
3577N/A addException(e);
3577N/A }
3577N/A }
3577N/A }
3577N/A
3577N/A private static synchronized void addException(final Exception exception) {
3577N/A exceptions.add(exception);
3577N/A }
3577N/A
3577N/A public static synchronized void handleExceptions() {
3577N/A if (false == exceptions.isEmpty()) {
3577N/A throw new RuntimeException(getErrorText(exceptions));
3577N/A }
3577N/A }
3577N/A
3577N/A private static String getErrorText(final List<Exception> exceptions) {
3577N/A final StringWriter sw = new StringWriter();
3577N/A final PrintWriter pw = new PrintWriter(sw);
3577N/A
3577N/A pw.println("Test ERROR: Unexpected exceptions thrown on test threads:");
3577N/A for (Exception exception : exceptions) {
3577N/A pw.print("\t");
3577N/A pw.println(exception);
3577N/A for (StackTraceElement element : exception.getStackTrace()) {
3577N/A pw.print("\t\tat ");
3577N/A pw.println(element);
3577N/A }
3577N/A }
3577N/A
3577N/A pw.close();
3577N/A return sw.toString();
3577N/A }
3577N/A }
3577N/A}
3577N/A