InetAddress address;
ServerSocket serverSocket; try {
address = InetAddress.getLocalHost(); int anyPort = 0; int defaultBacklog = BACKLOG_QUEUE_LENGTH;
serverSocket = new ServerSocket(anyPort, defaultBacklog, address);
} catch (IOException ioe) { thrownew Error("FATAL error while loading the test: " + ioe);
}
display(serverSocket.toString());
Socket server[] = new Socket[CONNECTIONS_TO_TRY];
Socket client[] = new Socket[CONNECTIONS_TO_TRY];
int i, port = serverSocket.getLocalPort(); for (i = 0; i < CONNECTIONS_TO_TRY; i++) { try {
client[i] = new Socket(address, port);
display(">Open: client[" + i + "] = " + client[i]);
server[i] = serverSocket.accept();
display(">Open: server[" + i + "] = " + server[i]);
} catch (IOException ioe) {
display(">OOPS! -- failed to open connection #" + i); break;
}
}
display("> Could open "
+ (i < CONNECTIONS_TO_TRY ? "only " : "") + i + " connections.");
display(">Closing them:"); for (int j = 0; j < i; j++) { try {
server[j].close();
client[j].close();
} catch (IOException ioe) { thrownew Error("FATAL error while loading the test: " + ioe);
}
}
display(">OK."); int safeConnections = i - CONNECTIONS_RESERVE; if (safeConnections < 1) {
safeConnections = 1;
} if (safeConnections < MAX_CONNECTIONS) {
complain("------------------------- CAUTION: -------------------");
complain("While checking the OS limitations, the test found that");
complain("only " + i + " TCP/IP socket connections could be safely open");
complain("simultaneously. However, possibility to open at least");
complain("" + MAX_CONNECTIONS + "+" + CONNECTIONS_RESERVE
+ " connections were expected.");
complain("");
complain("So, the test will check only " + safeConnections + " connection"
+ (safeConnections == 1 ? "" : "s") + " which seem");
complain("safe to be open simultaneously.");
complain("------------------------------------------------------");
} return safeConnections;
}
// // Get the Internet address of the local machine. //
InetAddress address = null; try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException exception) {
complain(exception.toString()); return2; // FAILED
}
display("Host: " + address);
// // Detect if it is safe to open MAX_CONNETIONS simultaneously: // finalint CONNECTIONS = detectOSLimitation();
// // Assign ServerSocket, and start client VM which should open // the prescribed number of CONNECTIONS to that ServerSocket. //
ServerSocket serverSocket; try { finalint anyPort = 0; finalint defaultBacklog = BACKLOG_QUEUE_LENGTH;
serverSocket = new ServerSocket(anyPort, defaultBacklog, address);
} catch (IOException exception) {
complain("Cannot assign a ServerSocket on: " + address); return2;
}
// // Start the client process on different VM. //
String jdkPath = System.getProperty("test.jdk");
Path toolName = Paths.get("bin", "java" + (isWindows() ? ".exe" : ""));
Path jdkTool = Paths.get(jdkPath, toolName.toString());
try { // Start clients on different JVM:
client = runtime.exec(command);
// Provide clients with access to stderr and stdout:
InputStream clientOut = client.getInputStream();
InputStream clientErr = client.getErrorStream();
redirectOut = new IORedirector(clientOut, DEBUG_MODE ? out : null);
redirectErr = new IORedirector(clientErr, out);
redirectOut.start();
redirectErr.start();
} catch (IOException exception) {
complain("Failed to start client: " + exception); return2;
} // // Start the server threads (and let them establish connections): //
Server server[] = new Server[CONNECTIONS]; for (int i = 0; i < CONNECTIONS; i++) {
server[i] = new Server(serverSocket);
display("Server #" + i + ": " + server[i]);
server[i].start();
}
// // Wait for the servers and the clients: // boolean testFailed = false;
try {
client.waitFor(); int clientStatus = client.exitValue();
display("Client VM exitCode=" + clientStatus);
// Let I/O redirectors to flush: if (redirectOut.isAlive()) {
redirectOut.join();
} if (redirectErr.isAlive()) {
redirectErr.join();
}
// If client has crashed, also terminate the server (to avoid hangup). if (clientStatus != 95) {
complain("Client VM has crashed: exit status=" + clientStatus);
testFailed = true;
}
// Client has finished OK; wait for the server. for (int i = 0; i < CONNECTIONS; i++) {
display("Server: waiting for #" + i); if (server[i].isAlive()) {
display("Server #" + i + ": (joining...)" + server[i]);
server[i].join();
} if (server[i].exception != null) { if (server[i].message != null) {
complain("Server #" + i + "(finished): with message:" + server[i].message);
}
Random random = new Random(getPort()); // suggested by Oleg -- to avoid race conditions /* try{ Thread.sleep(500); } catch(java.lang.InterruptedExceptione) {
}*/
for (int i = 0; i < DATA_PARCELS; i++) {
Parcel etalon = new Parcel(random);
message = "sending parcel number: " + i;
etalon.send(ostream);
ostream.flush();
message = "reading parcel number: " + i;
Parcel sample = new Parcel(istream); // read if (!sample.equals(etalon)) {
complain("Client thread for port #"
+ getPort() + " got unexpected parcel:\n"
+ "sample=" + sample + "\n"
+ "etalon=" + etalon); thrownew TestFailure( "parcel context is unexpected to client");
}
}
if (istream.available() > 0) { int datum = istream.read(); thrownew TestFailure( "client has read ambigous byte: " + datum);
}
ostream.close(); // implies: socket.close()
for (int i = 0; i < CONNECTIONS; i++) {
client[i].start();
}
// // Wait until testing is not finished: // int status = 0; for (int i = 0; i < CONNECTIONS; i++) {
display("Client: waiting for #" + i); if (client[i].isAlive()) {
display("Client #" + i + ": (joining...)" + client[i]);
try {
client[i].join();
} catch (InterruptedException ie) {
complain("Client #" + i + ": " + ie);
status = 3;
}
} if (client[i].exception != null) { if (client[i].message != null) {
complain("Client #" + i + "(finished) with message: " + client[i].message);
}
complain("Client #" + i + "(finished): " + client[i].exception);
client[i].exception.printStackTrace(complainStream);
complainStream.flush(); if (status == 0) {
status = 2;
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.