Skip to content

Commit 0b75987

Browse files
author
Bret Ambrose
committed
All MQTT tests that require a successful connection now retry-wrapped
1 parent c9e48ba commit 0b75987

14 files changed

Lines changed: 1130 additions & 915 deletions

src/test/java/software/amazon/awssdk/crt/test/CustomKeyOpsTest.java

Lines changed: 83 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.Test;
1010
import static org.junit.Assert.*;
1111

12+
import software.amazon.awssdk.crt.CrtResource;
1213
import software.amazon.awssdk.crt.io.*;
1314
import software.amazon.awssdk.crt.mqtt5.Mqtt5Client;
1415
import software.amazon.awssdk.crt.mqtt5.Mqtt5ClientOptions.LifecycleEvents;
@@ -29,6 +30,9 @@
2930

3031
/* For environment variable setup, see SetupCrossCICrtEnvironment in the CRT builder */
3132
public class CustomKeyOpsTest extends MqttClientConnectionFixture {
33+
private final static int MAX_TEST_RETRIES = 3;
34+
private final static int TEST_RETRY_SLEEP_MILLIS = 2000;
35+
3236
public CustomKeyOpsTest() {
3337
}
3438

@@ -78,7 +82,7 @@ static class TestKeyOperationHandler implements TlsKeyOperationHandler {
7882
}
7983

8084
public void performOperation(TlsKeyOperation operation) {
81-
if (this.throwException == true) {
85+
if (this.throwException) {
8286
throw new RuntimeException("Test Exception!");
8387
}
8488

@@ -116,44 +120,52 @@ public void performOperation(TlsKeyOperation operation) {
116120

117121
operation.complete(signatureBytes);
118122

119-
if (this.performExtraComplete == true) {
123+
if (this.performExtraComplete) {
120124
operation.complete(signatureBytes);
121125
}
122126

123127
} catch (Exception ex) {
124128
operation.completeExceptionally(ex);
125129

126-
if (this.performExtraComplete == true) {
130+
if (this.performExtraComplete) {
127131
operation.completeExceptionally(ex);
128132
}
129133
}
130134
}
131135
}
132136

137+
private void doHappyPathTest() {
138+
TestKeyOperationHandler myKeyOperationHandler = new TestKeyOperationHandler(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY, false, false);
139+
TlsContextCustomKeyOperationOptions keyOperationOptions = new TlsContextCustomKeyOperationOptions(myKeyOperationHandler);
140+
keyOperationOptions.withCertificateFilePath(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT);
141+
142+
try (TlsContextOptions contextOptions = TlsContextOptions.createWithMtlsCustomKeyOperations(keyOperationOptions);
143+
TlsContext context = new TlsContext(contextOptions)) {
144+
connectDirect(
145+
context,
146+
AWS_TEST_MQTT311_IOT_CORE_HOST,
147+
8883,
148+
null,
149+
null,
150+
null);
151+
disconnect();
152+
} catch (Exception ex) {
153+
throw new RuntimeException(ex);
154+
} finally {
155+
close();
156+
}
157+
}
158+
133159
@Test
134-
public void testHappyPath()
135-
{
160+
public void testHappyPath() throws Exception {
136161
skipIfNetworkUnavailable();
137162
Assume.assumeNotNull(
138163
AWS_TEST_MQTT311_IOT_CORE_HOST, AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT,
139164
AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY);
140-
TestKeyOperationHandler myKeyOperationHandler = new TestKeyOperationHandler(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY, false, false);
141-
TlsContextCustomKeyOperationOptions keyOperationOptions = new TlsContextCustomKeyOperationOptions(myKeyOperationHandler);
142-
keyOperationOptions.withCertificateFilePath(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT);
143165

144-
try (TlsContextOptions contextOptions = TlsContextOptions.createWithMtlsCustomKeyOperations(keyOperationOptions);
145-
TlsContext context = new TlsContext(contextOptions);)
146-
{
147-
connectDirectWithConfig(
148-
context,
149-
AWS_TEST_MQTT311_IOT_CORE_HOST,
150-
8883,
151-
null,
152-
null,
153-
null);
154-
disconnect();
155-
close();
156-
}
166+
TestUtils.doRetryableTest(this::doHappyPathTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
167+
168+
CrtResource.waitForNoResources();
157169
}
158170

159171
// Not ideal, but I don't see the point of making a new file just for a single MQTT5 Custom Key Ops test.
@@ -175,23 +187,16 @@ public void onDisconnection(Mqtt5Client client, software.amazon.awssdk.crt.mqtt5
175187
public void onStopped(Mqtt5Client client, software.amazon.awssdk.crt.mqtt5.OnStoppedReturn onStoppedReturn) {}
176188
}
177189

178-
@Test
179-
public void testHappyPathMQTT5() {
180-
skipIfNetworkUnavailable();
181-
Assume.assumeNotNull(
182-
AWS_TEST_MQTT5_IOT_CORE_HOST, AWS_TEST_MQTT5_CUSTOM_KEY_OPS_CERT,
183-
AWS_TEST_MQTT5_CUSTOM_KEY_OPS_KEY);
190+
private void doHappyPathMQTT5Test() {
184191
try {
185192
TestKeyOperationHandler myKeyOperationHandler = new TestKeyOperationHandler(AWS_TEST_MQTT5_CUSTOM_KEY_OPS_KEY, false, false);
186193
TlsContextCustomKeyOperationOptions keyOperationOptions = new TlsContextCustomKeyOperationOptions(myKeyOperationHandler);
187194
keyOperationOptions.withCertificateFilePath(AWS_TEST_MQTT5_CUSTOM_KEY_OPS_CERT);
188195

189196
LifecycleEvents_Futured events = new LifecycleEvents_Futured();
190-
try (
191-
TlsContextOptions contextOptions = TlsContextOptions.createWithMtlsCustomKeyOperations(keyOperationOptions);
192-
TlsContext context = new TlsContext(contextOptions);
193-
) {
194-
Mqtt5ClientOptionsBuilder builder = new Mqtt5ClientOptionsBuilder(AWS_TEST_MQTT5_IOT_CORE_HOST, 8883l);
197+
try (TlsContextOptions contextOptions = TlsContextOptions.createWithMtlsCustomKeyOperations(keyOperationOptions);
198+
TlsContext context = new TlsContext(contextOptions)) {
199+
Mqtt5ClientOptionsBuilder builder = new Mqtt5ClientOptionsBuilder(AWS_TEST_MQTT5_IOT_CORE_HOST, 8883L);
195200
builder.withLifecycleEvents(events);
196201
builder.withTlsContext(context);
197202

@@ -202,95 +207,111 @@ public void testHappyPathMQTT5() {
202207
}
203208
}
204209
} catch (Exception ex) {
205-
fail(ex.getMessage());
210+
throw new RuntimeException(ex);
206211
}
207212
}
208213

209214
@Test
210-
public void testExceptionFailurePath()
211-
{
215+
public void testHappyPathMQTT5() throws Exception {
216+
skipIfNetworkUnavailable();
217+
Assume.assumeNotNull(
218+
AWS_TEST_MQTT5_IOT_CORE_HOST, AWS_TEST_MQTT5_CUSTOM_KEY_OPS_CERT,
219+
AWS_TEST_MQTT5_CUSTOM_KEY_OPS_KEY);
220+
221+
TestUtils.doRetryableTest(this::doHappyPathMQTT5Test, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
222+
223+
CrtResource.waitForNoResources();
224+
}
225+
226+
@Test
227+
public void testExceptionFailurePath() {
212228
skipIfNetworkUnavailable();
213229
Assume.assumeNotNull(
214230
AWS_TEST_MQTT311_IOT_CORE_HOST, AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT,
215231
AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY);
232+
216233
TestKeyOperationHandler myKeyOperationHandler = new TestKeyOperationHandler(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY, true, false);
217234
TlsContextCustomKeyOperationOptions keyOperationOptions = new TlsContextCustomKeyOperationOptions(myKeyOperationHandler);
218235
keyOperationOptions.withCertificateFilePath(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT);
219236

220237
try (TlsContextOptions contextOptions = TlsContextOptions.createWithMtlsCustomKeyOperations(keyOperationOptions);
221-
TlsContext context = new TlsContext(contextOptions);)
222-
{
223-
connectDirectWithConfigThrows(
238+
TlsContext context = new TlsContext(contextOptions)) {
239+
connectDirect(
224240
context,
225241
AWS_TEST_MQTT311_IOT_CORE_HOST,
226242
8883,
227243
null,
228244
null,
229245
null);
230-
}
231-
catch (Exception ex) {
232-
close();
246+
} catch (Exception ex) {
233247
return;
248+
} finally {
249+
close();
234250
}
235-
close();
251+
236252
fail("Connection did not fail!");
237253
}
238254

239-
@Test
240-
public void testExtraCompleteHappy() {
241-
skipIfNetworkUnavailable();
242-
Assume.assumeNotNull(
243-
AWS_TEST_MQTT311_IOT_CORE_HOST, AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT,
244-
AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY);
255+
private void doExtraCompleteHappyTest() {
245256
TestKeyOperationHandler myKeyOperationHandler = new TestKeyOperationHandler(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY, false, true);
246257
TlsContextCustomKeyOperationOptions keyOperationOptions = new TlsContextCustomKeyOperationOptions(myKeyOperationHandler);
247258
keyOperationOptions.withCertificateFilePath(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT);
248259

249260
try (TlsContextOptions contextOptions = TlsContextOptions.createWithMtlsCustomKeyOperations(keyOperationOptions);
250-
TlsContext context = new TlsContext(contextOptions);)
251-
{
252-
connectDirectWithConfig(
261+
TlsContext context = new TlsContext(contextOptions)) {
262+
connectDirect(
253263
context,
254264
AWS_TEST_MQTT311_IOT_CORE_HOST,
255265
8883,
256266
null,
257267
null,
258268
null);
259-
}
260-
catch (Exception ex) {
261-
fail("Exception during connect: " + ex.toString());
262-
} finally {
263269
disconnect();
270+
} catch (Exception ex) {
271+
throw new RuntimeException(ex);
272+
} finally {
264273
close();
265274
}
266275
}
267276

277+
@Test
278+
public void testExtraCompleteHappy() throws Exception {
279+
skipIfNetworkUnavailable();
280+
Assume.assumeNotNull(
281+
AWS_TEST_MQTT311_IOT_CORE_HOST, AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT,
282+
AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY);
283+
284+
TestUtils.doRetryableTest(this::doExtraCompleteHappyTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
285+
286+
CrtResource.waitForNoResources();
287+
}
288+
268289
@Test
269290
public void testExceptionExtraCompleteFailurePath() {
270291
skipIfNetworkUnavailable();
271292
Assume.assumeNotNull(
272293
AWS_TEST_MQTT311_IOT_CORE_HOST, AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT,
273294
AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY);
295+
274296
TestKeyOperationHandler myKeyOperationHandler = new TestKeyOperationHandler(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY, true, true);
275297
TlsContextCustomKeyOperationOptions keyOperationOptions = new TlsContextCustomKeyOperationOptions(myKeyOperationHandler);
276298
keyOperationOptions.withCertificateFilePath(AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT);
277299

278300
try (TlsContextOptions contextOptions = TlsContextOptions.createWithMtlsCustomKeyOperations(keyOperationOptions);
279-
TlsContext context = new TlsContext(contextOptions);)
280-
{
281-
connectDirectWithConfigThrows(
301+
TlsContext context = new TlsContext(contextOptions)) {
302+
connectDirect(
282303
context,
283304
AWS_TEST_MQTT311_IOT_CORE_HOST,
284305
8883,
285306
null,
286307
null,
287308
null);
288-
}
289-
catch (Exception ex) {
290-
close();
309+
} catch (Exception ex) {
291310
return;
311+
} finally {
312+
close();
292313
}
293-
close();
314+
294315
fail("Connection did not fail!");
295316
}
296317
};

src/test/java/software/amazon/awssdk/crt/test/Http2ClientConnectionTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testHttp2ConnectionGetVersion() throws Exception {
5757
skipIfAndroid();
5858
skipIfNetworkUnavailable();
5959

60-
TestUtils.doRetryableTest(() -> { this.doHttp2ConnectionGetVersionTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
60+
TestUtils.doRetryableTest(this::doHttp2ConnectionGetVersionTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
6161

6262
CrtResource.waitForNoResources();
6363
}
@@ -95,7 +95,7 @@ public void testHttp2ConnectionUpdateSettings() throws Exception {
9595
skipIfAndroid();
9696
skipIfNetworkUnavailable();
9797

98-
TestUtils.doRetryableTest(() -> { this.doHttp2ConnectionUpdateSettingsTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
98+
TestUtils.doRetryableTest(this::doHttp2ConnectionUpdateSettingsTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
9999

100100
CrtResource.waitForNoResources();
101101
}
@@ -130,7 +130,7 @@ public void testHttp2ConnectionUpdateSettingsEmpty() throws Exception {
130130
/* empty settings is allowed to send */
131131
skipIfNetworkUnavailable();
132132

133-
TestUtils.doRetryableTest(() -> { this.doHttp2ConnectionUpdateSettingsEmptyTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
133+
TestUtils.doRetryableTest(this::doHttp2ConnectionUpdateSettingsEmptyTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
134134

135135
CrtResource.waitForNoResources();
136136
}
@@ -167,7 +167,7 @@ public void testHttp2ConnectionPing() throws Exception {
167167
skipIfAndroid();
168168
skipIfNetworkUnavailable();
169169

170-
TestUtils.doRetryableTest(() -> { this.doHttp2ConnectionPingTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
170+
TestUtils.doRetryableTest(this::doHttp2ConnectionPingTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
171171

172172
CrtResource.waitForNoResources();
173173
}
@@ -211,7 +211,7 @@ public void testHttp2ConnectionPingExceptionPingDataLength() throws Exception {
211211
skipIfAndroid();
212212
skipIfNetworkUnavailable();
213213

214-
TestUtils.doRetryableTest(() -> { this.doHttp2ConnectionPingExceptionPingDataLengthTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
214+
TestUtils.doRetryableTest(this::doHttp2ConnectionPingExceptionPingDataLengthTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
215215

216216
CrtResource.waitForNoResources();
217217
}
@@ -249,7 +249,7 @@ public void testHttp2ConnectionSendGoAway() throws Exception {
249249
*/
250250
skipIfNetworkUnavailable();
251251

252-
TestUtils.doRetryableTest(() -> { this.doHttp2ConnectionSendGoAwayTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
252+
TestUtils.doRetryableTest(this::doHttp2ConnectionSendGoAwayTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
253253

254254
CrtResource.waitForNoResources();
255255
}
@@ -287,7 +287,7 @@ public void testHttp2ConnectionUpdateConnectionWindow() throws Exception {
287287
*/
288288
skipIfNetworkUnavailable();
289289

290-
TestUtils.doRetryableTest(() -> { this.doHttp2ConnectionUpdateConnectionWindowTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
290+
TestUtils.doRetryableTest(this::doHttp2ConnectionUpdateConnectionWindowTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
291291

292292
CrtResource.waitForNoResources();
293293
}

src/test/java/software/amazon/awssdk/crt/test/Http2RequestResponseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void testHttp2ResetStream() throws Exception {
224224
*/
225225
skipIfNetworkUnavailable();
226226

227-
TestUtils.doRetryableTest(() -> { this.doHttp2ResetStreamTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, 5, 2000);
227+
TestUtils.doRetryableTest(this::doHttp2ResetStreamTest, TestUtils::isRetryableTimeout, 5, 2000);
228228

229229
CrtResource.waitForNoResources();
230230
}

src/test/java/software/amazon/awssdk/crt/test/HttpRequestResponseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public void testHttpRequestUnActivated() throws Exception {
314314
skipIfAndroid();
315315
skipIfNetworkUnavailable();
316316

317-
TestUtils.doRetryableTest(() -> { this.doHttpRequestUnActivatedTest(); }, (ex) -> { return TestUtils.isRetryableTimeout(ex); }, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
317+
TestUtils.doRetryableTest(this::doHttpRequestUnActivatedTest, TestUtils::isRetryableTimeout, MAX_TEST_RETRIES, TEST_RETRY_SLEEP_MILLIS);
318318

319319
CrtResource.waitForNoResources();
320320
}

0 commit comments

Comments
 (0)