2016-05-13 3 views
0

私はHttpClientバージョン4.5を使用するクラスでテストを作成しようとしています。私が書いたテストはとても遅いので、問題を切り分けようとしました。HttpClientとLocalServerTestBaseは実際には遅いです

httpClient.execute(method) 

実行する10秒を取る:私は持っている問題は、命令があることである

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); 
    } 
} 

:私は私の問題を実証テストを書くことができます。これは非常に遅いですが、なぜこれが遅いのがわかりません。私のテストで何か間違いや忘れてしまったことはありますか?

+0

それが正しく実行されていますか?つまり、テスト対象のサーバーは起動されますか? –

+0

ハンドラが正しく呼び出された、はい –

答えて

0

これは、レスポンスオブジェクトで何もしないためです。少なくとも1人はそれを閉じなければなりません。私は理解できない理由で、次のように基底クラスのHTTPClientのを使用すると、正常に動作するために

+0

「閉じる」とはどういう意味ですか? –

+0

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

+0

ちょうどチュートに続きますが、それほど良くないようです。とにかくありがとう –

0

@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); 
    } 
    } 

} 
関連する問題