I am writing some JUnit unit tests to test my DynamoDB data accessor object. Here is one of the test.
....
private static DynamoDBMapper ddbMapper;
@BeforeClass
public static void setup() {
ddbMapper = DynamoDBClients.getTestingDynamoDBMapper();
}
@Before
public void setupTestItem() {
Item item = new Item();
item.setItemID(TEST_COUTSE_ITEM_ID);
// Create an item with every fields populated
ddbMapper.save(item);
}
@Test
public void test_update_simple_attribute() {
Item item = ddbMapper.load(Item.class, TEST_ITEM_ID, TEST_ITEM_VARIANT);
item.setLanguageTag(TEST_LANGUAGE_TAG + "CHANGED");
ddbMapper.save(item);
Item updatedItem = ddbMapper.load(Item.class, TEST_ITEM_ID, TEST_ITEM_VARIANT);
assertEquals(updatedItem.getLanguageTag(), TEST_LANGUAGE_TAG + "CHANGED"); // This field has been changed
}
I have more tests that will update the item, and assert whether the update got pushed through to DynamoDB, and I could read it back.
However I noticed that if I run these tests more often, I sometime ran into issue that DynamoDB have not yet fully write the updated data in, and when I load it, it is still showing the old data. Rerunning the tests usually solve the issue.
I believe that DynamoDB uses eventual consistency model for write, so it makes sense that sometime update might take a bit longer than the Java execution speed. One way I could mitigate this is to have the JUnit test to suspend for 100ms or so.
But I would have to include some code to suspend execution everywhere I do a ddbMapper.save() or ddbMapper.delete(), that doesn't seem feasible for all the tests I have and the tests I will write.
It seems like I could tackle this with an annotation driven approach. Perhaps implementing a class level annotation @SuspendAtEndOfMethod and all its method could get affected, I am wondering if this is possible?