jdk/test/java/util/Hashtable/SerializationDeadlock.java
author ctornqvi
Thu, 16 May 2013 15:31:00 +0200
changeset 17598 f78d22d3c6c2
parent 9734 b33a24d77590
permissions -rw-r--r--
8008169: test/runtime/7158804/Test7158804.sh has bad copyright header Summary: Re-wrote test in Java in addition to fixing the Copyright notice. Also reviewed by leonid.mesnik@oracle.com Reviewed-by: coleenp, ctornqvi Contributed-by: Mikhailo Seledtsov <mikhailo.seledtsov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     1
/*
9734
b33a24d77590 7044486: open jdk repos have files with incorrect copyright headers, which can end up in src bundles
katleman
parents: 8394
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     4
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     8
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    13
 * accompanied this code).
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    14
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    18
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    21
 * questions.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    22
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    23
 * -------------------------------------------
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    24
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    25
 * Portions Copyright (c) 2010, 2011 IBM Corporation
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    26
 */
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    27
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    28
/*
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    29
 * @test
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    30
 * @bug 6927486
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    31
 * @summary Serializing Hashtable objects which refer to each other should not be able to deadlock.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    32
 * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    33
 */
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    34
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    35
import java.io.ByteArrayOutputStream;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    36
import java.io.IOException;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    37
import java.io.ObjectOutputStream;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    38
import java.io.PrintWriter;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    39
import java.io.Serializable;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    40
import java.io.StringWriter;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    41
import java.util.ArrayList;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    42
import java.util.Hashtable;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    43
import java.util.List;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    44
import java.util.concurrent.CyclicBarrier;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    45
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    46
public class SerializationDeadlock {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    47
    public static void main(final String[] args) throws Exception {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    48
        // Test for Hashtable serialization deadlock
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    49
        final Hashtable<Object, Object> h1 = new Hashtable<>();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    50
        final Hashtable<Object, Object> h2 = new Hashtable<>();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    51
        final TestBarrier testStart = new TestBarrier(3);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    52
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    53
        // Populate the hashtables so that they refer to each other
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    54
        h1.put(testStart, h2);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    55
        h2.put(testStart, h1);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    56
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    57
        final CyclicBarrier testEnd = new CyclicBarrier(3);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    58
        final TestThread t1 = new TestThread(h1, testEnd);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    59
        final TestThread t2 = new TestThread(h2, testEnd);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    60
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    61
        t1.start();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    62
        t2.start();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    63
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    64
        // Wait for both test threads to have initiated serialization
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    65
        // of the 'testStart' object (and hence of both 'h1' and 'h2')
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    66
        testStart.await();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    67
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    68
        // Wait for both test threads to successfully finish serialization
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    69
        // of 'h1' and 'h2'.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    70
        System.out.println("Waiting for Hashtable serialization to complete ...");
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    71
        System.out.println("(This test will hang if serialization deadlocks)");
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    72
        testEnd.await();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    73
        System.out.println("Test PASSED: serialization completed successfully");
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    74
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    75
        TestThread.handleExceptions();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    76
    }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    77
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    78
    static final class TestBarrier extends CyclicBarrier
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    79
            implements Serializable {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    80
        public TestBarrier(final int count) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    81
            super(count);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    82
        }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    83
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    84
        private void writeObject(final ObjectOutputStream oos)
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    85
                throws IOException {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    86
            oos.defaultWriteObject();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    87
            // Wait until all test threads have started serializing data
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    88
            try {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    89
                await();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    90
            } catch (final Exception e) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    91
                throw new IOException("Test ERROR: Unexpected exception caught", e);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    92
            }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    93
        }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    94
    }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    95
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    96
    static final class TestThread extends Thread {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    97
        private static final List<Exception> exceptions = new ArrayList<>();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    98
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    99
        private final Hashtable<Object, Object> hashtable;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   100
        private final CyclicBarrier testEnd;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   101
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   102
        public TestThread(final Hashtable<Object, Object> hashtable,
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   103
                final CyclicBarrier testEnd) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   104
            this.hashtable = hashtable;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   105
            this.testEnd = testEnd;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   106
            setDaemon(true);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   107
        }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   108
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   109
        public void run() {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   110
            try {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   111
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   112
                final ObjectOutputStream oos = new ObjectOutputStream(baos);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   113
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   114
                oos.writeObject(hashtable);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   115
                oos.close();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   116
            } catch (final IOException ioe) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   117
                addException(ioe);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   118
            } finally {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   119
                try {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   120
                    testEnd.await();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   121
                } catch (Exception e) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   122
                    addException(e);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   123
                }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   124
            }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   125
        }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   126
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   127
        private static synchronized void addException(final Exception exception) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   128
            exceptions.add(exception);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   129
        }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   130
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   131
        public static synchronized void handleExceptions() {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   132
            if (false == exceptions.isEmpty()) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   133
                throw new RuntimeException(getErrorText(exceptions));
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   134
            }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   135
        }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   136
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   137
        private static String getErrorText(final List<Exception> exceptions) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   138
            final StringWriter sw = new StringWriter();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   139
            final PrintWriter pw = new PrintWriter(sw);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   140
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   141
            pw.println("Test ERROR: Unexpected exceptions thrown on test threads:");
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   142
            for (Exception exception : exceptions) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   143
                pw.print("\t");
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   144
                pw.println(exception);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   145
                for (StackTraceElement element : exception.getStackTrace()) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   146
                    pw.print("\t\tat ");
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   147
                    pw.println(element);
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   148
                }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   149
            }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   150
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   151
            pw.close();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   152
            return sw.toString();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   153
        }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   154
    }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   155
}
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
   156