Search in sources :

Example 6 with SocketFactory

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);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) SocketFactory(javax.net.SocketFactory) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) Socket(java.net.Socket) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 7 with SocketFactory

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"));
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) SocketFactory(javax.net.SocketFactory) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 8 with SocketFactory

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());
}
Also used : SocketFactory(javax.net.SocketFactory) Test(org.junit.Test)

Example 9 with SocketFactory

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());
}
Also used : SocketFactory(javax.net.SocketFactory) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 10 with SocketFactory

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());
}
Also used : SocketFactory(javax.net.SocketFactory) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

SocketFactory (javax.net.SocketFactory)66 Socket (java.net.Socket)25 Test (org.junit.Test)25 IOException (java.io.IOException)18 InetSocketAddress (java.net.InetSocketAddress)14 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 SSLSocket (javax.net.ssl.SSLSocket)10 OutputStream (java.io.OutputStream)9 ServerSocket (java.net.ServerSocket)9 SocketAddress (java.net.SocketAddress)6 Configuration (org.apache.hadoop.conf.Configuration)5 ServerSocketFactory (javax.net.ServerSocketFactory)4 InputStream (java.io.InputStream)3 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)3 StandardSocketFactory (org.apache.hadoop.net.StandardSocketFactory)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 SocketException (java.net.SocketException)2