use of javax.net.SocketFactory in project tomcat by apache.
the class TestNonBlockingAPI method testNonBlockingWriteError.
@Test
public void testNonBlockingWriteError() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
TesterAccessLogValve alv = new TesterAccessLogValve();
ctx.getPipeline().addValve(alv);
NBWriteServlet servlet = new NBWriteServlet();
String servletName = NBWriteServlet.class.getName();
Tomcat.addServlet(ctx, servletName, servlet);
ctx.addServletMappingDecoded("/", servletName);
tomcat.getConnector().setProperty("socket.txBufSize", "1024");
tomcat.start();
SocketFactory factory = SocketFactory.getDefault();
Socket s = factory.createSocket("localhost", getPort());
ByteChunk result = new ByteChunk();
OutputStream os = s.getOutputStream();
os.write(("GET / HTTP/1.1\r\n" + "Host: localhost:" + getPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.flush();
InputStream is = s.getInputStream();
byte[] buffer = new byte[8192];
int read = 0;
int readSinceLastPause = 0;
int readTotal = 0;
while (read != -1 && readTotal < WRITE_SIZE / 32) {
long start = System.currentTimeMillis();
read = is.read(buffer);
long end = System.currentTimeMillis();
log.info("Client read [" + read + "] bytes in [" + (end - start) + "] ms");
if (read > 0) {
result.append(buffer, 0, read);
}
readSinceLastPause += read;
readTotal += read;
if (readSinceLastPause > WRITE_SIZE / 64) {
readSinceLastPause = 0;
Thread.sleep(WRITE_PAUSE_MS);
}
}
os.close();
is.close();
s.close();
String resultString = result.toString();
log.info("Client read " + resultString.length() + " bytes");
int lineStart = 0;
int lineEnd = resultString.indexOf('\n', 0);
String line = resultString.substring(lineStart, lineEnd + 1);
Assert.assertEquals("HTTP/1.1 200 \r\n", line);
// Listeners are invoked and access valve entries created on a different
// thread so give that thread a chance to complete its work.
int count = 0;
while (count < 100 && !(servlet.wlistener.onErrorInvoked || servlet.rlistener.onErrorInvoked)) {
Thread.sleep(100);
count++;
}
while (count < 100 && alv.getEntryCount() < 1) {
Thread.sleep(100);
count++;
}
Assert.assertTrue("Error listener should have been invoked.", servlet.wlistener.onErrorInvoked || servlet.rlistener.onErrorInvoked);
// TODO Figure out why non-blocking writes with the NIO connector appear
// to be slower on Linux
alv.validateAccessLog(1, 500, WRITE_PAUSE_MS, WRITE_PAUSE_MS + 30 * 1000);
}
use of javax.net.SocketFactory in project tomcat by apache.
the class TesterAjpNonBlockingClient method testNonBlockingWrite.
@Test
public void testNonBlockingWrite() throws Exception {
SocketFactory factory = SocketFactory.getDefault();
Socket s = factory.createSocket("localhost", 80);
ByteChunk result = new ByteChunk();
OutputStream os = s.getOutputStream();
os.write(("GET /examples/servlets/nonblocking/numberwriter HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.flush();
InputStream is = s.getInputStream();
byte[] buffer = new byte[8192];
int read = 0;
int readSinceLastPause = 0;
while (read != -1) {
read = is.read(buffer);
if (read > 0) {
result.append(buffer, 0, read);
}
readSinceLastPause += read;
if (readSinceLastPause > 40000) {
readSinceLastPause = 0;
Thread.sleep(500);
}
}
os.close();
is.close();
s.close();
// Validate the result
String resultString = result.toString();
log.info("Client read " + resultString.length() + " bytes");
System.out.println(resultString);
Assert.assertTrue(resultString.contains("00000000000000010000"));
}
use of javax.net.SocketFactory in project mockito by mockito.
the class DeepStubbingTest method withPatternPrimitive.
/**
* Test that deep stubbing work with primitive expected values with
* pattern method arguments
*/
@Test
public void withPatternPrimitive() throws Exception {
int a = 12, b = 23, c = 34;
SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
when(sf.createSocket(eq("stackoverflow.com"), eq(80)).getPort()).thenReturn(a);
when(sf.createSocket(eq("google.com"), anyInt()).getPort()).thenReturn(b);
when(sf.createSocket(eq("stackoverflow.com"), eq(8080)).getPort()).thenReturn(c);
assertEquals(b, sf.createSocket("google.com", 80).getPort());
assertEquals(c, sf.createSocket("stackoverflow.com", 8080).getPort());
assertEquals(a, sf.createSocket("stackoverflow.com", 80).getPort());
}
use of javax.net.SocketFactory in project mockito by mockito.
the class DeepStubbingTest method oneLevelDeep.
/**
* Test that deep stubbing works for one intermediate level
*/
@Test
public void oneLevelDeep() throws Exception {
OutputStream out = new ByteArrayOutputStream();
SocketFactory socketFactory = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
when(socketFactory.createSocket().getOutputStream()).thenReturn(out);
assertSame(out, socketFactory.createSocket().getOutputStream());
}
use of javax.net.SocketFactory in project mockito by mockito.
the class DeepStubbingTest method withComplexPatternArguments.
/**
* Test that deep stubbing work with argument patterns
*/
@Test
public void withComplexPatternArguments() throws Exception {
OutputStream out1 = new ByteArrayOutputStream();
OutputStream out2 = new ByteArrayOutputStream();
SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
when(sf.createSocket(anyString(), eq(80)).getOutputStream()).thenReturn(out1);
when(sf.createSocket(anyString(), eq(8080)).getOutputStream()).thenReturn(out2);
assertSame(out2, sf.createSocket("stackoverflow.com", 8080).getOutputStream());
assertSame(out1, sf.createSocket("google.com", 80).getOutputStream());
assertSame(out2, sf.createSocket("google.com", 8080).getOutputStream());
assertSame(out1, sf.createSocket("stackoverflow.com", 80).getOutputStream());
}
Aggregations