Home > Back-end >  Why does this test fail if someone else runs it at the same time?
Why does this test fail if someone else runs it at the same time?

Time:02-11

I was watching a conference talk (No need to watch it to understand my question but if you're curious it's from 35m28s to 36m28s). The following test was shown:

TEST(Foo, StorageTest){
  StorageServer* server = GetStorageServerHandle(); 
  auto my_val = rand();
  server -> Store("testKey", my_val); 
  EXPECT_EQ(my_val, server->Load("testKey")); 
}
  1. One of the speakers said: "you can only expect that storing data to a production service works if only one copy of that test is running at a time."

  2. The other speaker said: "Once you add continuous integration in the mix, the test starts failing". I don't know a lot about CI/CD.

I am having some trouble understanding both claims 1. and 2. Any help?

CodePudding user response:

One of the speakers said: "you can only expect that storing data to a production service works if only one copy of that test is running at a time."

Right. Imagine if two instances of this code are running. If both Store operations execute before either Load operation takes place, the one whose Store executed first will load the wrong value.

Consider this pattern where the two instances are called "first" and "second":

  1. First Store executes, stores first random value.
  2. Second Store starts executing, starts storing second random value.
  3. First Load is blocked on the second Store completing due to a lock internal to the database
  4. Second Load is blocked on the Store completing due to a local internal to the database.
  5. Second Store finishes and release the internal lock.
  6. First Load can now execute, it gets second random value.
  7. EXPECT_EQ fails as the first and second random values are different.

The other speaker said: "Once you add continuous integration in the mix, the test starts failing".

If a CI system is testing multiple instances of the code at the same time, race conditions like the example above can occur and cause tests to fail as the multiple instances race with each other.

  • Related