Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/ChunkedOutputStream/checkError.java
38867 views
1
/*
2
* Copyright (c) 2004, 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 5054016
27
* @run main/othervm/timeout=300 checkError
28
* @summary get the failure immediately when writing individual chunks over socket fail
29
*/
30
31
import java.io.*;
32
import java.net.*;
33
import java.util.StringTokenizer;
34
35
36
public class checkError {
37
static final int TEST_PASSED = 95;
38
static final int TEST_FAILED = 97;
39
40
static int testStatus = TEST_PASSED;
41
42
static String serverName = "localhost";
43
static int bufferSize = 8192; // 8k
44
static int totalBytes = 1048576; // 1M
45
46
static int j = 0;
47
48
static public Object threadStarting = new Object();
49
static public Object threadWaiting = new Object();
50
51
52
public static void main(String[] args) throws Exception {
53
HttpURLConnection conn = null;
54
OutputStream toServer = null;
55
byte[] buffer = null;
56
HTTPServer server = null;
57
synchronized(threadWaiting) {
58
System.out.println("HTTP-client>Starting default Http-server");
59
synchronized(threadStarting) {
60
server = new HTTPServer();
61
server.start();
62
try {
63
System.out.println("waiting server to be start");
64
threadStarting.wait();
65
} catch (InterruptedException e) {
66
}
67
}
68
int port = server.getPort();
69
URL url = new URL("http://" + serverName + ":" + port);
70
conn = (HttpURLConnection )url.openConnection();
71
conn.setRequestMethod("POST");
72
conn.setDoOutput(true);
73
74
System.out.println("assigning 1024 to the chunk length");
75
conn.setChunkedStreamingMode(1024);
76
conn.connect();
77
78
toServer = conn.getOutputStream();
79
buffer = getThickBuffer(bufferSize);
80
System.out.println("sending " + totalBytes + " bytes");
81
}
82
83
int byteAtOnce = 0;
84
int sendingBytes = totalBytes;
85
try {
86
while (sendingBytes > 0) {
87
if (sendingBytes > bufferSize) {
88
byteAtOnce = bufferSize;
89
} else {
90
byteAtOnce = sendingBytes;
91
}
92
toServer.write(buffer, 0, byteAtOnce);
93
sendingBytes -= byteAtOnce;
94
// System.out.println((totalBytes - sendingBytes) + " was sent");
95
toServer.flush();
96
}
97
} catch (OutOfMemoryError e) {
98
e.printStackTrace();
99
System.out.println("***ERR***> UNEXPECTED error: " + e);
100
testStatus = TEST_FAILED;
101
testExit();
102
} catch (IOException e) {
103
// e.printStackTrace();
104
// this is the expected IOException
105
// due to server.close()
106
testStatus = TEST_PASSED;
107
testExit();
108
} finally {
109
toServer.close();
110
}
111
112
// we have not received the expected IOException
113
// test fail
114
testStatus = TEST_FAILED;
115
testExit();
116
117
}
118
119
static void testExit() {
120
if (testStatus == TEST_FAILED) {
121
throw new RuntimeException("Test Failed: haven't received the expected IOException");
122
} else {
123
System.out.println("TEST PASSED");
124
}
125
System.exit(testStatus);
126
}
127
128
static byte[] getThickBuffer(int size) {
129
130
byte[] buffer = new byte[size];
131
132
for (int i = 0; i < size; i++) {
133
if (j > 9)
134
j = 0;
135
String s = Integer.toString(j);
136
buffer[i] = (byte )s.charAt(0);
137
j++;
138
}
139
140
return buffer;
141
}
142
}
143
144
145
class HTTPServer extends Thread {
146
147
static volatile boolean isCompleted;
148
149
Socket client;
150
ServerSocket serverSocket;
151
152
int getPort() {
153
return serverSocket.getLocalPort();
154
}
155
156
public void run() {
157
158
synchronized(checkError.threadStarting) {
159
160
try {
161
serverSocket = new ServerSocket(0, 100);
162
} catch (Exception e) {
163
e.printStackTrace();
164
checkError.testStatus = checkError.TEST_FAILED;
165
return;
166
}
167
checkError.threadStarting.notify();
168
}
169
170
try {
171
client = serverSocket.accept();
172
} catch (Exception e) {
173
e.printStackTrace();
174
checkError.testStatus = checkError.TEST_FAILED;
175
return;
176
}
177
178
System.out.println("Server started");
179
180
BufferedReader in = null;
181
PrintStream out = null;
182
InputStreamReader reader = null;
183
String version = null;
184
String line;
185
String method;
186
187
synchronized(checkError.threadWaiting) {
188
try {
189
reader = new InputStreamReader(client.getInputStream());
190
in = new BufferedReader(reader);
191
line = in.readLine();
192
193
} catch (Exception e) {
194
e.printStackTrace();
195
checkError.testStatus = checkError.TEST_FAILED;
196
return;
197
}
198
StringTokenizer st = new StringTokenizer(line);
199
method = st.nextToken();
200
String fileName = st.nextToken();
201
202
// save version for replies
203
if (st.hasMoreTokens()) version = st.nextToken();
204
205
System.out.println("HTTP version: " + version);
206
207
}
208
209
try {
210
211
while (line != null && line.length() > 0) {
212
line = in.readLine();
213
System.out.println(line);
214
}
215
} catch (IOException e) {
216
e.printStackTrace();
217
checkError.testStatus = checkError.TEST_FAILED;
218
return;
219
}
220
221
if (method.equals("POST")) {
222
System.out.println("receiving data");
223
byte[] buf = new byte[1024];
224
try {
225
//reading bytes until chunk whose size is zero,
226
// see 19.4.6 Introduction of Transfer-Encoding in RFC2616
227
int count = 0;
228
while (count <=5) {
229
count++;
230
in.readLine();
231
}
232
233
System.out.println("Server socket is closed");
234
in.close();
235
client.close();
236
serverSocket.close();
237
238
} catch (IOException e) {
239
e.printStackTrace();
240
checkError.testStatus = checkError.TEST_FAILED;
241
return;
242
} catch (OutOfMemoryError e) {
243
e.printStackTrace();
244
checkError.testStatus = checkError.TEST_FAILED;
245
return;
246
}
247
248
}
249
}
250
251
}
252
253