forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_tests.cpp
More file actions
161 lines (115 loc) · 3.49 KB
/
common_tests.cpp
File metadata and controls
161 lines (115 loc) · 3.49 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright Sebastian Jeckel 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "gtest/gtest.h"
#include "react/common/syncpoint.h"
#include <chrono>
#include <cstdio>
#include <thread>
using namespace react;
///////////////////////////////////////////////////////////////////////////////////////////////////
TEST(SyncPointTest, DependencyCreation)
{
SyncPoint sp;
{
SyncPoint::Dependency dep1(sp);
SyncPoint::Dependency dep2(sp);
SyncPoint::Dependency dep3(sp);
std::vector<SyncPoint::Dependency> deps1 = { dep1, dep2, dep3 };
std::vector<SyncPoint::Dependency> deps2 = { dep1 };
SyncPoint::Dependency dep4( begin(deps1), end(deps1) );
SyncPoint::Dependency dep5;
dep5 = std::move(dep4);
SyncPoint::Dependency dep6;
dep6 = dep5;
SyncPoint::Dependency dep7( dep6 );
SyncPoint::Dependency dep8( begin(deps2), end(deps2) );
}
bool done = sp.WaitFor(std::chrono::milliseconds(1));
EXPECT_EQ(true, done);
}
TEST(SyncPointTest, SingleWait)
{
SyncPoint sp;
SyncPoint::Dependency dep(sp);
int output = 0;
std::thread t1([storedDep = std::move(dep), &output] ()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
output = 1;
});
sp.Wait();
EXPECT_EQ(1, output);
t1.join();
}
TEST(SyncPointTest, MultiWait)
{
SyncPoint sp;
SyncPoint::Dependency dep1(sp);
SyncPoint::Dependency dep2(sp);
SyncPoint::Dependency dep3(sp);
int output1 = 0;
int output2 = 0;
int output3 = 0;
std::thread t1([storedDep = std::move(dep1), &output1] ()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
output1 = 1;
});
std::thread t2([storedDep = std::move(dep2), &output2] ()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
output2 = 2;
});
std::thread t3([storedDep = std::move(dep3), &output3] ()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
output3 = 3;
});
sp.Wait();
EXPECT_EQ(1, output1);
EXPECT_EQ(2, output2);
EXPECT_EQ(3, output3);
t1.join();
t2.join();
t3.join();
}
TEST(SyncPointTest, MultiWaitFor)
{
SyncPoint sp;
SyncPoint::Dependency dep1(sp);
SyncPoint::Dependency dep2(sp);
SyncPoint::Dependency dep3(sp);
int output1 = 0;
int output2 = 0;
int output3 = 0;
std::thread t1([storedDep = std::move(dep1), &output1] ()
{
std::this_thread::sleep_for(std::chrono::seconds(3));
output1 = 1;
});
std::thread t2([storedDep = std::move(dep2), &output2] ()
{
std::this_thread::sleep_for(std::chrono::seconds(3));
output2 = 2;
});
std::thread t3([storedDep = std::move(dep3), &output3] ()
{
std::this_thread::sleep_for(std::chrono::seconds(3));
output3 = 3;
});
bool done = sp.WaitFor(std::chrono::milliseconds(1));
EXPECT_FALSE(done);
EXPECT_EQ(0, output1);
EXPECT_EQ(0, output2);
EXPECT_EQ(0, output3);
done = sp.WaitFor(std::chrono::seconds(10));
EXPECT_TRUE(done);
EXPECT_EQ(1, output1);
EXPECT_EQ(2, output2);
EXPECT_EQ(3, output3);
t1.join();
t2.join();
t3.join();
}