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"));
}
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."
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":
- First
Store
executes, stores first random value. - Second
Store
starts executing, starts storing second random value. - First
Load
is blocked on the secondStore
completing due to a lock internal to the database - Second
Load
is blocked on theStore
completing due to a local internal to the database. - Second
Store
finishes and release the internal lock. - First
Load
can now execute, it gets second random value. 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.