use of org.eclipse.jetty.io.SocketChannelEndPoint in project jetty.project by eclipse.
the class HTTP2ServerTest method testCommitFailure.
@Test
public void testCommitFailure() throws Exception {
final long delay = 1000;
final AtomicBoolean broken = new AtomicBoolean();
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// Wait for the SETTINGS frames to be exchanged.
Thread.sleep(delay);
broken.set(true);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
server.stop();
ServerConnector connector2 = new ServerConnector(server, new HTTP2ServerConnectionFactory(new HttpConfiguration())) {
@Override
protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {
@Override
public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException {
if (broken.get())
callback.failed(new IOException("explicitly_thrown_by_test"));
else
super.write(callback, buffers);
}
};
}
};
server.addConnector(connector2);
server.start();
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
MetaData.Request metaData = newRequest("GET", new HttpFields());
generator.control(lease, new HeadersFrame(1, metaData, null, true));
try (Socket client = new Socket("localhost", connector2.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
// The server will close the connection abruptly since it
// cannot write and therefore cannot even send the GO_AWAY.
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192);
boolean closed = parseResponse(client, parser, 2 * delay);
Assert.assertTrue(closed);
}
}
use of org.eclipse.jetty.io.SocketChannelEndPoint in project jetty.project by eclipse.
the class ThreadStarvationTest method testDefaultServletSuccess.
@Test
@Slow
public void testDefaultServletSuccess() throws Exception {
int maxThreads = 10;
QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads, maxThreads);
threadPool.setDetailedDump(true);
_server = new Server(threadPool);
// Prepare a big file to download.
File directory = MavenTestingUtils.getTargetTestingDir();
Files.createDirectories(directory.toPath());
String resourceName = "resource.bin";
Path resourcePath = Paths.get(directory.getPath(), resourceName);
try (OutputStream output = Files.newOutputStream(resourcePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
byte[] chunk = new byte[1024];
Arrays.fill(chunk, (byte) 'X');
chunk[chunk.length - 2] = '\r';
chunk[chunk.length - 1] = '\n';
for (int i = 0; i < 256 * 1024; ++i) output.write(chunk);
}
final CountDownLatch writePending = new CountDownLatch(1);
ServerConnector connector = new ServerConnector(_server, 0, 1) {
@Override
protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {
@Override
protected void onIncompleteFlush() {
super.onIncompleteFlush();
writePending.countDown();
}
};
}
};
connector.setIdleTimeout(Long.MAX_VALUE);
_server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler(_server, "/");
context.setResourceBase(directory.toURI().toString());
context.addServlet(DefaultServlet.class, "/*").setAsyncSupported(false);
_server.setHandler(context);
_server.start();
List<Socket> sockets = new ArrayList<>();
for (int i = 0; i < maxThreads * 2; ++i) {
Socket socket = new Socket("localhost", connector.getLocalPort());
sockets.add(socket);
OutputStream output = socket.getOutputStream();
String request = "" + "GET /" + resourceName + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
Thread.sleep(100);
}
// Wait for a the servlet to block.
Assert.assertTrue(writePending.await(5, TimeUnit.SECONDS));
long expected = Files.size(resourcePath);
byte[] buffer = new byte[48 * 1024];
List<Exchanger<Long>> totals = new ArrayList<>();
for (Socket socket : sockets) {
final Exchanger<Long> x = new Exchanger<>();
totals.add(x);
final InputStream input = socket.getInputStream();
new Thread() {
@Override
public void run() {
long total = 0;
try {
// look for CRLFCRLF
StringBuilder header = new StringBuilder();
int state = 0;
while (state < 4 && header.length() < 2048) {
int ch = input.read();
if (ch < 0)
break;
header.append((char) ch);
switch(state) {
case 0:
if (ch == '\r')
state = 1;
break;
case 1:
if (ch == '\n')
state = 2;
else
state = 0;
break;
case 2:
if (ch == '\r')
state = 3;
else
state = 0;
break;
case 3:
if (ch == '\n')
state = 4;
else
state = 0;
break;
}
}
while (total < expected) {
int read = input.read(buffer);
if (read < 0)
break;
total += read;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
x.exchange(total);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
for (Exchanger<Long> x : totals) {
Long total = x.exchange(-1L, 10000, TimeUnit.SECONDS);
Assert.assertEquals(expected, total.longValue());
}
// We could read everything, good.
for (Socket socket : sockets) socket.close();
}
use of org.eclipse.jetty.io.SocketChannelEndPoint in project jetty.project by eclipse.
the class SocketCustomizationListener method onOpened.
@Override
public void onOpened(Connection connection) {
EndPoint endp = connection.getEndPoint();
boolean ssl = false;
if (_ssl && endp instanceof DecryptedEndPoint) {
endp = ((DecryptedEndPoint) endp).getSslConnection().getEndPoint();
ssl = true;
}
if (endp instanceof SocketChannelEndPoint) {
Socket socket = ((SocketChannelEndPoint) endp).getSocket();
customize(socket, connection.getClass(), ssl);
}
}
use of org.eclipse.jetty.io.SocketChannelEndPoint in project athenz by yahoo.
the class JettyConnectionLoggerTest method getMockedProxyConncetion.
private MockedConnection getMockedProxyConncetion() throws Exception {
MockedConnection mockedConnection = new MockedConnection();
SSLEngine sslEngine = Mockito.mock(SSLEngine.class);
mockedConnection.sslEngine = sslEngine;
SslConnection sslConnection = Mockito.mock(SslConnection.class);
when(sslConnection.getSSLEngine()).thenReturn(sslEngine);
SocketChannelEndPoint socketChannelEndPoint = Mockito.mock(SocketChannelEndPoint.class);
when(socketChannelEndPoint.getLocalAddress()).thenReturn(new InetSocketAddress(InetAddress.getLocalHost(), 4444));
when(socketChannelEndPoint.getRemoteAddress()).thenReturn(new InetSocketAddress(InetAddress.getLocalHost(), 5555));
ProxyConnectionFactory.ProxyEndPoint proxyEndPoint = Mockito.mock(ProxyConnectionFactory.ProxyEndPoint.class);
when(proxyEndPoint.getRemoteAddress()).thenReturn(new InetSocketAddress(InetAddress.getLocalHost(), 3333));
when(proxyEndPoint.unwrap()).thenReturn(socketChannelEndPoint);
when(sslConnection.getEndPoint()).thenReturn(proxyEndPoint);
mockedConnection.endpoint = proxyEndPoint;
mockedConnection.sslConnection = sslConnection;
return mockedConnection;
}
use of org.eclipse.jetty.io.SocketChannelEndPoint in project jetty.project by eclipse.
the class ThreadStarvationTest method testFailureStarvation.
@Test
public void testFailureStarvation() throws Exception {
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
int acceptors = 0;
int selectors = 1;
int maxThreads = 10;
final int barried = maxThreads - acceptors - selectors * 2;
final CyclicBarrier barrier = new CyclicBarrier(barried);
QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads, maxThreads);
threadPool.setDetailedDump(true);
_server = new Server(threadPool);
ServerConnector connector = new ServerConnector(_server, acceptors, selectors) {
@Override
protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {
@Override
public boolean flush(ByteBuffer... buffers) throws IOException {
super.flush(buffers[0]);
throw new IOException("TEST FAILURE");
}
};
}
};
connector.setIdleTimeout(Long.MAX_VALUE);
_server.addConnector(connector);
final AtomicInteger count = new AtomicInteger(0);
_server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
int c = count.getAndIncrement();
try {
if (c < barried) {
barrier.await(10, TimeUnit.SECONDS);
}
} catch (InterruptedException | BrokenBarrierException | TimeoutException e) {
throw new ServletException(e);
}
baseRequest.setHandled(true);
response.setStatus(200);
response.setContentLength(13);
response.getWriter().print("Hello World!\n");
response.getWriter().flush();
}
});
_server.start();
List<Socket> sockets = new ArrayList<>();
for (int i = 0; i < maxThreads * 2; ++i) {
Socket socket = new Socket("localhost", connector.getLocalPort());
sockets.add(socket);
OutputStream output = socket.getOutputStream();
String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + // "Connection: close\r\n" +
"\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
}
byte[] buffer = new byte[48 * 1024];
List<Exchanger<Integer>> totals = new ArrayList<>();
for (Socket socket : sockets) {
final Exchanger<Integer> x = new Exchanger<>();
totals.add(x);
final InputStream input = socket.getInputStream();
new Thread() {
@Override
public void run() {
int read = 0;
try {
// look for CRLFCRLF
StringBuilder header = new StringBuilder();
int state = 0;
while (state < 4 && header.length() < 2048) {
int ch = input.read();
if (ch < 0)
break;
header.append((char) ch);
switch(state) {
case 0:
if (ch == '\r')
state = 1;
break;
case 1:
if (ch == '\n')
state = 2;
else
state = 0;
break;
case 2:
if (ch == '\r')
state = 3;
else
state = 0;
break;
case 3:
if (ch == '\n')
state = 4;
else
state = 0;
break;
}
}
read = input.read(buffer);
} catch (IOException e) {
// e.printStackTrace();
} finally {
try {
x.exchange(read);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
for (Exchanger<Integer> x : totals) {
Integer read = x.exchange(-1, 10, TimeUnit.SECONDS);
Assert.assertEquals(-1, read.intValue());
}
// We could read everything, good.
for (Socket socket : sockets) socket.close();
_server.stop();
}
}
Aggregations