2016-05-13 76 views
0

我想寫一個使用HttpClient版本4.5的類的測試。我寫的測試非常慢,所以我試圖找出問題。我可以寫證明我的問題一個試驗:HttpClient和LocalServerTestBase真的很慢

public class RequeteurRESTTest extends LocalServerTestBase { 

    private String startServer(String urlSuffix, HttpRequestHandler handler) throws Exception{ 
     this.serverBootstrap.registerHandler(urlSuffix, handler); 
     HttpHost target = start(); 
     String serverUrl = "http://localhost:" + target.getPort(); 
     return serverUrl; 
    } 

    @Test 
    public void testCase() throws Exception{ 
     String baseURL = startServer("/api", new HttpRequestHandler() { 
      @Override 
      public void handle(HttpRequest request, HttpResponse response,  HttpContext context) throws HttpException, IOException { 
       response.setStatusCode(HttpStatus.SC_OK); 
      } 
     }); 

     HttpClient httpClient; 
     httpClient = HttpClients.custom().build(); 

     HttpGet method = new HttpGet(baseURL + "/api"); 
     HttpResponse response = httpClient.execute(method); 
    } 
} 

我的問題是,該指令:

httpClient.execute(method) 

需要10秒執行。這很慢,但我不明白爲什麼這麼慢。我在測試中犯過什麼錯誤,或者忘記了什麼?

+0

是否正確執行?即被測試的服務器是否被調用? –

+0

處理程序被正確調用,是 –

回答

0

這很可能是因爲您對響應對象無能爲力。至少有一個人應該關閉它。

+0

「應該關閉它」是什麼意思? –

+0

http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fundamentals.html#d5e145 – oleg

+0

只是遵循tuto,但似乎沒有更好。不管怎麼說,還是要謝謝你 –

0

對於原因,我無法理解,使用基類的HttpClient的正常工作,具體如下:

@Test 
public void testCase() throws Exception{ 
    String baseURL = startServer("/api", new HttpRequestHandler() { 
     @Override 
     public void handle(HttpRequest request, HttpResponse response,  HttpContext context) throws HttpException, IOException { 
      response.setStatusCode(HttpStatus.SC_OK); 
     } 
    }); 

    HttpGet method = new HttpGet(baseURL + "/api"); 
    HttpResponse response = this.httpclient.execute(method); 

} 
1

如果你看一下LocalServerTestBase類的源代碼,你可以看到下面的方法:

@After 
public void shutDown() throws Exception { 
    if(this.httpclient != null) { 
     this.httpclient.close(); 
    } 

    if(this.server != null) { 
     this.server.shutdown(10L, TimeUnit.SECONDS); 
    } 

} 

通過在您自己的測試用例中重寫此設置,您可以將超時設置爲0秒。這應該消除你在執行中看到的延遲。

實現示例:

public class MyClassTest extends LocalServerTestBase{ 
    HttpHost httpHost; 
    @Before 
    public void setUp() throws Exception { 
     super.setUp(); 
     this.serverBootstrap.registerHandler("/*", new HttpRequestHandler() { 
     @Override 
     public void handle(HttpRequest request, HttpResponse response, HttpContext context) 
          throws HttpException, IOException { 
      System.out.println(request.getRequestLine().getMethod()); 
      response.setStatusCode(HttpStatus.SC_OK);     
     } 

     }); 

     httpHost = start(); 

    } 

    @Test 
    public void myMethodTest() throws Exception { 
     //Create an instance and give the server url it should call 
     MyClass instance = new MyClass(httpHost.toURI()); 
     //Test method that calls the server 
     instance.myMethod(); 
    } 

    @After @Override 
    public void shutDown() throws Exception { 
    if(this.httpclient != null) { 
     this.httpclient.close(); 
    } 
    if(this.server != null) { 
     this.server.shutdown(0L, TimeUnit.SECONDS); 
    } 
    } 

}