|
| 1 | +package oracle.modernappdev; |
| 2 | + |
| 3 | +import org.apache.http.HttpResponse; |
| 4 | +import org.apache.http.HttpStatus; |
| 5 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 6 | +import org.apache.http.util.EntityUtils; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 10 | +import static org.hamcrest.Matchers.equalTo; |
| 11 | + |
| 12 | +public class CrashAfterOrderInsertedBeforeMessageSentTest extends TransactionalTests { |
| 13 | + |
| 14 | + /** |
| 15 | + * Transactional lab of "simplify microservices" |
| 16 | + * Crash the order service after order is inserted and before message is sent to inventory service |
| 17 | + * Order should rollback. |
| 18 | + * @throws Exception |
| 19 | + */ |
| 20 | + @Test |
| 21 | + void testCrashAfterOrderInsertedBeforeMessageSent() throws Exception { |
| 22 | + CloseableHttpClient httpClient = getCloseableHttpClientAndDeleteAllOrders(); |
| 23 | + setInventoryToOne(); |
| 24 | + //set the "crashAfterInsert" failure case |
| 25 | + HttpResponse httpResponse = setCrashType(crashAfterInsert); |
| 26 | + //assert success of request |
| 27 | + assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_OK)); |
| 28 | + //place and order async, expecting hang due to order service crash |
| 29 | + placeOrder(httpClient, true); |
| 30 | + //sleep so that the next request is after order service restarts |
| 31 | + System.out.println("CrashAfterOrderInsertedBeforeMessageSentTest.testCrashAfterOrderInsertedBeforeMessageSent sleep for a minute"); |
| 32 | + Thread.sleep(60 * 1000); // non-deterministic but if exceed also indicates and issue we should look into |
| 33 | + //show the order (use clean/new http client) |
| 34 | + httpResponse = showorder(getHttpClient()); |
| 35 | + //assert success of request |
| 36 | + assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_OK)); |
| 37 | + //confirm the order is null (doesnt exist) because it was rolledback |
| 38 | + String jsonFromResponse = EntityUtils.toString(httpResponse.getEntity()); |
| 39 | + while (jsonFromResponse.contains("Connection refused")) { |
| 40 | + Thread.sleep(1000 * 1); |
| 41 | + httpResponse = showorder(getHttpClient()); |
| 42 | + jsonFromResponse = EntityUtils.toString(httpResponse.getEntity()); |
| 43 | + System.out.println("testCrashAfterOrderMessageReceived jsonFromResponse:" + jsonFromResponse); |
| 44 | + } |
| 45 | + System.out.println("CrashAfterOrderInsertedBeforeMessageSentTest.testCrashAfterOrderInsertedBeforeMessageSent jsonFromResponse:" + jsonFromResponse); |
| 46 | + assertThat(jsonFromResponse, equalTo("null")); // null means rolledback |
| 47 | + assertInventoryCount(1); |
| 48 | + } |
| 49 | +} |
0 commit comments