2016-04-13 7 views
1

サーバーにファイルを送信するPOSTリクエストを呼び出す統合テストを行っています。 POSTの直後に、ドキュメントを取得してそのドキュメントの属性を返すGETリクエストを呼び出します。しかし、たまには数秒後に再度実行するまではドキュメントが存在しないと言って404エラーが出ることもあります。GETリクエストを呼び出す前にPOSTが処理を終了するのを待つ

私はPOSTがまだ処理を終えていないと思っていますので、10秒待つためにThread.sleepを実装しましたが、必要以上に長く待つかもしれないし、十分長く待たないかもしれません。 POSTがGETを要求する前に処理を完了できるようにする、何らかの「暗黙の待ち」がありますか?以下

は、コードスニペットである:

@Test 
public void PostDocumentThenCheckIfDocumentExistThenRemove() throws IOException, InterruptedException {  
    try { 
     String str = fileToStringProcessing("C:/Users/Linh/Desktop/file.xml"); 
     ResponseEntity<Message> postResponse = getRestTemplate().exchange(getUri() + "documents", HttpMethod.POST, new HttpEntity(str, getHeaders()), Message.class); 
     Thread.sleep(10000); 
     ResponseEntity<Account> getResponse = getRestTemplate().exchange(getUri() + "account/7452616052/documents?start=2015-01-01&end=2016-03-31", HttpMethod.GET, getHttpEntity(), Account.class); 
     ResponseEntity<Message> deleteResponse = getRestTemplate().exchange(getUri() + "documents/file.xml", HttpMethod.DELETE, getHttpEntity(), Message.class); 

     assertThat(postResponse.getStatusCode(), is(HttpStatus.CREATED)); 
     assertThat(getResponse.getStatusCode(), is(HttpStatus.OK)); 
     assertThat(deleteResponse.getStatusCode(), is(HttpStatus.OK)); 
    }catch(HttpClientErrorException e) { 
     fail("Error! Status code " + e.getStatusCode()); 
    } 
} 

ここコンソール404エラーです。ここでPOSTは成功しているが、GETは成功していないことがわかります。その後GETをやって、数秒後、成功

11:56:32.700 [main] DEBUG o.s.web.client.RestTemplate - POST request for "https://dpdev.billing.com/tf/dp/documents" resulted in 201 (Created) 
11:56:32.700 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Message] as "application/xml" using [org.springfr[email protected]1dac5ef] 
11:56:32.904 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" 
11:56:32.914 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json] 
11:56:33.690 [main] DEBUG o.s.web.client.RestTemplate - GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" resulted in 404 (Not Found); invoking error handler 

GETが表示され、右側の404の後にリクエストをDELETE:

12:00:21.383 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" 
12:00:21.444 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json] 
12:00:23.176 [main] DEBUG o.s.web.client.RestTemplate - GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" resulted in 200 (OK) 
12:00:23.176 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Account] as "application/xml" using [org.springfr[email protected]37271612] 
12:00:23.380 [main] DEBUG o.s.web.client.RestTemplate - Created DELETE request for "https://dpdev.billing.com/tf/dp/documents/file.xml" 
12:00:23.381 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json] 
12:00:25.120 [main] DEBUG o.s.web.client.RestTemplate - DELETE request for "https://dpdev.billing.com/tf/dp/documents/file.xml" resulted in 200 (OK) 
12:00:25.120 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Message] as "application/xml" using [org.springfr[email protected]37271612] 

答えて

0

は、非同期要求を受信したRESTサービスです。そうでない場合は、次のGET要求を発行するときに文書を利用できるようにする必要があります。 HTTPスレッドは、POSTに対する応答が得られるまでブロックされます。 POSTによってエラーコードが返されている可能性があります。 getを発行する前に、まずpostResponseからステータスコードを確認してください。

String str = fileToStringProcessing("C:/Users/Linh/Desktop/file.xml"); 
    ResponseEntity<Message> postResponse = getRestTemplate().exchange(getUri() + "documents", HttpMethod.POST, new HttpEntity(str, getHeaders()), Message.class); 
    if(postResponse.getStatusCode() == HttpStatus.OK){ or other success code 
     ResponseEntity<Account> getResponse = getRestTemplate().exchange(getUri() + "account/7452616052/documents?start=2015-01-01&end=2016-03-31", HttpMethod.GET, getHttpEntity(), Account.class); 
     ResponseEntity<Message> deleteResponse = getRestTemplate().exchange(getUri() + "documents/2015067452616054", HttpMethod.DELETE, getHttpEntity(), Message.class); 
    } 
    else{ 
     //process error response 
    } 
+0

完全なコードで質問を更新しました。おそらく全体の方法を立てたはずです。投稿時には、200 OKステータスを取得しているので、POSTは確実に成功しています。後でコンソール情報でも更新されます – LinhSaysHi

関連する問題