Java Code Examples for org.apache.hadoop.http.HttpServer2#start()

The following examples show how to use org.apache.hadoop.http.HttpServer2#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestJobEndNotifier.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private static HttpServer2 startHttpServer() throws Exception {
  new File(System.getProperty(
      "build.webapps", "build/webapps") + "/test").mkdirs();
  HttpServer2 server = new HttpServer2.Builder().setName("test")
      .addEndpoint(URI.create("http://localhost:0"))
      .setFindPort(true).build();
  server.addServlet("jobend", "/jobend", JobEndServlet.class);
  server.start();

  JobEndServlet.calledTimes = 0;
  JobEndServlet.requestUri = null;
  JobEndServlet.baseUrl = "http://localhost:"
      + server.getConnectorAddress(0).getPort() + "/";
  JobEndServlet.foundJobState = null;
  return server;
}
 
Example 2
Source File: TestTransferFsImage.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Test to verify the read timeout
 */
@Test(timeout = 5000)
public void testGetImageTimeout() throws Exception {
  HttpServer2 testServer = HttpServerFunctionalTest.createServer("hdfs");
  try {
    testServer.addServlet("ImageTransfer", ImageServlet.PATH_SPEC,
        TestImageTransferServlet.class);
    testServer.start();
    URL serverURL = HttpServerFunctionalTest.getServerURL(testServer);
    TransferFsImage.timeout = 2000;
    try {
      TransferFsImage.getFileClient(serverURL, "txid=1", null,
          null, false);
      fail("TransferImage Should fail with timeout");
    } catch (SocketTimeoutException e) {
      assertEquals("Read should timeout", "Read timed out", e.getMessage());
    }
  } finally {
    if (testServer != null) {
      testServer.stop();
    }
  }
}
 
Example 3
Source File: TestJobEndNotifier.java    From big-c with Apache License 2.0 6 votes vote down vote up
private static HttpServer2 startHttpServer() throws Exception {
  new File(System.getProperty(
      "build.webapps", "build/webapps") + "/test").mkdirs();
  HttpServer2 server = new HttpServer2.Builder().setName("test")
      .addEndpoint(URI.create("http://localhost:0"))
      .setFindPort(true).build();
  server.addServlet("jobend", "/jobend", JobEndServlet.class);
  server.start();

  JobEndServlet.calledTimes = 0;
  JobEndServlet.requestUri = null;
  JobEndServlet.baseUrl = "http://localhost:"
      + server.getConnectorAddress(0).getPort() + "/";
  JobEndServlet.foundJobState = null;
  return server;
}
 
Example 4
Source File: TestTransferFsImage.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Test to verify the read timeout
 */
@Test(timeout = 5000)
public void testGetImageTimeout() throws Exception {
  HttpServer2 testServer = HttpServerFunctionalTest.createServer("hdfs");
  try {
    testServer.addServlet("ImageTransfer", ImageServlet.PATH_SPEC,
        TestImageTransferServlet.class);
    testServer.start();
    URL serverURL = HttpServerFunctionalTest.getServerURL(testServer);
    TransferFsImage.timeout = 2000;
    try {
      TransferFsImage.getFileClient(serverURL, "txid=1", null,
          null, false);
      fail("TransferImage Should fail with timeout");
    } catch (SocketTimeoutException e) {
      assertEquals("Read should timeout", "Read timed out", e.getMessage());
    }
  } finally {
    if (testServer != null) {
      testServer.stop();
    }
  }
}
 
Example 5
Source File: TestTransferFsImage.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Test to verify the timeout of Image upload
 */
@Test(timeout = 10000)
public void testImageUploadTimeout() throws Exception {
  Configuration conf = new HdfsConfiguration();
  NNStorage mockStorage = Mockito.mock(NNStorage.class);
  HttpServer2 testServer = HttpServerFunctionalTest.createServer("hdfs");
  try {
    testServer.addServlet("ImageTransfer", ImageServlet.PATH_SPEC,
        TestImageTransferServlet.class);
    testServer.start();
    URL serverURL = HttpServerFunctionalTest.getServerURL(testServer);
    // set the timeout here, otherwise it will take default.
    TransferFsImage.timeout = 2000;

    File tmpDir = new File(new FileSystemTestHelper().getTestRootDir());
    tmpDir.mkdirs();

    File mockImageFile = File.createTempFile("image", "", tmpDir);
    FileOutputStream imageFile = new FileOutputStream(mockImageFile);
    imageFile.write("data".getBytes());
    imageFile.close();
    Mockito.when(
        mockStorage.findImageFile(Mockito.any(NameNodeFile.class),
            Mockito.anyLong())).thenReturn(mockImageFile);
    Mockito.when(mockStorage.toColonSeparatedString()).thenReturn(
        "storage:info:string");
    
    try {
      TransferFsImage.uploadImageFromStorage(serverURL, conf, mockStorage,
          NameNodeFile.IMAGE, 1L);
      fail("TransferImage Should fail with timeout");
    } catch (SocketTimeoutException e) {
      assertEquals("Upload should timeout", "Read timed out", e.getMessage());
    }
  } finally {
    testServer.stop();
  }
}
 
Example 6
Source File: TestTransferFsImage.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Test to verify the timeout of Image upload
 */
@Test(timeout = 10000)
public void testImageUploadTimeout() throws Exception {
  Configuration conf = new HdfsConfiguration();
  NNStorage mockStorage = Mockito.mock(NNStorage.class);
  HttpServer2 testServer = HttpServerFunctionalTest.createServer("hdfs");
  try {
    testServer.addServlet("ImageTransfer", ImageServlet.PATH_SPEC,
        TestImageTransferServlet.class);
    testServer.start();
    URL serverURL = HttpServerFunctionalTest.getServerURL(testServer);
    // set the timeout here, otherwise it will take default.
    TransferFsImage.timeout = 2000;

    File tmpDir = new File(new FileSystemTestHelper().getTestRootDir());
    tmpDir.mkdirs();

    File mockImageFile = File.createTempFile("image", "", tmpDir);
    FileOutputStream imageFile = new FileOutputStream(mockImageFile);
    imageFile.write("data".getBytes());
    imageFile.close();
    Mockito.when(
        mockStorage.findImageFile(Mockito.any(NameNodeFile.class),
            Mockito.anyLong())).thenReturn(mockImageFile);
    Mockito.when(mockStorage.toColonSeparatedString()).thenReturn(
        "storage:info:string");
    
    try {
      TransferFsImage.uploadImageFromStorage(serverURL, conf, mockStorage,
          NameNodeFile.IMAGE, 1L);
      fail("TransferImage Should fail with timeout");
    } catch (SocketTimeoutException e) {
      assertEquals("Upload should timeout", "Read timed out", e.getMessage());
    }
  } finally {
    testServer.stop();
  }
}
 
Example 7
Source File: TestLogLevel.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public void testDynamicLogLevel() throws Exception {
  String logName = TestLogLevel.class.getName();
  Log testlog = LogFactory.getLog(logName);

  //only test Log4JLogger
  if (testlog instanceof Log4JLogger) {
    Logger log = ((Log4JLogger)testlog).getLogger();
    log.debug("log.debug1");
    log.info("log.info1");
    log.error("log.error1");
    assertTrue(!Level.ERROR.equals(log.getEffectiveLevel()));

    HttpServer2 server = new HttpServer2.Builder().setName("..")
        .addEndpoint(new URI("http://localhost:0")).setFindPort(true)
        .build();
    
    server.start();
    String authority = NetUtils.getHostPortString(server
        .getConnectorAddress(0));

    //servlet
    URL url = new URL("http://" + authority + "/logLevel?log=" + logName
        + "&level=" + Level.ERROR);
    out.println("*** Connecting to " + url);
    URLConnection connection = url.openConnection();
    connection.connect();

    BufferedReader in = new BufferedReader(new InputStreamReader(
        connection.getInputStream()));
    for(String line; (line = in.readLine()) != null; out.println(line));
    in.close();

    log.debug("log.debug2");
    log.info("log.info2");
    log.error("log.error2");
    assertTrue(Level.ERROR.equals(log.getEffectiveLevel()));

    //command line
    String[] args = {"-setlevel", authority, logName, Level.DEBUG.toString()};
    LogLevel.main(args);
    log.debug("log.debug3");
    log.info("log.info3");
    log.error("log.error3");
    assertTrue(Level.DEBUG.equals(log.getEffectiveLevel()));
  }
  else {
    out.println(testlog.getClass() + " not tested.");
  }
}
 
Example 8
Source File: TestLogLevel.java    From big-c with Apache License 2.0 4 votes vote down vote up
public void testDynamicLogLevel() throws Exception {
  String logName = TestLogLevel.class.getName();
  Log testlog = LogFactory.getLog(logName);

  //only test Log4JLogger
  if (testlog instanceof Log4JLogger) {
    Logger log = ((Log4JLogger)testlog).getLogger();
    log.debug("log.debug1");
    log.info("log.info1");
    log.error("log.error1");
    assertTrue(!Level.ERROR.equals(log.getEffectiveLevel()));

    HttpServer2 server = new HttpServer2.Builder().setName("..")
        .addEndpoint(new URI("http://localhost:0")).setFindPort(true)
        .build();
    
    server.start();
    String authority = NetUtils.getHostPortString(server
        .getConnectorAddress(0));

    //servlet
    URL url = new URL("http://" + authority + "/logLevel?log=" + logName
        + "&level=" + Level.ERROR);
    out.println("*** Connecting to " + url);
    URLConnection connection = url.openConnection();
    connection.connect();

    BufferedReader in = new BufferedReader(new InputStreamReader(
        connection.getInputStream()));
    for(String line; (line = in.readLine()) != null; out.println(line));
    in.close();

    log.debug("log.debug2");
    log.info("log.info2");
    log.error("log.error2");
    assertTrue(Level.ERROR.equals(log.getEffectiveLevel()));

    //command line
    String[] args = {"-setlevel", authority, logName, Level.DEBUG.toString()};
    LogLevel.main(args);
    log.debug("log.debug3");
    log.info("log.info3");
    log.error("log.error3");
    assertTrue(Level.DEBUG.equals(log.getEffectiveLevel()));
  }
  else {
    out.println(testlog.getClass() + " not tested.");
  }
}