|
1 /* |
|
2 * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8006395 8012244 |
|
27 * @summary Tests racing code that reads and closes a Socket |
|
28 */ |
|
29 |
|
30 import java.io.InputStream; |
|
31 import java.net.ServerSocket; |
|
32 import java.net.Socket; |
|
33 import java.net.SocketException; |
|
34 import java.util.concurrent.Phaser; |
|
35 |
|
36 // Racey test, will not always fail, but if it does then we have a problem. |
|
37 |
|
38 public class Race { |
|
39 final static int THREADS = 100; |
|
40 |
|
41 public static void main(String[] args) throws Exception { |
|
42 try (ServerSocket ss = new ServerSocket(0)) { |
|
43 final int port = ss.getLocalPort(); |
|
44 final Phaser phaser = new Phaser(THREADS + 1); |
|
45 for (int i=0; i<100; i++) { |
|
46 final Socket s = new Socket("localhost", port); |
|
47 s.setSoLinger(false, 0); |
|
48 try (Socket sa = ss.accept()) { |
|
49 sa.setSoLinger(false, 0); |
|
50 final InputStream is = s.getInputStream(); |
|
51 Thread[] threads = new Thread[THREADS]; |
|
52 for (int j=0; j<THREADS; j++) { |
|
53 threads[j] = new Thread() { |
|
54 public void run() { |
|
55 try { |
|
56 phaser.arriveAndAwaitAdvance(); |
|
57 while (is.read() != -1) |
|
58 Thread.sleep(50); |
|
59 } catch (Exception x) { |
|
60 if (!(x instanceof SocketException |
|
61 && x.getMessage().equalsIgnoreCase("socket closed"))) |
|
62 x.printStackTrace(); |
|
63 // ok, expect Socket closed |
|
64 } |
|
65 }}; |
|
66 } |
|
67 for (int j=0; j<100; j++) |
|
68 threads[j].start(); |
|
69 phaser.arriveAndAwaitAdvance(); |
|
70 s.close(); |
|
71 for (int j=0; j<100; j++) |
|
72 threads[j].join(); |
|
73 } |
|
74 } |
|
75 } |
|
76 } |
|
77 } |