Skip to content

Commit 7aa17d2

Browse files
committed
Add test asserts to verify RequestState handling
1 parent 4986724 commit 7aa17d2

File tree

3 files changed

+85
-29
lines changed

3 files changed

+85
-29
lines changed

Today.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ Query::Query(appointmentsLoader&& getAppointments, tasksLoader&& getTasks, unrea
3939
{
4040
}
4141

42-
void Query::loadAppointments() const
42+
void Query::loadAppointments(const std::shared_ptr<service::RequestState>& state) const
4343
{
44+
if (state)
45+
{
46+
auto todayState = std::static_pointer_cast<RequestState>(state);
47+
48+
todayState->appointmentsRequestId = todayState->requestId;
49+
todayState->loadAppointmentsCount++;
50+
}
51+
4452
if (_getAppointments)
4553
{
4654
_appointments = _getAppointments();
@@ -50,7 +58,7 @@ void Query::loadAppointments() const
5058

5159
std::shared_ptr<Appointment> Query::findAppointment(const std::shared_ptr<service::RequestState>& state, const std::vector<uint8_t>& id) const
5260
{
53-
loadAppointments();
61+
loadAppointments(state);
5462

5563
for (const auto& appointment : _appointments)
5664
{
@@ -65,8 +73,16 @@ std::shared_ptr<Appointment> Query::findAppointment(const std::shared_ptr<servic
6573
return nullptr;
6674
}
6775

68-
void Query::loadTasks() const
76+
void Query::loadTasks(const std::shared_ptr<service::RequestState>& state) const
6977
{
78+
if (state)
79+
{
80+
auto todayState = std::static_pointer_cast<RequestState>(state);
81+
82+
todayState->tasksRequestId = todayState->requestId;
83+
todayState->loadTasksCount++;
84+
}
85+
7086
if (_getTasks)
7187
{
7288
_tasks = _getTasks();
@@ -76,7 +92,7 @@ void Query::loadTasks() const
7692

7793
std::shared_ptr<Task> Query::findTask(const std::shared_ptr<service::RequestState>& state, const std::vector<uint8_t>& id) const
7894
{
79-
loadTasks();
95+
loadTasks(state);
8096

8197
for (const auto& task : _tasks)
8298
{
@@ -91,8 +107,16 @@ std::shared_ptr<Task> Query::findTask(const std::shared_ptr<service::RequestStat
91107
return nullptr;
92108
}
93109

94-
void Query::loadUnreadCounts() const
110+
void Query::loadUnreadCounts(const std::shared_ptr<service::RequestState>& state) const
95111
{
112+
if (state)
113+
{
114+
auto todayState = std::static_pointer_cast<RequestState>(state);
115+
116+
todayState->unreadCountsRequestId = todayState->requestId;
117+
todayState->loadUnreadCountsCount++;
118+
}
119+
96120
if (_getUnreadCounts)
97121
{
98122
_unreadCounts = _getUnreadCounts();
@@ -102,7 +126,7 @@ void Query::loadUnreadCounts() const
102126

103127
std::shared_ptr<Folder> Query::findUnreadCount(const std::shared_ptr<service::RequestState>& state, const std::vector<uint8_t>& id) const
104128
{
105-
loadUnreadCounts();
129+
loadUnreadCounts(state);
106130

107131
for (const auto& folder : _unreadCounts)
108132
{
@@ -248,7 +272,7 @@ std::future<std::shared_ptr<object::AppointmentConnection>> Query::getAppointmen
248272
return std::async(std::launch::async,
249273
[this, spThis](const std::shared_ptr<service::RequestState>& stateWrapped, std::unique_ptr<int>&& firstWrapped, std::unique_ptr<response::Value>&& afterWrapped, std::unique_ptr<int>&& lastWrapped, std::unique_ptr<response::Value>&& beforeWrapped)
250274
{
251-
loadAppointments();
275+
loadAppointments(stateWrapped);
252276

253277
EdgeConstraints<Appointment, AppointmentConnection> constraints(stateWrapped, _appointments);
254278
auto connection = constraints(firstWrapped.get(), afterWrapped.get(), lastWrapped.get(), beforeWrapped.get());
@@ -263,7 +287,7 @@ std::future<std::shared_ptr<object::TaskConnection>> Query::getTasks(const std::
263287
return std::async(std::launch::async,
264288
[this, spThis](const std::shared_ptr<service::RequestState>& stateWrapped, std::unique_ptr<int>&& firstWrapped, std::unique_ptr<response::Value>&& afterWrapped, std::unique_ptr<int>&& lastWrapped, std::unique_ptr<response::Value>&& beforeWrapped)
265289
{
266-
loadTasks();
290+
loadTasks(stateWrapped);
267291

268292
EdgeConstraints<Task, TaskConnection> constraints(stateWrapped, _tasks);
269293
auto connection = constraints(firstWrapped.get(), afterWrapped.get(), lastWrapped.get(), beforeWrapped.get());
@@ -278,7 +302,7 @@ std::future<std::shared_ptr<object::FolderConnection>> Query::getUnreadCounts(co
278302
return std::async(std::launch::async,
279303
[this, spThis](const std::shared_ptr<service::RequestState>& stateWrapped, std::unique_ptr<int>&& firstWrapped, std::unique_ptr<response::Value>&& afterWrapped, std::unique_ptr<int>&& lastWrapped, std::unique_ptr<response::Value>&& beforeWrapped)
280304
{
281-
loadUnreadCounts();
305+
loadUnreadCounts(stateWrapped);
282306

283307
EdgeConstraints<Folder, FolderConnection> constraints(stateWrapped, _unreadCounts);
284308
auto connection = constraints(firstWrapped.get(), afterWrapped.get(), lastWrapped.get(), beforeWrapped.get());

include/Today.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ namespace facebook {
99
namespace graphql {
1010
namespace today {
1111

12+
struct RequestState : service::RequestState
13+
{
14+
RequestState(size_t id)
15+
: requestId(id)
16+
{
17+
}
18+
19+
const size_t requestId;
20+
21+
size_t appointmentsRequestId = 0;
22+
size_t tasksRequestId = 0;
23+
size_t unreadCountsRequestId = 0;
24+
25+
size_t loadAppointmentsCount = 0;
26+
size_t loadTasksCount = 0;
27+
size_t loadUnreadCountsCount = 0;
28+
};
29+
1230
class Appointment;
1331
class Task;
1432
class Folder;
@@ -36,9 +54,9 @@ class Query : public object::Query
3654
std::shared_ptr<Folder> findUnreadCount(const std::shared_ptr<service::RequestState>& state, const std::vector<uint8_t>& id) const;
3755

3856
// Lazy load the fields in each query
39-
void loadAppointments() const;
40-
void loadTasks() const;
41-
void loadUnreadCounts() const;
57+
void loadAppointments(const std::shared_ptr<service::RequestState>& state) const;
58+
void loadTasks(const std::shared_ptr<service::RequestState>& state) const;
59+
void loadUnreadCounts(const std::shared_ptr<service::RequestState>& state) const;
4260

4361
mutable appointmentsLoader _getAppointments;
4462
mutable tasksLoader _getTasks;

tests.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,6 @@ size_t TodayServiceCase::_getAppointmentsCount = 0;
9090
size_t TodayServiceCase::_getTasksCount = 0;
9191
size_t TodayServiceCase::_getUnreadCountsCount = 0;
9292

93-
struct TestRequestId
94-
: service::RequestState
95-
{
96-
TestRequestId(size_t id)
97-
: _id(id)
98-
{
99-
}
100-
101-
size_t _id;
102-
};
103-
10493
TEST_F(TodayServiceCase, QueryEverything)
10594
{
10695
auto ast = R"(
@@ -135,11 +124,18 @@ TEST_F(TodayServiceCase, QueryEverything)
135124
}
136125
})"_graphql;
137126
response::Value variables(response::Type::Map);
138-
auto state = std::make_shared<TestRequestId>(0);
127+
auto state = std::make_shared<today::RequestState>(1);
139128
auto result = _service->resolve(state, *ast->root, "Everything", variables).get();
140129
EXPECT_EQ(size_t(1), _getAppointmentsCount) << "today service lazy loads the appointments and caches the result";
141130
EXPECT_EQ(size_t(1), _getTasksCount) << "today service lazy loads the tasks and caches the result";
142131
EXPECT_EQ(size_t(1), _getUnreadCountsCount) << "today service lazy loads the unreadCounts and caches the result";
132+
EXPECT_EQ(size_t(1), state->appointmentsRequestId) << "today service passed the same RequestState";
133+
EXPECT_EQ(size_t(1), state->tasksRequestId) << "today service passed the same RequestState";
134+
EXPECT_EQ(size_t(1), state->unreadCountsRequestId) << "today service passed the same RequestState";
135+
EXPECT_EQ(size_t(1), state->loadAppointmentsCount) << "today service called the loader once";
136+
EXPECT_EQ(size_t(1), state->loadTasksCount) << "today service called the loader once";
137+
EXPECT_EQ(size_t(1), state->loadUnreadCountsCount) << "today service called the loader once";
138+
143139

144140
try
145141
{
@@ -210,11 +206,17 @@ TEST_F(TodayServiceCase, QueryAppointments)
210206
}
211207
})"_graphql;
212208
response::Value variables(response::Type::Map);
213-
auto state = std::make_shared<TestRequestId>(1);
209+
auto state = std::make_shared<today::RequestState>(2);
214210
auto result = _service->resolve(state, *ast->root, "", variables).get();
215211
EXPECT_EQ(size_t(1), _getAppointmentsCount) << "today service lazy loads the appointments and caches the result";
216212
EXPECT_GE(size_t(1), _getTasksCount) << "today service lazy loads the tasks and caches the result";
217213
EXPECT_GE(size_t(1), _getUnreadCountsCount) << "today service lazy loads the unreadCounts and caches the result";
214+
EXPECT_EQ(size_t(2), state->appointmentsRequestId) << "today service passed the same RequestState";
215+
EXPECT_EQ(size_t(0), state->tasksRequestId) << "today service did not call the loader";
216+
EXPECT_EQ(size_t(0), state->unreadCountsRequestId) << "today service did not call the loader";
217+
EXPECT_EQ(size_t(1), state->loadAppointmentsCount) << "today service called the loader once";
218+
EXPECT_EQ(size_t(0), state->loadTasksCount) << "today service did not call the loader";
219+
EXPECT_EQ(size_t(0), state->loadUnreadCountsCount) << "today service did not call the loader";
218220

219221
try
220222
{
@@ -266,11 +268,17 @@ TEST_F(TodayServiceCase, QueryTasks)
266268
}
267269
})gql"_graphql;
268270
response::Value variables(response::Type::Map);
269-
auto state = std::make_shared<TestRequestId>(2);
271+
auto state = std::make_shared<today::RequestState>(3);
270272
auto result = _service->resolve(state, *ast->root, "", variables).get();
271273
EXPECT_GE(size_t(1), _getAppointmentsCount) << "today service lazy loads the appointments and caches the result";
272274
EXPECT_EQ(size_t(1), _getTasksCount) << "today service lazy loads the tasks and caches the result";
273275
EXPECT_GE(size_t(1), _getUnreadCountsCount) << "today service lazy loads the unreadCounts and caches the result";
276+
EXPECT_EQ(size_t(0), state->appointmentsRequestId) << "today service did not call the loader";
277+
EXPECT_EQ(size_t(3), state->tasksRequestId) << "today service passed the same RequestState";
278+
EXPECT_EQ(size_t(0), state->unreadCountsRequestId) << "today service did not call the loader";
279+
EXPECT_EQ(size_t(0), state->loadAppointmentsCount) << "today service did not call the loader";
280+
EXPECT_EQ(size_t(1), state->loadTasksCount) << "today service called the loader once";
281+
EXPECT_EQ(size_t(0), state->loadUnreadCountsCount) << "today service did not call the loader";
274282

275283
try
276284
{
@@ -321,11 +329,17 @@ TEST_F(TodayServiceCase, QueryUnreadCounts)
321329
}
322330
})"_graphql;
323331
response::Value variables(response::Type::Map);
324-
auto state = std::make_shared<TestRequestId>(3);
332+
auto state = std::make_shared<today::RequestState>(4);
325333
auto result = _service->resolve(state, *ast->root, "", variables).get();
326334
EXPECT_GE(size_t(1), _getAppointmentsCount) << "today service lazy loads the appointments and caches the result";
327335
EXPECT_GE(size_t(1), _getTasksCount) << "today service lazy loads the tasks and caches the result";
328336
EXPECT_EQ(size_t(1), _getUnreadCountsCount) << "today service lazy loads the unreadCounts and caches the result";
337+
EXPECT_EQ(size_t(0), state->appointmentsRequestId) << "today service did not call the loader";
338+
EXPECT_EQ(size_t(0), state->tasksRequestId) << "today service did not call the loader";
339+
EXPECT_EQ(size_t(4), state->unreadCountsRequestId) << "today service passed the same RequestState";
340+
EXPECT_EQ(size_t(0), state->loadAppointmentsCount) << "today service did not call the loader";
341+
EXPECT_EQ(size_t(0), state->loadTasksCount) << "today service did not call the loader";
342+
EXPECT_EQ(size_t(1), state->loadUnreadCountsCount) << "today service called the loader once";
329343

330344
try
331345
{
@@ -375,7 +389,7 @@ TEST_F(TodayServiceCase, MutateCompleteTask)
375389
}
376390
})"_graphql;
377391
response::Value variables(response::Type::Map);
378-
auto state = std::make_shared<TestRequestId>(4);
392+
auto state = std::make_shared<today::RequestState>(5);
379393
auto result = _service->resolve(state, *ast->root, "", variables).get();
380394

381395
try
@@ -451,7 +465,7 @@ TEST_F(TodayServiceCase, Introspection)
451465
}
452466
})"_graphql;
453467
response::Value variables(response::Type::Map);
454-
auto state = std::make_shared<TestRequestId>(5);
468+
auto state = std::make_shared<today::RequestState>(6);
455469
auto result = _service->resolve(state, *ast->root, "", variables).get();
456470

457471
try

0 commit comments

Comments
 (0)