0

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?

  • 1
    A better approach would be to somehow programmatically ensure that the DynamoDB has finished writing, if that is indeed the cause. "I believe" is not a good starting point when programming: you need to know for certain. – SeverityOne Apr 12 '18 at 18:21
  • @SeverityOne It seems like I found something about using strongly consistent read in the DynamoDB [FAQ](https://aws.amazon.com/dynamodb/faqs/), by default it is using eventual consistency read. Perhaps I would need to switch to that. However, I am still interested in whether such annotation is possible. – Kuangyou Yao Apr 12 '18 at 18:28
  • Annotations are a bit like wallpaper. They don't actually add functionality to your code; they simply enable other code to understand members, methods or classes better. So you would need some sort of agent that sits between your test classes and whatever you use to run them (such as an IDE). – SeverityOne Apr 12 '18 at 18:35
  • You could probably find an annotation processor which lets you manipulate the generated code. In fact, here's one: https://stackoverflow.com/a/13980086/996081 – cbr Apr 12 '18 at 18:37

0 Answers0