Skip to content

Commit 42e44fe

Browse files
authored
sleep when query comes up empty
1 parent f15dbe9 commit 42e44fe

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/searchableencryption/BasicSearchableEncryptionExample.java

+23-10
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,29 @@ public static void PutItemQueryItemWithBeacon(String ddbTableName, String branch
318318
.expressionAttributeValues(expressionAttributeValues)
319319
.build();
320320

321-
final QueryResponse queryResponse = ddb.query(queryRequest);
322-
List<Map<String, AttributeValue>> attributeValues = queryResponse.items();
323-
// Validate query was returned successfully
324-
assert 200 == queryResponse.sdkHttpResponse().statusCode();
325-
// Validate only 1 item was returned: the item we just put
326-
assert attributeValues.size() == 1;
327-
final Map<String, AttributeValue> returnedItem = attributeValues.get(0);
328-
// Validate the item has the expected attributes
329-
assert returnedItem.get("inspector_id_last4").s().equals("4321");
330-
assert returnedItem.get("unit").s().equals("123456789012");
321+
// GSIs do not update instantly
322+
// so if the results come back empty
323+
// we retry after a short sleep
324+
for (int i=0; i<10; ++i) {
325+
final QueryResponse queryResponse = ddb.query(queryRequest);
326+
List<Map<String, AttributeValue>> attributeValues = queryResponse.items();
327+
// Validate query was returned successfully
328+
assert 200 == queryResponse.sdkHttpResponse().statusCode();
329+
330+
// if no results, sleep and try again
331+
if (attributeValues.size() == 0) {
332+
try {Thread.sleep(20);} catch (Exception e) {}
333+
continue;
334+
}
335+
336+
// Validate only 1 item was returned: the item we just put
337+
assert attributeValues.size() == 1;
338+
final Map<String, AttributeValue> returnedItem = attributeValues.get(0);
339+
// Validate the item has the expected attributes
340+
assert returnedItem.get("inspector_id_last4").s().equals("4321");
341+
assert returnedItem.get("unit").s().equals("123456789012");
342+
break;
343+
}
331344
}
332345

333346
public static void main(final String[] args) {

0 commit comments

Comments
 (0)