GTEST模拟功能如何模仿两个功能之间的比赛?

发布于 2025-02-10 11:27:35 字数 531 浏览 1 评论 0原文

我有一个称为调整大小的函数,该功能会更改共享对象的大小。我有另一个称为getize的函数,该功能获得该共享对象的大小。我想在需要延迟特定的调整大小的延迟的地方介绍一场竞赛,而我想调整大小的特定点才能睡觉,并在该特定时间进行调用并获得运行。有什么办法可以使用GTEST模拟设施来实现这一目标?

Resize(int newsize) {
  resizeint(newsize);
  // Want to introduce delay here and call Getsize().
  // This will simulate cache inconsistency.
  // Getsize() will return old size even though shared object is resized.
  writenewsizetocache();
}
int Getsize() {
  int size = readsizefromlocalcahce();
  return size;
}

I have a function called resize which changes the size of a shared object. I have another function called getsize which gets the size of that shared object. I want to introduce a race between resize and getsize where I need a delay in exactly a particular line of resize and that particular point I want resize to sleep and getsize to be called and getsize to run at that particular time. Is there any way I can achieve this using gtest MOCK facility?

Resize(int newsize) {
  resizeint(newsize);
  // Want to introduce delay here and call Getsize().
  // This will simulate cache inconsistency.
  // Getsize() will return old size even though shared object is resized.
  writenewsizetocache();
}
int Getsize() {
  int size = readsizefromlocalcahce();
  return size;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2025-02-17 11:27:35

一种方法是将您的功能包裹在类sizewrapper中,并引入一个私人变量,例如resize_delay _,模仿您想要的延迟并在构造函数中分配给它。

然后,您可以将sizewrapper实例化,以使您不想要该延迟的情况以及要插入延迟的情况的正数。

请参阅下面:


// Global size, representing the cache.
// Normally, this should be atomic or protected by mutex.
int global_size;

class SizeWrapper {
 private:
  void resizeint(int newsize) { temporary_size_ = newsize; }
  void writenewsizetocache() { global_size = temporary_size_; }
  int readsizefromlocalcahce() { return global_size; }
  int temporary_size_;
  int resize_delay_;

 public:
  SizeWrapper(int resize_delay)
      : resize_delay_(resize_delay), temporary_size_(0) {}
  void Resize(int newsize) {
    resizeint(newsize);
    // Want to introduce delay here and call Getsize().
    // This will simulate cache inconsistency.
    // Getsize() will return old size even though shared object is resized.
    if (resize_delay_ > 0) {
      std::this_thread::sleep_for(std::chrono::milliseconds(resize_delay_));
    }

    writenewsizetocache();
  }

  int Getsize() {
    int size;
    size = readsizefromlocalcahce();
    return size;
  }
};

TEST(SizeWrapperTest, GetSizeBeforeResize) {
  global_size = 0;

  int resize_write_value = 10;
  int getsize_read_value = 0;

  // getsize delay is larger than resize delay.
  const int resize_delay = 100;
  const int getsize_delay = 200;

  SizeWrapper size_wrapper(resize_delay);

  auto t1 = std::thread([&size_wrapper, resize_write_value] {
    size_wrapper.Resize(resize_write_value);
  });

  auto t2 = std::thread([&size_wrapper, &getsize_read_value, getsize_delay] {
    // Read the size with some delay.
    std::this_thread::sleep_for(std::chrono::milliseconds(getsize_delay));
    getsize_read_value = size_wrapper.Getsize();
  });

  t1.join();
  t2.join();

  // Thre read and write values are equal.
  EXPECT_EQ(getsize_read_value, resize_write_value);
}

TEST(SizeWrapperTest, GetSizeAfterResize) {
  global_size = 0;

  int resize_write_value = 10;
  int getsize_read_value = 0;

  // getsize delay is smaller than resize delay.
  const int resize_delay = 200;
  const int getsize_delay = 100;

  SizeWrapper size_wrapper(resize_delay);

  auto t1 = std::thread([&size_wrapper, resize_write_value] {
    size_wrapper.Resize(resize_write_value);
  });

  auto t2 = std::thread([&size_wrapper, &getsize_read_value, getsize_delay] {
    std::this_thread::sleep_for(std::chrono::milliseconds(getsize_delay));
    getsize_read_value = size_wrapper.Getsize();
  });

  t1.join();
  t2.join();

  // Thre read and write values are NOT equal.
  EXPECT_NE(getsize_read_value, resize_write_value);
}

请参见此实时示例: https://godbolt.org/z/yq9pmokds

One way to do this is to wrap your functions inside a class SizeWrapper and introduce a private variable like resize_delay_ that mimics the delay that you want and assign to it in the constructor.

You can then instantiate SizeWrapper with 0 for the case that you don't want that delay and with a positive number for the case that you want to insert the delay.

See below:


// Global size, representing the cache.
// Normally, this should be atomic or protected by mutex.
int global_size;

class SizeWrapper {
 private:
  void resizeint(int newsize) { temporary_size_ = newsize; }
  void writenewsizetocache() { global_size = temporary_size_; }
  int readsizefromlocalcahce() { return global_size; }
  int temporary_size_;
  int resize_delay_;

 public:
  SizeWrapper(int resize_delay)
      : resize_delay_(resize_delay), temporary_size_(0) {}
  void Resize(int newsize) {
    resizeint(newsize);
    // Want to introduce delay here and call Getsize().
    // This will simulate cache inconsistency.
    // Getsize() will return old size even though shared object is resized.
    if (resize_delay_ > 0) {
      std::this_thread::sleep_for(std::chrono::milliseconds(resize_delay_));
    }

    writenewsizetocache();
  }

  int Getsize() {
    int size;
    size = readsizefromlocalcahce();
    return size;
  }
};

TEST(SizeWrapperTest, GetSizeBeforeResize) {
  global_size = 0;

  int resize_write_value = 10;
  int getsize_read_value = 0;

  // getsize delay is larger than resize delay.
  const int resize_delay = 100;
  const int getsize_delay = 200;

  SizeWrapper size_wrapper(resize_delay);

  auto t1 = std::thread([&size_wrapper, resize_write_value] {
    size_wrapper.Resize(resize_write_value);
  });

  auto t2 = std::thread([&size_wrapper, &getsize_read_value, getsize_delay] {
    // Read the size with some delay.
    std::this_thread::sleep_for(std::chrono::milliseconds(getsize_delay));
    getsize_read_value = size_wrapper.Getsize();
  });

  t1.join();
  t2.join();

  // Thre read and write values are equal.
  EXPECT_EQ(getsize_read_value, resize_write_value);
}

TEST(SizeWrapperTest, GetSizeAfterResize) {
  global_size = 0;

  int resize_write_value = 10;
  int getsize_read_value = 0;

  // getsize delay is smaller than resize delay.
  const int resize_delay = 200;
  const int getsize_delay = 100;

  SizeWrapper size_wrapper(resize_delay);

  auto t1 = std::thread([&size_wrapper, resize_write_value] {
    size_wrapper.Resize(resize_write_value);
  });

  auto t2 = std::thread([&size_wrapper, &getsize_read_value, getsize_delay] {
    std::this_thread::sleep_for(std::chrono::milliseconds(getsize_delay));
    getsize_read_value = size_wrapper.Getsize();
  });

  t1.join();
  t2.join();

  // Thre read and write values are NOT equal.
  EXPECT_NE(getsize_read_value, resize_write_value);
}

See this live example: https://godbolt.org/z/Yq9PMoKds

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文