|
10 | 10 |
|
11 | 11 | import static org.junit.Assert.assertEquals;
|
12 | 12 | import static org.junit.Assert.assertFalse;
|
| 13 | +import static org.junit.Assert.assertNotNull; |
13 | 14 | import static org.junit.Assert.assertNull;
|
14 | 15 | import static org.junit.Assert.assertTrue;
|
15 | 16 | import static org.mockito.ArgumentMatchers.any;
|
| 17 | +import static org.mockito.ArgumentMatchers.argThat; |
16 | 18 | import static org.mockito.Mockito.doNothing;
|
17 | 19 | import static org.mockito.Mockito.doThrow;
|
18 | 20 | import static org.mockito.Mockito.mock;
|
|
30 | 32 | import java.io.InputStream;
|
31 | 33 | import java.net.URL;
|
32 | 34 | import java.util.Collections;
|
| 35 | +import java.util.concurrent.atomic.AtomicReference; |
| 36 | + |
33 | 37 | import org.json.JSONArray;
|
34 | 38 | import org.json.JSONObject;
|
35 | 39 | import org.junit.After;
|
@@ -552,4 +556,33 @@ public void testSaveObjectCommandUpdate() {
|
552 | 556 | ParsePlugins.reset();
|
553 | 557 | Parse.destroy();
|
554 | 558 | }
|
| 559 | + |
| 560 | + @Test |
| 561 | + public void testIdempotencyLogic() throws Exception { |
| 562 | + ParseHttpClient mockHttpClient = mock(ParseHttpClient.class); |
| 563 | + AtomicReference<String> requestIdAtomicReference = new AtomicReference<>(); |
| 564 | + when(mockHttpClient.execute( |
| 565 | + argThat( |
| 566 | + argument -> { |
| 567 | + assertNotNull( |
| 568 | + argument.getHeader(ParseRESTCommand.HEADER_REQUEST_ID)); |
| 569 | + if (requestIdAtomicReference.get() == null) |
| 570 | + requestIdAtomicReference.set( |
| 571 | + argument.getHeader( |
| 572 | + ParseRESTCommand.HEADER_REQUEST_ID)); |
| 573 | + assertEquals( |
| 574 | + argument.getHeader(ParseRESTCommand.HEADER_REQUEST_ID), |
| 575 | + requestIdAtomicReference.get()); |
| 576 | + return true; |
| 577 | + }))) |
| 578 | + .thenThrow(new IOException()); |
| 579 | + |
| 580 | + ParseRESTCommand.server = new URL("http://parse.com"); |
| 581 | + ParseRESTCommand command = new ParseRESTCommand.Builder().build(); |
| 582 | + Task<Void> task = command.executeAsync(mockHttpClient).makeVoid(); |
| 583 | + task.waitForCompletion(); |
| 584 | + |
| 585 | + verify(mockHttpClient, times(ParseRequest.DEFAULT_MAX_RETRIES + 1)) |
| 586 | + .execute(any(ParseHttpRequest.class)); |
| 587 | + } |
555 | 588 | }
|
0 commit comments