author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5970 | d4e98bbfb0be |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2005, 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 |
@bug 4726957 4724030 6232954 |
|
26 |
@summary Test if the SocketChannel with timeout set can be closed immediately |
|
27 |
@run main/timeout=20 CloseTimeoutChannel |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.nio.*; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.net.*; |
|
34 |
||
35 |
public class CloseTimeoutChannel { |
|
36 |
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
|
37 |
int port = -1; |
2 | 38 |
try { |
39 |
ServerSocketChannel listener=ServerSocketChannel.open(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
40 |
listener.socket().bind(new InetSocketAddress(0)); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
41 |
port = listener.socket().getLocalPort(); |
2 | 42 |
AcceptorThread thread=new AcceptorThread(listener); |
43 |
thread.start(); |
|
44 |
} catch (IOException e) { |
|
45 |
System.out.println("Mysterious IO problem"); |
|
46 |
e.printStackTrace(); |
|
47 |
System.exit(1); |
|
48 |
} |
|
49 |
||
50 |
//Establish connection. Bug only happens if we open with channel. |
|
51 |
try { |
|
52 |
System.out.println("Establishing connection"); |
|
53 |
Socket socket=SocketChannel.open( |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
54 |
new InetSocketAddress("127.0.0.1", port)).socket(); |
2 | 55 |
OutputStream out=socket.getOutputStream(); |
56 |
InputStream in=socket.getInputStream(); |
|
57 |
||
58 |
System.out.println("1. Writing byte 1"); |
|
59 |
out.write((byte)1); |
|
60 |
||
61 |
int n=read(socket, in); |
|
62 |
System.out.println("Read byte "+n+"\n"); |
|
63 |
||
64 |
System.out.println("3. Writing byte 3"); |
|
65 |
out.write((byte)3); |
|
66 |
||
67 |
System.out.println("Closing"); |
|
68 |
socket.close(); |
|
69 |
} catch (IOException e) { |
|
70 |
System.out.println("Mysterious IO problem"); |
|
71 |
e.printStackTrace(); |
|
72 |
System.exit(1); |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
/** Reads one byte from in, which must be s.getInputStream. */ |
|
77 |
private static int read(Socket s, InputStream in) throws IOException { |
|
78 |
try { |
|
79 |
s.setSoTimeout(8000); //causes a bug! |
|
80 |
return in.read(); |
|
81 |
} finally { |
|
82 |
s.setSoTimeout(0); |
|
83 |
} |
|
84 |
||
85 |
} |
|
86 |
||
87 |
/** Server thread */ |
|
88 |
static class AcceptorThread extends Thread { |
|
89 |
final String INDENT="\t\t\t\t"; |
|
90 |
ServerSocketChannel _listener; |
|
91 |
/** @param listener MUST be bound to a port */ |
|
92 |
AcceptorThread(ServerSocketChannel listener) { |
|
93 |
_listener=listener; |
|
94 |
} |
|
95 |
||
96 |
public void run() { |
|
97 |
try { |
|
98 |
try { |
|
99 |
Thread.sleep(100); |
|
100 |
} catch (InterruptedException e) { } |
|
101 |
||
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
102 |
System.out.println(INDENT+"Listening on port "+ |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
103 |
_listener.socket().getLocalPort()); |
2 | 104 |
ByteBuffer buf=ByteBuffer.allocate(5); |
105 |
Socket client=_listener.accept().socket();; |
|
106 |
System.out.println(INDENT+"Accepted client"); |
|
107 |
||
108 |
OutputStream out=client.getOutputStream(); |
|
109 |
InputStream in=client.getInputStream(); |
|
110 |
||
111 |
int n=in.read(); |
|
112 |
System.out.println(INDENT+"Read byte "+n+"\n"); |
|
113 |
||
114 |
System.out.println(INDENT+"2. Writing byte 2"); |
|
115 |
out.write((byte)2); |
|
116 |
||
117 |
n=in.read(); |
|
118 |
System.out.println(INDENT+"Read byte "+n+"\n"); |
|
119 |
||
120 |
n=in.read(); |
|
121 |
System.out.println(INDENT+"Read byte " |
|
122 |
+(n<0 ? "EOF" : Integer.toString(n))); |
|
123 |
||
124 |
System.out.println(INDENT+"Closing"); |
|
125 |
client.close(); |
|
126 |
} catch (IOException e) { |
|
127 |
System.out.println(INDENT+"Error accepting!"); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
128 |
} finally { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
129 |
try { _listener.close(); } catch (IOException ignore) { } |
2 | 130 |
} |
131 |
} |
|
132 |
} |
|
133 |
} |