Commit a18f2c01 authored by Mirek Klimos's avatar Mirek Klimos Committed by Facebook Github Bot 0

Replace RequestContext::create with RequestContextScopeGuard in tests

Summary: RequestContextScopeGuard should be preferred to RequestContext::create because it makes sure that RequestContext is cleared afterwards - need to create more examples of this in the codebase, migrating unit tests should be safe

Reviewed By: interwq

Differential Revision: D3489969

fbshipit-source-id: 202fec93116db3d435c108fafecad26a4ed9b603
parent 12c01784
...@@ -1267,18 +1267,21 @@ TEST(FiberManager, RequestContext) { ...@@ -1267,18 +1267,21 @@ TEST(FiberManager, RequestContext) {
folly::fibers::Baton baton3; folly::fibers::Baton baton3;
folly::fibers::Baton baton4; folly::fibers::Baton baton4;
folly::RequestContext::create(); {
folly::RequestContextScopeGuard rctx;
auto rcontext1 = folly::RequestContext::get(); auto rcontext1 = folly::RequestContext::get();
fm.addTask([&]() { fm.addTask([&]() {
EXPECT_EQ(rcontext1, folly::RequestContext::get()); EXPECT_EQ(rcontext1, folly::RequestContext::get());
baton1.wait([&]() { EXPECT_EQ(rcontext1, folly::RequestContext::get()); }); baton1.wait(
[&]() { EXPECT_EQ(rcontext1, folly::RequestContext::get()); });
EXPECT_EQ(rcontext1, folly::RequestContext::get()); EXPECT_EQ(rcontext1, folly::RequestContext::get());
runInMainContext( runInMainContext(
[&]() { EXPECT_EQ(rcontext1, folly::RequestContext::get()); }); [&]() { EXPECT_EQ(rcontext1, folly::RequestContext::get()); });
checkRun1 = true; checkRun1 = true;
}); });
}
folly::RequestContext::create(); {
folly::RequestContextScopeGuard rctx;
auto rcontext2 = folly::RequestContext::get(); auto rcontext2 = folly::RequestContext::get();
fm.addTaskRemote([&]() { fm.addTaskRemote([&]() {
EXPECT_EQ(rcontext2, folly::RequestContext::get()); EXPECT_EQ(rcontext2, folly::RequestContext::get());
...@@ -1286,8 +1289,9 @@ TEST(FiberManager, RequestContext) { ...@@ -1286,8 +1289,9 @@ TEST(FiberManager, RequestContext) {
EXPECT_EQ(rcontext2, folly::RequestContext::get()); EXPECT_EQ(rcontext2, folly::RequestContext::get());
checkRun2 = true; checkRun2 = true;
}); });
}
folly::RequestContext::create(); {
folly::RequestContextScopeGuard rctx;
auto rcontext3 = folly::RequestContext::get(); auto rcontext3 = folly::RequestContext::get();
fm.addTaskFinally( fm.addTaskFinally(
[&]() { [&]() {
...@@ -1301,17 +1305,19 @@ TEST(FiberManager, RequestContext) { ...@@ -1301,17 +1305,19 @@ TEST(FiberManager, RequestContext) {
EXPECT_EQ(rcontext3, folly::RequestContext::get()); EXPECT_EQ(rcontext3, folly::RequestContext::get());
checkRun3 = true; checkRun3 = true;
}); });
}
{
folly::RequestContext::setContext(nullptr); folly::RequestContext::setContext(nullptr);
fm.addTask([&]() { fm.addTask([&]() {
folly::RequestContext::create(); folly::RequestContextScopeGuard rctx;
auto rcontext4 = folly::RequestContext::get(); auto rcontext4 = folly::RequestContext::get();
baton4.wait(); baton4.wait();
EXPECT_EQ(rcontext4, folly::RequestContext::get()); EXPECT_EQ(rcontext4, folly::RequestContext::get());
checkRun4 = true; checkRun4 = true;
}); });
}
folly::RequestContext::create(); {
folly::RequestContextScopeGuard rctx;
auto rcontext = folly::RequestContext::get(); auto rcontext = folly::RequestContext::get();
fm.loopUntilNoReady(); fm.loopUntilNoReady();
...@@ -1340,6 +1346,7 @@ TEST(FiberManager, RequestContext) { ...@@ -1340,6 +1346,7 @@ TEST(FiberManager, RequestContext) {
fm.loopUntilNoReady(); fm.loopUntilNoReady();
EXPECT_TRUE(checkRun4); EXPECT_TRUE(checkRun4);
EXPECT_EQ(rcontext, folly::RequestContext::get()); EXPECT_EQ(rcontext, folly::RequestContext::get());
}
} }
TEST(FiberManager, resizePeriodically) { TEST(FiberManager, resizePeriodically) {
......
...@@ -30,7 +30,7 @@ class TestData : public RequestData { ...@@ -30,7 +30,7 @@ class TestData : public RequestData {
TEST(Context, basic) { TEST(Context, basic) {
// Start a new context // Start a new context
RequestContext::create(); folly::RequestContextScopeGuard rctx;
EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test")); EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
......
...@@ -822,10 +822,12 @@ TEST(Future, RequestContext) { ...@@ -822,10 +822,12 @@ TEST(Future, RequestContext) {
bool value; bool value;
}; };
Promise<int> p1, p2;
{
NewThreadExecutor e; NewThreadExecutor e;
RequestContext::create(); folly::RequestContextScopeGuard rctx;
RequestContext::get()->setContextData("key", RequestContext::get()->setContextData(
folly::make_unique<MyRequestData>(true)); "key", folly::make_unique<MyRequestData>(true));
auto checker = [](int lineno) { auto checker = [](int lineno) {
return [lineno](Try<int>&& /* t */) { return [lineno](Try<int>&& /* t */) {
auto d = static_cast<MyRequestData*>( auto d = static_cast<MyRequestData*>(
...@@ -839,13 +841,13 @@ TEST(Future, RequestContext) { ...@@ -839,13 +841,13 @@ TEST(Future, RequestContext) {
e.setHandlesPriorities(); e.setHandlesPriorities();
makeFuture(2).via(&e).then(checker(__LINE__)); makeFuture(2).via(&e).then(checker(__LINE__));
Promise<int> p1, p2;
p1.getFuture().then(checker(__LINE__)); p1.getFuture().then(checker(__LINE__));
e.setThrowsOnAdd(); e.setThrowsOnAdd();
p2.getFuture().via(&e).then(checker(__LINE__)); p2.getFuture().via(&e).then(checker(__LINE__));
}
RequestContext::create(); // Assert that no RequestContext is set
EXPECT_FALSE(RequestContext::saveContext());
p1.setValue(3); p1.setValue(3);
p2.setValue(4); p2.setValue(4);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment