如果其他人同时运行此测试,为什么它会失败?

Why does this test fail if someone else runs it at the same time?

我正在观看 conference talk(无需观看即可理解我的问题,但如果您好奇,它是从 35 分 28 秒到 36 分 28 秒)。显示了以下测试:

TEST(Foo, StorageTest){
  StorageServer* server = GetStorageServerHandle(); 
  auto my_val = rand();
  server -> Store("testKey", my_val); 
  EXPECT_EQ(my_val, server->Load("testKey")); 
}
  1. 其中一位发言者说:“只有一次 运行 该测试的一个副本,您才能期望将数据存储到生产服务中。”

  2. 另一位演讲者说:“一旦你在混合中添加持续集成,测试就会开始失败”。我对 CI/CD.

    了解不多

我在理解索赔 1 和索赔 2 时遇到一些问题。有什么帮助吗?

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."

没错。想象一下,如果此代码的两个实例是 运行。如果两个 Store 操作都在任一 Load 操作发生之前执行,则先执行 Store 的操作将加载错误的值。

考虑这种模式,其中两个实例称为“第一个”和“第二个”:

  1. 首先 Store 执行,存储第一个随机值。
  2. 第二个Store开始执行,开始存储第二个随机值。
  3. 由于数据库内部锁定,第一个 Load 在第二个 Store 完成时被阻止
  4. 由于数据库的本地内部,第二个 LoadStore 完成时被阻止。
  5. 第二个 Store 完成并释放内部锁。
  6. 第一个 Load 现在可以执行,它获得第二个随机值。
  7. EXPECT_EQ 失败,因为第一个和第二个随机值不同。

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

如果 CI 系统同时测试代码的多个实例,可能会出现如上例所示的竞争条件,并导致测试失败,因为多个实例相互竞争。