Joh*_*nes 7 c++ unit-testing systemtime c++11 c++-chrono
我依赖可能会或可能不会响应的硬件.因此,我经常最终编写具有超时功能.系统时间是脆性单元测试的已知来源,因此注入受控且稳定的时间似乎是测试的好主意.
我想知道std :: chrono中是否有任何设施可以帮助解决这个问题.我看到的替代方法是围绕系统时间编写一个包装器并依赖于该适配器.
以下是包装器外观的最小示例.
#pragma once
#include <memory>
#include <chrono>
#include <thread>
#include <iostream>
using std::chrono::system_clock;
using std::chrono::milliseconds;
using std::shared_ptr;
using std::make_shared;
class Wrapped_Clock
{
public:
virtual system_clock::time_point Now() { return system_clock::now(); }
virtual void Sleep(milliseconds ms) { std::this_thread::sleep_for(ms); }
};
class Mock_Clock : public Wrapped_Clock
{
private:
system_clock::time_point now;
public:
Mock_Clock() : now(system_clock::now()){}
~Mock_Clock() {}
system_clock::time_point Now() { return now; }
void Sleep(milliseconds ms) { }
};
class CanTimeOut
{
private:
shared_ptr<Wrapped_Clock> sclock;
public:
CanTimeOut(shared_ptr<Wrapped_Clock> sclock = make_shared<Wrapped_Clock>()) : sclock(sclock) {}
~CanTimeOut() {}
milliseconds TimeoutAction(milliseconds maxtime)
{
using std::chrono::duration_cast;
int x = 0;
system_clock::time_point start = sclock->Now();
system_clock::time_point timeout = sclock->Now() + maxtime;
while (timeout > sclock->Now() && x != 2000)
{
sclock->Sleep(milliseconds(1));
++x;
}
milliseconds elapsed = duration_cast<milliseconds>(sclock->Now() - start);
return elapsed;
}
};
#define EXPECT_GE(left, right, test) \
{ if (!(left >= right)) { \
std::cout << #test << " " << "!(" << left << " >= " << right << ")" << std::endl; \
} }
#define EXPECT_EQ(expected, actual, test) \
{ if (!(expected == actual)) { \
std::cout << #test << " " << "!(" << expected << " == " << actual << ")" << std::endl; \
} }
void TestWithSystemClock()
{
CanTimeOut cto;
long long timeout = 1000;
milliseconds actual = cto.TimeoutAction(milliseconds(timeout));
EXPECT_GE(actual.count(), timeout, TestWithSystemClock);
}
void TestWithMockClock()
{
CanTimeOut cto(make_shared<Mock_Clock>());
milliseconds actual = cto.TimeoutAction(milliseconds(1000));
EXPECT_EQ(0, actual.count(), TestWithMockClock);
}
int main()
{
TestWithSystemClock();
TestWithMockClock();
}
Run Code Online (Sandbox Code Playgroud)
用std :: chrone的功能可以取代多少?
std::
设施的差异.相反,它看起来像是在嘲笑std::this_thread::sleep
.
这有点棘手,因为它是一个只有免费功能的命名空间.为测试目的,很难"注入"命名空间.实际上,您应该使用您自己的类型将该命名空间中的函数包装起来.
我使用静态依赖注入,àlaC++:
#include <memory>
#include <chrono>
#include <thread>
#include <iostream>
using std::chrono::system_clock;
using std::chrono::milliseconds;
struct production {
using clock = std::chrono::system_clock;
struct this_thread {
template<typename... A> static auto sleep_for(A&&... a) { return std::this_thread::sleep_for(std::forward<A>(a)...); }
template<typename... A> static auto sleep_until(A&&... a) { return std::this_thread::sleep_until(std::forward<A>(a)...); }
};
};
struct mock {
struct clock : std::chrono::system_clock {
using base_type = std::chrono::system_clock;
static time_point now() { static auto onetime = base_type::now(); return onetime; }
};
struct this_thread {
template<typename... A> static auto sleep_for(A&&... a) {}
template<typename... A> static auto sleep_until(A&&... a) {}
};
};
template <typename services = production,
typename clock = typename services::clock,
typename this_thread = typename services::this_thread>
class CanTimeOut
{
public:
milliseconds TimeoutAction(milliseconds maxtime)
{
using std::chrono::duration_cast;
int x = 0;
auto start = clock::now();
auto timeout = clock::now() + maxtime;
while (timeout > clock::now() && x != 2000)
{
this_thread::sleep_for(milliseconds(1));
++x;
}
milliseconds elapsed = duration_cast<milliseconds>(clock::now() - start);
return elapsed;
}
};
#define EXPECT_GE(left, right, test) \
{ if (!(left >= right)) { \
std::cout << #test << " " << "!(" << left << " >= " << right << ")" << std::endl; \
} }
#define EXPECT_EQ(expected, actual, test) \
{ if (!(expected == actual)) { \
std::cout << #test << " " << "!(" << expected << " == " << actual << ")" << std::endl; \
} }
void TestWithSystemClock()
{
CanTimeOut<> cto;
long long timeout = 1000;
milliseconds actual = cto.TimeoutAction(milliseconds(timeout));
EXPECT_GE(actual.count(), timeout, TestWithSystemClock);
}
void TestWithMockClock()
{
CanTimeOut<mock> cto;
milliseconds actual = cto.TimeoutAction(milliseconds(1000));
EXPECT_EQ(0, actual.count(), TestWithMockClock);
}
int main()
{
TestWithSystemClock();
TestWithMockClock();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2381 次 |
最近记录: |