GTEST模拟功能如何模仿两个功能之间的比赛?
我有一个称为调整大小的函数,该功能会更改共享对象的大小。我有另一个称为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是将您的功能包裹在类
sizewrapper
中,并引入一个私人变量,例如resize_delay _
,模仿您想要的延迟并在构造函数中分配给它。然后,您可以将
sizewrapper
实例化,以使您不想要该延迟的情况以及要插入延迟的情况的正数。请参阅下面:
请参见此实时示例: https://godbolt.org/z/yq9pmokds
One way to do this is to wrap your functions inside a class
SizeWrapper
and introduce a private variable likeresize_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:
See this live example: https://godbolt.org/z/Yq9PMoKds