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