* Listen on a server endpoint with a no-op dispatcher and a
* client-side socket factory whose first socket hangs on
* attempt to read.
*/
SF sf = new SF();
final Endpoint ep =
TcpServerEndpoint.getInstance(null, 0, sf, null)
.enumerateListenEndpoints(new ListenContext() {
public ListenCookie addListenEndpoint(ListenEndpoint lep)
throws IOException
{
return lep.listen(new RequestDispatcher() {
public void dispatch(InboundRequest req) { }
}).getCookie();
}
});
/*
* Initiate first request in a separate thread. We expect it
* to hang waiting for connection handshake data; wait until
* we're sure that it is blocking on read.
*/
Thread t = new Thread(new Runnable() {
public void run() {
try {
System.err.println(
"Initiating first request asynchronously:");
ep.newRequest(InvocationConstraints.EMPTY).next();
System.err.println("First request initiated.");
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.setDaemon(true); // so that test VM exits upon completion
t.start();
System.err.println("Waiting for first request initiation to block:");
synchronized (sf) {
while (!sf.blocked) {
sf.wait();
}
}
System.err.println("First request initiation blocked.");
/*
* Initiate another request on the same endpoint while the
* first request initiation is still blocking on the bogus
* socket; this request should use a normal socket and thus it
* should return quickly. If not, then the test will hang
* (and time out, if run in the harness).
*/
System.err.println("Initiating second request:");
ep.newRequest(InvocationConstraints.EMPTY).next();
System.err.println("Second request initiated.");
System.err.println("TEST PASSED");
}