2016-02-11 16:39:42 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cpp-utils/pointer/unique_ref.h>
|
2016-02-23 21:07:23 +01:00
|
|
|
#include <cryfs-cli/CallAfterTimeout.h>
|
2018-02-02 21:34:45 +01:00
|
|
|
#include <atomic>
|
2015-11-12 22:08:43 +01:00
|
|
|
|
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using boost::chrono::milliseconds;
|
|
|
|
using boost::chrono::minutes;
|
|
|
|
using boost::this_thread::sleep_for;
|
2019-01-19 22:02:41 +01:00
|
|
|
using namespace cryfs_cli;
|
2015-11-12 22:08:43 +01:00
|
|
|
|
|
|
|
class CallAfterTimeoutTest : public ::testing::Test {
|
|
|
|
public:
|
2018-02-02 21:34:45 +01:00
|
|
|
CallAfterTimeoutTest(): called(false) {}
|
|
|
|
|
2015-11-12 22:08:43 +01:00
|
|
|
unique_ref<CallAfterTimeout> callAfterTimeout(milliseconds timeout) {
|
2019-01-20 12:25:21 +01:00
|
|
|
return make_unique_ref<CallAfterTimeout>(timeout, [this] {called = true;}, "test");
|
2015-11-12 22:08:43 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 21:34:45 +01:00
|
|
|
std::atomic<bool> called;
|
2015-11-12 22:08:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2018-09-14 03:41:58 +02:00
|
|
|
auto obj = callAfterTimeout(milliseconds(200));
|
|
|
|
sleep_for(milliseconds(100));
|
2015-11-12 22:08:43 +01:00
|
|
|
obj->resetTimer();
|
2018-09-14 03:41:58 +02:00
|
|
|
sleep_for(milliseconds(125));
|
2015-11-12 22:08:43 +01:00
|
|
|
obj->resetTimer();
|
2018-09-14 03:41:58 +02:00
|
|
|
sleep_for(milliseconds(125));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_FALSE(called);
|
2018-09-14 03:41:58 +02:00
|
|
|
sleep_for(milliseconds(125));
|
2015-11-12 22:08:43 +01:00
|
|
|
EXPECT_TRUE(called);
|
|
|
|
}
|