-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPlatisSolutionsBenchmarker_test.cpp
57 lines (49 loc) · 1.84 KB
/
PlatisSolutionsBenchmarker_test.cpp
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
#include "MockDownloader.hpp"
#include "PlatisSolutionsBenchmarker.hpp"
#include "gtest/gtest.h"
#include <chrono>
#include <thread>
using namespace testing;
using namespace after;
struct BenchmarkerTest : public Test
{
MockDownloader mDownloader;
PlatisSolutionsBenchmarker mBenchmarker{mDownloader};
};
TEST_F(BenchmarkerTest, getResponseTime_WhenCalled_WillDownloadFromWebsite)
{
EXPECT_CALL(mDownloader, download(_, _));
mBenchmarker.getResponseTime();
}
TEST_F(BenchmarkerTest,
getResponseTime_WhenDownloadNotSuccessful_WillReturnInvalid)
{
EXPECT_CALL(mDownloader, download(_, _)).WillOnce(Return(false));
EXPECT_FALSE(mBenchmarker.getResponseTime());
}
TEST_F(BenchmarkerTest,
getResponseTime_WhenDownloadSuccessful_WillReturnValidTime)
{
EXPECT_CALL(mDownloader, download(_, _)).WillOnce(Return(true));
EXPECT_TRUE(mBenchmarker.getResponseTime());
}
TEST_F(BenchmarkerTest,
getResponseTime_WhenDownloadSuccessful_WillReturnCorrectTime)
{
// If you really want to test the benchmarking/time calculation
// you can do so **at the expense of your execution time**.
// Alternatively, you can abstract time out as well, as described
// in the `time` chapter.
using namespace std::chrono_literals;
const auto executionTime = 1ms; // Or something larger that would not
// normally happen with gmock but not
// too large to make your tests slow
EXPECT_CALL(mDownloader, download(_, _))
.WillOnce(DoAll(InvokeWithoutArgs([&executionTime]() {
std::this_thread::sleep_for(executionTime);
}),
Return(true)));
const auto validTime = mBenchmarker.getResponseTime();
ASSERT_TRUE(validTime);
EXPECT_GE(validTime.value(), executionTime);
}