-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimeoutGuardTests.cpp
More file actions
62 lines (50 loc) · 1.54 KB
/
TimeoutGuardTests.cpp
File metadata and controls
62 lines (50 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "stdafx.h"
#include "CppUnitTest.h"
#include "TimeoutGuard.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace utility
{
TEST_CLASS( TimeoutGuardTest )
{
public:
std::atomic_bool triggered{ false };
void shoud_trigger()
{
triggered.store( true );
}
TEST_METHOD( TimeoutGuardExpiration )
{
TimeoutGuard tg{
std::chrono::milliseconds{ 5 },
std::bind( &TimeoutGuardTest::shoud_trigger, this )
};
triggered.store( false );
tg.watch();
std::this_thread::sleep_for( std::chrono::milliseconds{ 10 } );
Assert::IsTrue( triggered.load(), L"Failed to call the timeout alarm on the first run", LINE_INFO() );
triggered.store( false );
tg.watch();
std::this_thread::sleep_for( std::chrono::milliseconds{ 10 } );
Assert::IsTrue( triggered.load(), L"Failed to call the timeout alarm on the second run", LINE_INFO() );
}
TEST_METHOD( TimeoutGuardNoAlarm )
{
TimeoutGuard tg{
std::chrono::milliseconds{ 5 },
std::bind( &TimeoutGuardTest::shoud_trigger, this )
};
triggered.store( false );
tg.watch();
std::this_thread::sleep_for( std::chrono::milliseconds{ 1 } );
Assert::IsFalse( triggered.load(), L"Wrongly called the timeout alarm on the first run", LINE_INFO() );
triggered.store( false );
tg.watch();
for (auto i = 0; i < 10; ++i)
{
std::this_thread::sleep_for( std::chrono::milliseconds{ 1 } );
tg.touch();
}
Assert::IsFalse( triggered.load(), L"Wrongly called the timeout alarm on the second run", LINE_INFO() );
}
};
}