author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 7668 | jdk/test/java/nio/channels/Selector/BasicAccept.java@d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @summary Test Selector with ServerSocketChannels |
|
26 |
* @library .. |
|
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.net.*; |
|
31 |
import java.nio.*; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.nio.channels.spi.SelectorProvider; |
|
34 |
import java.util.*; |
|
35 |
||
36 |
||
37 |
public class BasicAccept { |
|
38 |
||
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
39 |
static void server(ServerSocketChannel ssc) throws Exception { |
2 | 40 |
Selector acceptSelector = Selector.open(); |
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
41 |
try { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
42 |
ssc.configureBlocking(false); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
43 |
SelectionKey acceptKey |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
44 |
= ssc.register(acceptSelector, SelectionKey.OP_ACCEPT); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
45 |
for (;;) { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
46 |
int n = acceptSelector.select(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
47 |
if (Thread.interrupted()) |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
48 |
break; |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
49 |
if (n == 0) |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
50 |
continue; |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
51 |
Set<SelectionKey> readyKeys = acceptSelector.selectedKeys(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
52 |
Iterator<SelectionKey> i = readyKeys.iterator(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
53 |
while (i.hasNext()) { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
54 |
SelectionKey sk = i.next(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
55 |
i.remove(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
56 |
ServerSocketChannel nextReady |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
57 |
= (ServerSocketChannel)sk.channel(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
58 |
SocketChannel sc = nextReady.accept(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
59 |
ByteBuffer bb = ByteBuffer.wrap(new byte[] { 42 }); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
60 |
sc.write(bb); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
61 |
sc.close(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
62 |
} |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
63 |
} |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
64 |
} finally { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
65 |
acceptSelector.close(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
66 |
} |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
67 |
} |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
68 |
|
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
69 |
private static class Server extends TestThread { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
70 |
final ServerSocketChannel ssc; |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
71 |
Server() throws IOException { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
72 |
super("Server", System.err); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
73 |
this.ssc = ServerSocketChannel.open() |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
74 |
.bind(new InetSocketAddress(0)); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
75 |
} |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
76 |
int port() { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
77 |
return ssc.socket().getLocalPort(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
78 |
} |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
79 |
void go() throws Exception { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
80 |
try { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
81 |
server(ssc); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
82 |
} finally { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
83 |
ssc.close(); |
2 | 84 |
} |
85 |
} |
|
86 |
} |
|
87 |
||
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
88 |
static void client(int port) throws Exception { |
2 | 89 |
// Get a connection from the server |
90 |
InetAddress lh = InetAddress.getLocalHost(); |
|
91 |
InetSocketAddress isa |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
92 |
= new InetSocketAddress(lh, port); |
2 | 93 |
int connectFailures = 0; |
94 |
boolean result = false; |
|
95 |
SocketChannel sc = SocketChannel.open(); |
|
96 |
for (;;) { |
|
97 |
try { |
|
98 |
result = sc.connect(isa); |
|
99 |
break; |
|
100 |
} catch (java.net.ConnectException e) { |
|
101 |
connectFailures++; |
|
102 |
if (connectFailures > 30) |
|
103 |
throw new RuntimeException("Cannot connect"); |
|
104 |
Thread.currentThread().sleep(100); |
|
105 |
sc = SocketChannel.open(); |
|
106 |
} |
|
107 |
} |
|
108 |
if (result) { |
|
109 |
System.err.println("Connected"); |
|
110 |
} else { |
|
111 |
// Only happens when server and client are on separate machines |
|
112 |
System.err.println("Connection pending..."); |
|
113 |
connectFailures = 0; |
|
114 |
while (!result) { |
|
115 |
try { |
|
116 |
result = sc.finishConnect(); |
|
117 |
if (!result) |
|
118 |
System.err.println("Not finished"); |
|
119 |
Thread.sleep(50); |
|
120 |
} catch (java.net.ConnectException e) { |
|
121 |
Thread.sleep(100); |
|
122 |
connectFailures++; |
|
123 |
if (connectFailures > 30) |
|
124 |
throw new RuntimeException("Cannot finish connecting"); |
|
125 |
} |
|
126 |
} |
|
127 |
System.err.println("Finished connecting"); |
|
128 |
} |
|
129 |
||
130 |
ByteBuffer bb = ByteBuffer.allocateDirect(1024); |
|
131 |
if (sc.read(bb) < 0) |
|
132 |
throw new RuntimeException("Failed to read from server"); |
|
133 |
if (bb.get(0) != 42) |
|
134 |
throw new RuntimeException("Read wrong byte from server"); |
|
135 |
System.err.println("Read from server"); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
136 |
sc.close(); |
2 | 137 |
} |
138 |
||
139 |
public static void main(String[] args) throws Exception { |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
140 |
Server server = new Server(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
141 |
server.start(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
142 |
try { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
143 |
client(server.port()); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
144 |
} finally { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
145 |
server.interrupt(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
146 |
server.finish(2000); |
2 | 147 |
} |
148 |
} |
|
149 |
||
150 |
} |