2015-11-12 22:08:43 +01:00
|
|
|
#include <google/gtest/gtest.h>
|
|
|
|
#include <messmer/cpp-utils/pointer/unique_ref.h>
|
|
|
|
#include "../../src/cli/CallAfterTimeout.h"
|
|
|
|
|
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using boost::chrono::milliseconds;
|
|
|
|
using boost::chrono::minutes;
|
|
|
|
using boost::chrono::duration_cast;
|
|
|
|
using boost::this_thread::sleep_for;
|
|
|
|
using namespace cryfs;
|
|
|
|
|
|
|
|
class CallAfterTimeoutTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
unique_ref<CallAfterTimeout> callAfterTimeout(milliseconds timeout) {
|
|
|
|
return make_unique_ref<CallAfterTimeout>(timeout, [this] {called = true;});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool called = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(CallAfterTimeoutTest, NoReset_1) {
|
2015-11-13 00:39:52 +01:00
|
|
|
auto obj = callAfterTimeout(milliseconds(100));
|
|
|
|
sleep_for(milliseconds(50));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_FALSE(called);
|
2015-11-13 00:39:52 +01:00
|
|
|
sleep_for(milliseconds(100));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_TRUE(called);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CallAfterTimeoutTest, NoReset_2) {
|
2015-11-13 00:39:52 +01:00
|
|
|
auto obj = callAfterTimeout(milliseconds(200));
|
|
|
|
sleep_for(milliseconds(150));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_FALSE(called);
|
2015-11-13 00:39:52 +01:00
|
|
|
sleep_for(milliseconds(100));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_TRUE(called);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CallAfterTimeoutTest, DoesntCallTwice) {
|
|
|
|
auto obj = callAfterTimeout(milliseconds(50));
|
2015-11-26 16:40:45 +01:00
|
|
|
// Wait until it was called
|
|
|
|
while(!called) {
|
|
|
|
sleep_for(milliseconds(10));
|
|
|
|
}
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_TRUE(called);
|
2015-11-26 16:40:45 +01:00
|
|
|
// Test that it isn't called again
|
2015-11-12 22:08:43 +01:00
|
|
|
called = false;
|
2015-11-25 22:43:13 +01:00
|
|
|
sleep_for(milliseconds(150));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_FALSE(called);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CallAfterTimeoutTest, OneReset) {
|
2015-11-27 22:27:20 +01:00
|
|
|
auto obj = callAfterTimeout(milliseconds(200));
|
|
|
|
sleep_for(milliseconds(125));
|
2015-11-12 22:08:43 +01:00
|
|
|
obj->resetTimer();
|
2015-11-27 22:27:20 +01:00
|
|
|
sleep_for(milliseconds(125));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_FALSE(called);
|
2015-11-27 22:27:20 +01:00
|
|
|
sleep_for(milliseconds(125));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_TRUE(called);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CallAfterTimeoutTest, TwoResets) {
|
2015-11-13 00:39:52 +01:00
|
|
|
auto obj = callAfterTimeout(milliseconds(100));
|
|
|
|
sleep_for(milliseconds(50));
|
2015-11-12 22:08:43 +01:00
|
|
|
obj->resetTimer();
|
2015-11-13 00:39:52 +01:00
|
|
|
sleep_for(milliseconds(75));
|
2015-11-12 22:08:43 +01:00
|
|
|
obj->resetTimer();
|
2015-11-13 00:39:52 +01:00
|
|
|
sleep_for(milliseconds(75));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_FALSE(called);
|
2015-11-13 00:39:52 +01:00
|
|
|
sleep_for(milliseconds(75));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_TRUE(called);
|
|
|
|
}
|