proxygen
RequestContextTest.cpp File Reference
#include <thread>
#include <folly/Memory.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/Request.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

class  TestData
 
class  RequestContextTest
 

Functions

 TEST_F (RequestContextTest, SimpleTest)
 
 TEST_F (RequestContextTest, RequestContextScopeGuard)
 
 TEST_F (RequestContextTest, defaultContext)
 
 TEST_F (RequestContextTest, setIfAbsentTest)
 
 TEST_F (RequestContextTest, testSetUnset)
 
 TEST_F (RequestContextTest, deadlockTest)
 
 TEST_F (RequestContextTest, sharedGlobalTest)
 
 TEST_F (RequestContextTest, ShallowCopyBasic)
 
 TEST_F (RequestContextTest, ShallowCopyOverwrite)
 
 TEST_F (RequestContextTest, ShallowCopyDefaultContext)
 
 TEST_F (RequestContextTest, ShallowCopyClear)
 

Variables

RequestToken testtoken ("test")
 

Function Documentation

TEST_F ( RequestContextTest  ,
SimpleTest   
)

Definition at line 95 of file RequestContextTest.cpp.

References a, folly::RequestContext::create(), folly::data(), data_, EXPECT_EQ, EXPECT_NE, EXPECT_TRUE, folly::RequestContext::get(), folly::RequestContext::getContextData(), folly::EventBase::loopForever(), folly::EventBase::runInEventBaseThread(), folly::RequestContext::saveContext(), folly::RequestContext::setContext(), folly::RequestContext::setContextData(), folly::EventBase::terminateLoopSoon(), and testtoken.

95  {
96  EventBase base;
97 
98  // There should always be a default context with get()
99  EXPECT_TRUE(RequestContext::get() != nullptr);
100 
101  // but not with saveContext()
102  EXPECT_EQ(RequestContext::saveContext(), nullptr);
103  RequestContext::create();
104  EXPECT_NE(RequestContext::saveContext(), nullptr);
105  RequestContext::create();
106  EXPECT_NE(RequestContext::saveContext(), nullptr);
107 
108  EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
109 
110  RequestContext::get()->setContextData("test", std::make_unique<TestData>(10));
111  base.runInEventBaseThread([&]() {
112  EXPECT_TRUE(RequestContext::get() != nullptr);
113  auto data = dynamic_cast<TestData*>(
114  RequestContext::get()->getContextData(testtoken))
115  ->data_;
116  EXPECT_EQ(10, data);
117  base.terminateLoopSoon();
118  });
119  auto th = std::thread([&]() { base.loopForever(); });
120  th.join();
121  EXPECT_TRUE(RequestContext::get() != nullptr);
122  auto a =
123  dynamic_cast<TestData*>(RequestContext::get()->getContextData("test"));
124  auto data = a->data_;
125  EXPECT_EQ(10, data);
126 
127  RequestContext::setContext(std::shared_ptr<RequestContext>());
128  // There should always be a default context
129  EXPECT_TRUE(nullptr != RequestContext::get());
130 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void terminateLoopSoon()
Definition: EventBase.cpp:493
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
char a
RequestToken testtoken("test")
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
StringPiece data_

Definition at line 132 of file RequestContextTest.cpp.

References data_, EXPECT_EQ, and EXPECT_FALSE.

132  {
134  setData(10);
135  {
137  EXPECT_FALSE(hasData());
138  setData(20);
139  EXPECT_EQ(20, getData().data_);
140  EXPECT_EQ(1, getData().set_);
141  EXPECT_EQ(0, getData().unset_);
142  }
143  EXPECT_EQ(10, getData().data_);
144  EXPECT_EQ(2, getData().set_);
145  EXPECT_EQ(1, getData().unset_);
146 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
StringPiece data_
TEST_F ( RequestContextTest  ,
defaultContext   
)

Definition at line 148 of file RequestContextTest.cpp.

References data_, EXPECT_EQ, and EXPECT_FALSE.

148  {
149  // Don't create a top level guard
150  setData(10);
151  {
153  EXPECT_FALSE(hasData());
154  }
155  EXPECT_EQ(10, getData().data_);
156  EXPECT_EQ(1, getData().set_);
157  EXPECT_EQ(0, getData().unset_);
158 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
StringPiece data_
TEST_F ( RequestContextTest  ,
setIfAbsentTest   
)

Definition at line 160 of file RequestContextTest.cpp.

References data_, EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::RequestContext::get(), folly::RequestContext::setContext(), folly::RequestContext::setContextData(), and testtoken.

160  {
161  EXPECT_TRUE(RequestContext::get() != nullptr);
162 
163  RequestContext::get()->setContextData("test", std::make_unique<TestData>(10));
164  EXPECT_FALSE(RequestContext::get()->setContextDataIfAbsent(
165  "test", std::make_unique<TestData>(20)));
166  EXPECT_EQ(
167  10,
168  dynamic_cast<TestData*>(RequestContext::get()->getContextData(testtoken))
169  ->data_);
170 
171  EXPECT_TRUE(RequestContext::get()->setContextDataIfAbsent(
172  "test2", std::make_unique<TestData>(20)));
173  EXPECT_EQ(
174  20,
175  dynamic_cast<TestData*>(RequestContext::get()->getContextData("test2"))
176  ->data_);
177 
178  RequestContext::setContext(std::shared_ptr<RequestContext>());
179  EXPECT_TRUE(nullptr != RequestContext::get());
180 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
RequestToken testtoken("test")
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
StringPiece data_
TEST_F ( RequestContextTest  ,
testSetUnset   
)

Definition at line 182 of file RequestContextTest.cpp.

References folly::RequestContext::create(), EXPECT_EQ, folly::RequestContext::saveContext(), folly::RequestContext::setContext(), and testtoken.

182  {
183  RequestContext::create();
184  auto ctx1 = RequestContext::saveContext();
185  ctx1->setContextData("test", std::make_unique<TestData>(10));
186  auto testData1 = dynamic_cast<TestData*>(ctx1->getContextData("test"));
187 
188  // onSet called in setContextData
189  EXPECT_EQ(1, testData1->set_);
190 
191  // Override RequestContext
192  RequestContext::create();
193  auto ctx2 = RequestContext::saveContext();
194  ctx2->setContextData(testtoken, std::make_unique<TestData>(20));
195  auto testData2 = dynamic_cast<TestData*>(ctx2->getContextData(testtoken));
196 
197  // onSet called in setContextData
198  EXPECT_EQ(1, testData2->set_);
199 
200  // Check ctx1->onUnset was called
201  EXPECT_EQ(1, testData1->unset_);
202 
203  RequestContext::setContext(ctx1);
204  EXPECT_EQ(2, testData1->set_);
205  EXPECT_EQ(1, testData1->unset_);
206  EXPECT_EQ(1, testData2->unset_);
207 
208  RequestContext::setContext(ctx2);
209  EXPECT_EQ(2, testData1->set_);
210  EXPECT_EQ(2, testData1->unset_);
211  EXPECT_EQ(2, testData2->set_);
212  EXPECT_EQ(1, testData2->unset_);
213 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
RequestToken testtoken("test")
TEST_F ( RequestContextTest  ,
deadlockTest   
)

Definition at line 215 of file RequestContextTest.cpp.

References folly::RequestContext::clearContextData(), folly::RequestContext::get(), folly::RequestContext::setContextData(), string, testtoken, and val.

215  {
216  class DeadlockTestData : public RequestData {
217  public:
218  explicit DeadlockTestData(const std::string& val) : val_(val) {}
219 
220  ~DeadlockTestData() override {
222  val_, std::make_unique<TestData>(1));
223  }
224 
225  bool hasCallback() override {
226  return false;
227  }
228 
229  std::string val_;
230  };
231 
233  "test", std::make_unique<DeadlockTestData>("test2"));
235 }
void setContextData(const RequestToken &val, std::unique_ptr< RequestData > data)
Definition: Request.cpp:129
double val
Definition: String.cpp:273
virtual bool hasCallback()=0
void clearContextData(const RequestToken &val)
Definition: Request.cpp:176
RequestToken testtoken("test")
const char * string
Definition: Conv.cpp:212
static RequestContext * get()
Definition: Request.cpp:290
TEST_F ( RequestContextTest  ,
sharedGlobalTest   
)

Definition at line 239 of file RequestContextTest.cpp.

References ASSERT_FALSE, ASSERT_TRUE, folly::RequestContext::get(), and folly::RequestContext::setContextData().

239  {
240  static bool global = false;
241 
242  class GlobalTestData : public RequestData {
243  public:
244  void onSet() override {
245  ASSERT_FALSE(global);
246  global = true;
247  }
248 
249  void onUnset() override {
250  ASSERT_TRUE(global);
251  global = false;
252  }
253 
254  bool hasCallback() override {
255  return true;
256  }
257  };
258 
261  "test", std::make_unique<GlobalTestData>());
262  {
265  "test", std::make_unique<GlobalTestData>());
266  }
267 }
void setContextData(const RequestToken &val, std::unique_ptr< RequestData > data)
Definition: Request.cpp:129
virtual bool hasCallback()=0
virtual void onSet()
Definition: Request.h:81
virtual void onUnset()
Definition: Request.h:85
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
static RequestContext * get()
Definition: Request.cpp:290
TEST_F ( RequestContextTest  ,
ShallowCopyBasic   
)

Definition at line 269 of file RequestContextTest.cpp.

References data_, EXPECT_EQ, and EXPECT_FALSE.

269  {
271  setData(123, "immutable");
272  EXPECT_EQ(123, getData("immutable").data_);
273  EXPECT_FALSE(hasData());
274 
275  {
277  EXPECT_EQ(123, getData("immutable").data_);
278  setData(789);
279  EXPECT_EQ(789, getData().data_);
280  }
281 
282  EXPECT_FALSE(hasData());
283  EXPECT_EQ(123, getData("immutable").data_);
284  EXPECT_EQ(1, getData("immutable").set_);
285  EXPECT_EQ(0, getData("immutable").unset_);
286 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
StringPiece data_
TEST_F ( RequestContextTest  ,
ShallowCopyOverwrite   
)

Definition at line 288 of file RequestContextTest.cpp.

References data_, and EXPECT_EQ.

288  {
290  setData(123);
291  EXPECT_EQ(123, getData().data_);
292  {
294  "test", std::make_unique<TestData>(789));
295  EXPECT_EQ(789, getData().data_);
296  EXPECT_EQ(1, getData().set_);
297  EXPECT_EQ(0, getData().unset_);
298  }
299  EXPECT_EQ(123, getData().data_);
300  EXPECT_EQ(2, getData().set_);
301  EXPECT_EQ(1, getData().unset_);
302 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
StringPiece data_
TEST_F ( RequestContextTest  ,
ShallowCopyDefaultContext   
)

Definition at line 304 of file RequestContextTest.cpp.

References data_, and EXPECT_EQ.

304  {
305  // Don't set global scope guard
306  setData(123);
307  EXPECT_EQ(123, getData().data_);
308  {
310  "test", std::make_unique<TestData>(789));
311  EXPECT_EQ(789, getData().data_);
312  }
313  EXPECT_EQ(123, getData().data_);
314  EXPECT_EQ(1, getData().set_);
315  EXPECT_EQ(0, getData().unset_);
316 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
StringPiece data_
TEST_F ( RequestContextTest  ,
ShallowCopyClear   
)

Definition at line 318 of file RequestContextTest.cpp.

References data_, and EXPECT_EQ.

318  {
320  setData(123);
321  EXPECT_EQ(123, getData().data_);
322  {
324  EXPECT_EQ(123, getData().data_);
325  clearData();
326  setData(789);
327  EXPECT_EQ(789, getData().data_);
328  }
329  EXPECT_EQ(123, getData().data_);
330  EXPECT_EQ(2, getData().set_);
331  EXPECT_EQ(1, getData().unset_);
332 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
StringPiece data_

Variable Documentation

RequestToken testtoken("test")

Referenced by TEST_F().