author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 49573 | 6b46983d6fbe |
child 55045 | 3a8433d967ea |
permissions | -rw-r--r-- |
15522 | 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 |
|
16866
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
15522
diff
changeset
|
26 |
* @bug 8006395 8012244 |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
15522
diff
changeset
|
27 |
* @summary Tests racing code that reads and closes a Socket |
15522 | 28 |
*/ |
29 |
||
30 |
import java.io.InputStream; |
|
31 |
import java.net.ServerSocket; |
|
32 |
import java.net.Socket; |
|
49573
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
33 |
import java.net.ConnectException; |
15522 | 34 |
import java.net.SocketException; |
35 |
import java.util.concurrent.Phaser; |
|
36 |
||
37 |
// Racey test, will not always fail, but if it does then we have a problem. |
|
38 |
||
39 |
public class Race { |
|
40 |
final static int THREADS = 100; |
|
41 |
||
42 |
public static void main(String[] args) throws Exception { |
|
43 |
try (ServerSocket ss = new ServerSocket(0)) { |
|
44 |
final int port = ss.getLocalPort(); |
|
45 |
final Phaser phaser = new Phaser(THREADS + 1); |
|
46 |
for (int i=0; i<100; i++) { |
|
49573
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
47 |
try { |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
48 |
final Socket s = new Socket("localhost", port); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
49 |
s.setSoLinger(false, 0); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
50 |
try (Socket sa = ss.accept()) { |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
51 |
sa.setSoLinger(false, 0); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
52 |
final InputStream is = s.getInputStream(); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
53 |
Thread[] threads = new Thread[THREADS]; |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
54 |
for (int j=0; j<THREADS; j++) { |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
55 |
threads[j] = new Thread() { |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
56 |
public void run() { |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
57 |
try { |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
58 |
phaser.arriveAndAwaitAdvance(); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
59 |
while (is.read() != -1) |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
60 |
Thread.sleep(50); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
61 |
} catch (Exception x) { |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
62 |
if (!(x instanceof SocketException |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
63 |
&& x.getMessage().equalsIgnoreCase("socket closed"))) |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
64 |
x.printStackTrace(); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
65 |
// ok, expect Socket closed |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
66 |
} |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
67 |
}}; |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
68 |
} |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
69 |
for (int j=0; j<100; j++) |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
70 |
threads[j].start(); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
71 |
phaser.arriveAndAwaitAdvance(); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
72 |
s.close(); |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
73 |
for (int j=0; j<100; j++) |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
74 |
threads[j].join(); |
15522 | 75 |
} |
49573
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
76 |
} catch (ConnectException e) { |
6b46983d6fbe
8196775: java/net/Socket/asyncClose/Race.java failed intermittently on Windows with ConnectException: Connection refused
michaelm
parents:
47216
diff
changeset
|
77 |
System.err.println("Exception " + e + " Port: " + port); |
15522 | 78 |
} |
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
} |