Skip to content

Commit 963da43

Browse files
committed
[TEST] remove endless wait in RestClientTests (#30776)
This commit adds a max wait timeout of one second to all the latch.await calls made in RestClientTests. It also makes clearer that the `onSuccess` listener method will never be called given that the underlying http client is mocked and makes sure that `latch.countDown` is always called
1 parent 67182f9 commit 963da43

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

client/rest/src/test/java/org/elasticsearch/client/RestClientTests.java

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import java.net.URI;
2828
import java.util.Collections;
2929
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.TimeUnit;
3031

3132
import static org.elasticsearch.client.RestClientTestUtil.getHttpMethods;
3233
import static org.hamcrest.Matchers.instanceOf;
3334
import static org.junit.Assert.assertEquals;
3435
import static org.junit.Assert.assertThat;
36+
import static org.junit.Assert.assertTrue;
3537
import static org.junit.Assert.fail;
3638
import static org.mockito.Mockito.mock;
3739
import static org.mockito.Mockito.times;
@@ -57,17 +59,20 @@ public void testPerformAsyncWithUnsupportedMethod() throws Exception {
5759
restClient.performRequestAsync(new Request("unsupported", randomAsciiLettersOfLength(5)), new ResponseListener() {
5860
@Override
5961
public void onSuccess(Response response) {
60-
fail("should have failed because of unsupported method");
62+
throw new UnsupportedOperationException("onSuccess cannot be called when using a mocked http client");
6163
}
6264

6365
@Override
6466
public void onFailure(Exception exception) {
65-
assertThat(exception, instanceOf(UnsupportedOperationException.class));
66-
assertEquals("http method not supported: unsupported", exception.getMessage());
67-
latch.countDown();
67+
try {
68+
assertThat(exception, instanceOf(UnsupportedOperationException.class));
69+
assertEquals("http method not supported: unsupported", exception.getMessage());
70+
} finally {
71+
latch.countDown();
72+
}
6873
}
6974
});
70-
latch.await();
75+
assertTrue("time out waiting for request to return", latch.await(1000, TimeUnit.MILLISECONDS));
7176
}
7277
}
7378

@@ -81,17 +86,20 @@ public void testPerformAsyncOldStyleWithUnsupportedMethod() throws Exception {
8186
restClient.performRequestAsync("unsupported", randomAsciiLettersOfLength(5), new ResponseListener() {
8287
@Override
8388
public void onSuccess(Response response) {
84-
fail("should have failed because of unsupported method");
89+
throw new UnsupportedOperationException("onSuccess cannot be called when using a mocked http client");
8590
}
8691

8792
@Override
8893
public void onFailure(Exception exception) {
89-
assertThat(exception, instanceOf(UnsupportedOperationException.class));
90-
assertEquals("http method not supported: unsupported", exception.getMessage());
91-
latch.countDown();
94+
try {
95+
assertThat(exception, instanceOf(UnsupportedOperationException.class));
96+
assertEquals("http method not supported: unsupported", exception.getMessage());
97+
} finally {
98+
latch.countDown();
99+
}
92100
}
93101
});
94-
latch.await();
102+
assertTrue("time out waiting for request to return", latch.await(1000, TimeUnit.MILLISECONDS));
95103
}
96104
}
97105

@@ -105,17 +113,20 @@ public void testPerformOldStyleAsyncWithNullParams() throws Exception {
105113
restClient.performRequestAsync(randomAsciiLettersOfLength(5), randomAsciiLettersOfLength(5), null, new ResponseListener() {
106114
@Override
107115
public void onSuccess(Response response) {
108-
fail("should have failed because of null parameters");
116+
throw new UnsupportedOperationException("onSuccess cannot be called when using a mocked http client");
109117
}
110118

111119
@Override
112120
public void onFailure(Exception exception) {
113-
assertThat(exception, instanceOf(NullPointerException.class));
114-
assertEquals("parameters cannot be null", exception.getMessage());
115-
latch.countDown();
121+
try {
122+
assertThat(exception, instanceOf(NullPointerException.class));
123+
assertEquals("parameters cannot be null", exception.getMessage());
124+
} finally {
125+
latch.countDown();
126+
}
116127
}
117128
});
118-
latch.await();
129+
assertTrue("time out waiting for request to return", latch.await(1000, TimeUnit.MILLISECONDS));
119130
}
120131
}
121132

@@ -129,18 +140,21 @@ public void testPerformOldStyleAsyncWithNullHeaders() throws Exception {
129140
ResponseListener listener = new ResponseListener() {
130141
@Override
131142
public void onSuccess(Response response) {
132-
fail("should have failed because of null headers");
143+
throw new UnsupportedOperationException("onSuccess cannot be called when using a mocked http client");
133144
}
134145

135146
@Override
136147
public void onFailure(Exception exception) {
137-
assertThat(exception, instanceOf(NullPointerException.class));
138-
assertEquals("header cannot be null", exception.getMessage());
139-
latch.countDown();
148+
try {
149+
assertThat(exception, instanceOf(NullPointerException.class));
150+
assertEquals("header cannot be null", exception.getMessage());
151+
} finally {
152+
latch.countDown();
153+
}
140154
}
141155
};
142156
restClient.performRequestAsync("GET", randomAsciiLettersOfLength(5), listener, (Header) null);
143-
latch.await();
157+
assertTrue("time out waiting for request to return", latch.await(1000, TimeUnit.MILLISECONDS));
144158
}
145159
}
146160

@@ -150,17 +164,20 @@ public void testPerformAsyncWithWrongEndpoint() throws Exception {
150164
restClient.performRequestAsync(new Request("GET", "::http:///"), new ResponseListener() {
151165
@Override
152166
public void onSuccess(Response response) {
153-
fail("should have failed because of wrong endpoint");
167+
throw new UnsupportedOperationException("onSuccess cannot be called when using a mocked http client");
154168
}
155169

156170
@Override
157171
public void onFailure(Exception exception) {
158-
assertThat(exception, instanceOf(IllegalArgumentException.class));
159-
assertEquals("Expected scheme name at index 0: ::http:///", exception.getMessage());
160-
latch.countDown();
172+
try {
173+
assertThat(exception, instanceOf(IllegalArgumentException.class));
174+
assertEquals("Expected scheme name at index 0: ::http:///", exception.getMessage());
175+
} finally {
176+
latch.countDown();
177+
}
161178
}
162179
});
163-
latch.await();
180+
assertTrue("time out waiting for request to return", latch.await(1000, TimeUnit.MILLISECONDS));
164181
}
165182
}
166183

@@ -174,17 +191,20 @@ public void testPerformAsyncOldStyleWithWrongEndpoint() throws Exception {
174191
restClient.performRequestAsync("GET", "::http:///", new ResponseListener() {
175192
@Override
176193
public void onSuccess(Response response) {
177-
fail("should have failed because of wrong endpoint");
194+
throw new UnsupportedOperationException("onSuccess cannot be called when using a mocked http client");
178195
}
179196

180197
@Override
181198
public void onFailure(Exception exception) {
182-
assertThat(exception, instanceOf(IllegalArgumentException.class));
183-
assertEquals("Expected scheme name at index 0: ::http:///", exception.getMessage());
184-
latch.countDown();
199+
try {
200+
assertThat(exception, instanceOf(IllegalArgumentException.class));
201+
assertEquals("Expected scheme name at index 0: ::http:///", exception.getMessage());
202+
} finally {
203+
latch.countDown();
204+
}
185205
}
186206
});
187-
latch.await();
207+
assertTrue("time out waiting for request to return", latch.await(1000, TimeUnit.MILLISECONDS));
188208
}
189209
}
190210

0 commit comments

Comments
 (0)