proxygen
SynchronizedLruPskCacheTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 
14 #include <folly/Format.h>
15 
16 using namespace folly;
17 using namespace testing;
18 
19 namespace fizz {
20 namespace client {
21 namespace test {
22 
24  public:
25  void SetUp() override {
26  cache_ = std::make_unique<SynchronizedLruPskCache>(3);
27  ticketTime_ = std::chrono::system_clock::now();
28  }
29 
30  protected:
31  CachedPsk getCachedPsk(std::string pskName = "PSK") {
32  return getTestPsk(pskName, ticketTime_);
33  }
34 
35  std::unique_ptr<SynchronizedLruPskCache> cache_;
36  std::chrono::system_clock::time_point ticketTime_;
37 };
38 
40  auto psk = getCachedPsk();
41  cache_->putPsk("fizz", psk);
42  auto cachedPsk = cache_->getPsk("fizz");
43  EXPECT_TRUE(cachedPsk);
44  pskEq(psk, *cachedPsk);
45 
46  cache_->removePsk("fizz");
47  EXPECT_FALSE(cache_->getPsk("fizz"));
48 }
49 
51  for (int i : {1, 2, 3}) {
52  auto pskName = folly::sformat("psk {}", i);
53  auto psk = getCachedPsk(pskName);
54  cache_->putPsk(pskName, psk);
55  }
56 
57  // Prime 1 to be evicted
58  cache_->getPsk("psk 2");
59  cache_->getPsk("psk 3");
60 
61  auto evictingPsk = getCachedPsk("psk 4");
62  cache_->putPsk("psk 4", evictingPsk);
63 
64  auto psk1 = cache_->getPsk("psk 1");
65  EXPECT_FALSE(psk1);
66 }
67 
68 } // namespace test
69 } // namespace client
70 } // namespace fizz
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
std::chrono::steady_clock::time_point now()
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
CachedPsk getTestPsk(std::string pskName, std::chrono::system_clock::time_point issueTime)
Definition: Utilities.h:16
Definition: Actions.h:16
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void pskEq(const CachedPsk &psk1, const CachedPsk &psk2)
Definition: Utilities.h:34
const char * string
Definition: Conv.cpp:212
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
std::unique_ptr< SynchronizedLruPskCache > cache_