|
62 | 62 | import static org.mockito.ArgumentMatchers.anyString; |
63 | 63 | import static org.mockito.Mockito.any; |
64 | 64 | import static org.mockito.Mockito.doAnswer; |
| 65 | +import static org.mockito.Mockito.doThrow; |
65 | 66 | import static org.mockito.Mockito.mock; |
66 | 67 | import static org.mockito.Mockito.when; |
67 | 68 |
|
@@ -112,6 +113,50 @@ public void testCreateSender_CanCallStartMultipleTimes() throws Exception { |
112 | 113 | } |
113 | 114 | } |
114 | 115 |
|
| 116 | + public void testStart_ThrowsException_WhenAnErrorOccurs() throws IOException { |
| 117 | + var mockManager = mock(HttpClientManager.class); |
| 118 | + when(mockManager.getHttpClient()).thenReturn(mock(HttpClient.class)); |
| 119 | + doThrow(new Error("failed")).when(mockManager).start(); |
| 120 | + |
| 121 | + var senderFactory = new HttpRequestSender.Factory( |
| 122 | + ServiceComponentsTests.createWithEmptySettings(threadPool), |
| 123 | + mockManager, |
| 124 | + mockClusterServiceEmpty() |
| 125 | + ); |
| 126 | + |
| 127 | + try (var sender = senderFactory.createSender()) { |
| 128 | + // Checking for both exception types because there's a race condition between the Error being thrown on a separate thread |
| 129 | + // and the startCompleted latch timing out waiting for the start to complete |
| 130 | + var exception = expectThrowsAnyOf(List.of(Error.class, IllegalStateException.class), sender::startSynchronously); |
| 131 | + |
| 132 | + if (exception instanceof Error) { |
| 133 | + assertThat(exception.getMessage(), is("failed")); |
| 134 | + } else { |
| 135 | + // IllegalStateException can be thrown if the startCompleted latch times out waiting for the start to complete |
| 136 | + assertThat(exception.getMessage(), is("Http sender startup did not complete in time")); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public void testStart_ThrowsExceptionWaitingForStartToComplete() throws IOException { |
| 142 | + var mockManager = mock(HttpClientManager.class); |
| 143 | + when(mockManager.getHttpClient()).thenReturn(mock(HttpClient.class)); |
| 144 | + // This won't get rethrown because it is not an Error |
| 145 | + doThrow(new IllegalArgumentException("failed")).when(mockManager).start(); |
| 146 | + |
| 147 | + var senderFactory = new HttpRequestSender.Factory( |
| 148 | + ServiceComponentsTests.createWithEmptySettings(threadPool), |
| 149 | + mockManager, |
| 150 | + mockClusterServiceEmpty() |
| 151 | + ); |
| 152 | + |
| 153 | + try (var sender = senderFactory.createSender()) { |
| 154 | + var exception = expectThrows(IllegalStateException.class, sender::startSynchronously); |
| 155 | + |
| 156 | + assertThat(exception.getMessage(), is("Http sender startup did not complete in time")); |
| 157 | + } |
| 158 | + } |
| 159 | + |
115 | 160 | public void testCreateSender_CanCallStartAsyncMultipleTimes() throws Exception { |
116 | 161 | var asyncCalls = 3; |
117 | 162 | var senderFactory = new HttpRequestSender.Factory(createWithEmptySettings(threadPool), clientManager, mockClusterServiceEmpty()); |
|
0 commit comments