99import org .junit .Test ;
1010import static org .junit .Assert .*;
1111
12+ import software .amazon .awssdk .crt .CrtResource ;
1213import software .amazon .awssdk .crt .io .*;
1314import software .amazon .awssdk .crt .mqtt5 .Mqtt5Client ;
1415import software .amazon .awssdk .crt .mqtt5 .Mqtt5ClientOptions .LifecycleEvents ;
2930
3031/* For environment variable setup, see SetupCrossCICrtEnvironment in the CRT builder */
3132public 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};
0 commit comments