Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cache_test_base.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
20 
21 #ifndef PAGESPEED_KERNEL_CACHE_CACHE_TEST_BASE_H_
22 #define PAGESPEED_KERNEL_CACHE_CACHE_TEST_BASE_H_
23 
24 #include <vector>
25 
36 
37 namespace net_instaweb {
38 
39 class CacheTestBase : public testing::Test {
40  public:
45  public:
46  explicit Callback(CacheTestBase* test) : test_(test) { Reset(); }
47  Callback() : test_(NULL) { Reset(); }
48  virtual ~Callback() {}
49  Callback* Reset() {
50  SynchronousCallback::Reset();
51  validate_called_ = false;
52  noop_wait_called_ = false;
53  value_of_called_when_wait_was_invoked_ = false;
54  invalid_value_ = NULL;
55  invalid_key_ = NULL;
56  return this;
57  }
58 
59  virtual bool ValidateCandidate(const GoogleString& key,
61  validate_called_ = true;
62  if ((invalid_value_ != NULL) && (value_str() == invalid_value_)) {
63  return false;
64  }
65  if ((invalid_key_ != NULL) && (key == invalid_key_)) {
66  return false;
67  }
68  return true;
69  }
70 
71  virtual void Done(CacheInterface::KeyState state) {
72  SynchronousCallback::Done(state);
73  EXPECT_TRUE(validate_called_);
74  if (test_ != NULL) {
75  test_->GetDone();
76  }
77  }
78 
82  virtual void Wait() {
83  noop_wait_called_ = true;
84  value_of_called_when_wait_was_invoked_ = called();
85  }
86 
87  void set_invalid_value(const char* v) { invalid_value_ = v; }
88  void set_invalid_key(const char* k) { invalid_key_ = k; }
89  StringPiece value_str() { return value().Value(); }
90 
91  bool validate_called_;
92  bool noop_wait_called_;
93  bool value_of_called_when_wait_was_invoked_;
94 
95  private:
96  CacheTestBase* test_;
97  const char* invalid_value_;
98  const char* invalid_key_;
99 
100 
101  };
102 
103  protected:
104  CacheTestBase()
105  : invalid_value_(NULL),
106  invalid_key_(NULL),
107  mutex_(new NullMutex),
108  outstanding_fetches_(0) {
109  }
110  ~CacheTestBase() {
111  STLDeleteElements(&callbacks_);
112  }
113 
116  virtual Callback* NewCallback() { return new Callback(this); }
117  virtual CacheInterface* Cache() = 0;
118 
121  virtual void PostOpCleanup() {}
122 
125  void CheckGet(const GoogleString& key, const GoogleString& expected_value) {
126  CheckGet(Cache(), key, expected_value);
127  }
128 
130  void CheckGet(CacheInterface* cache, const GoogleString& key,
131  const GoogleString& expected_value) {
132  Callback* callback = InitiateGet(cache, key);
133  WaitAndCheck(callback, expected_value);
134  }
135 
137  void CheckPut(const GoogleString& key, const GoogleString& value) {
138  CheckPut(Cache(), key, value);
139  }
140 
141  void CheckPut(CacheInterface* cache, const GoogleString& key,
142  const GoogleString& value) {
143  cache->Put(key, SharedString(value));
144  PostOpCleanup();
145  }
146 
147  void CheckDelete(const GoogleString& key) {
148  Cache()->Delete(key);
149  PostOpCleanup();
150  }
151 
153  void CheckNotFound(const char* key) {
154  CheckNotFound(Cache(), key);
155  }
156 
157  void CheckNotFound(CacheInterface* cache, const char* key) {
158  Callback* callback = InitiateGet(cache, key);
159  WaitAndCheckNotFound(callback);
160  }
161 
165  Callback* callback = NewCallback();
166  callback->set_invalid_value(invalid_value_);
167  callback->set_invalid_key(invalid_key_);
168  callbacks_.push_back(callback);
169  return callback;
170  }
171 
172  void WaitAndCheck(Callback* callback, const GoogleString& expected_value) {
179  callback->Wait();
180  if (callback->noop_wait_called_) {
181  EXPECT_TRUE(callback->value_of_called_when_wait_was_invoked_);
186  }
187  ASSERT_TRUE(callback->called());
188  EXPECT_STREQ(expected_value, callback->value_str());
189  EXPECT_EQ(CacheInterface::kAvailable, callback->state());
190  PostOpCleanup();
191  }
192 
193  void WaitAndCheckNotFound(Callback* callback) {
194  callback->Wait();
195  ASSERT_TRUE(callback->called());
196  EXPECT_EQ(CacheInterface::kNotFound, callback->state());
197  PostOpCleanup();
198  }
199 
200  void IssueMultiGet(Callback* c0, const GoogleString& key0,
201  Callback* c1, const GoogleString& key1,
202  Callback* c2, const GoogleString& key2) {
203  CacheInterface::MultiGetRequest* request =
204  new CacheInterface::MultiGetRequest;
205  request->push_back(CacheInterface::KeyCallback(key0, c0));
206  request->push_back(CacheInterface::KeyCallback(key1, c1));
207  request->push_back(CacheInterface::KeyCallback(key2, c2));
208  Cache()->MultiGet(request);
209  }
210 
211  void TestMultiGet() {
212  PopulateCache(2);
213  Callback* n0 = AddCallback();
214  Callback* not_found = AddCallback();
215  Callback* n1 = AddCallback();
216  IssueMultiGet(n0, "n0", not_found, "not_found", n1, "n1");
217  WaitAndCheck(n0, "v0");
218  WaitAndCheckNotFound(not_found);
219  WaitAndCheck(n1, "v1");
220  }
221 
224  void PopulateCache(int num) {
225  for (int i = 0; i < num; ++i) {
226  CheckPut(StringPrintf("n%d", i), StringPrintf("v%d", i));
227  }
228  }
229 
230  void set_invalid_value(const char* v) { invalid_value_ = v; }
231  void set_invalid_key(const char* k) { invalid_key_ = k; }
232 
236  return InitiateGet(Cache(), key);
237  }
238 
239  Callback* InitiateGet(CacheInterface* cache, const GoogleString& key) {
240  {
241  ScopedMutex lock(mutex_.get());
242  ++outstanding_fetches_;
243  }
244  Callback* callback = AddCallback();
245  cache->Get(key, callback);
246  return callback;
247  }
248 
250  void set_mutex(AbstractMutex* mutex) { mutex_.reset(mutex); }
251 
255  ScopedMutex lock(mutex_.get());
256  return outstanding_fetches_;
257  }
258 
259  private:
265  void GetDone() {
266  ScopedMutex lock(mutex_.get());
267  --outstanding_fetches_;
268  }
269 
270  const char* invalid_value_;
271  const char* invalid_key_;
272  std::vector<Callback*> callbacks_;
273  scoped_ptr<AbstractMutex> mutex_;
274  int outstanding_fetches_;
275 };
276 
277 }
278 
279 #endif
virtual void Put(const GoogleString &key, const SharedString &value)=0
virtual bool ValidateCandidate(const GoogleString &key, CacheInterface::KeyState state)
Definition: cache_test_base.h:59
Abstract interface for a cache.
Definition: cache_interface.h:32
int outstanding_fetches()
Definition: cache_test_base.h:254
Requested key needs to be written.
Definition: cache_interface.h:36
virtual void Done(CacheInterface::KeyState state)
Definition: cache_test_base.h:71
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
void set_mutex(AbstractMutex *mutex)
Sets the mutex used to protect outstanding_fetches_.
Definition: cache_test_base.h:250
void CheckNotFound(const char *key)
Performs a Get and verifies that the key is not found.
Definition: cache_test_base.h:153
Requested key is available for serving.
Definition: cache_interface.h:35
StringPiece Value() const
KeyState
Definition: cache_interface.h:34
Callback * InitiateGet(const GoogleString &key)
Definition: cache_test_base.h:235
virtual void MultiGet(MultiGetRequest *request)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual void PostOpCleanup()
Definition: cache_test_base.h:121
Helper class for lexically scoped mutexing.
Definition: abstract_mutex.h:46
void CheckGet(const GoogleString &key, const GoogleString &expected_value)
Definition: cache_test_base.h:125
virtual void Get(const GoogleString &key, Callback *callback)=0
Callback * AddCallback()
Definition: cache_test_base.h:164
Definition: shared_string.h:40
virtual void Wait()
Definition: cache_test_base.h:82
void PopulateCache(int num)
Definition: cache_test_base.h:224
virtual Callback * NewCallback()
Definition: cache_test_base.h:116
void WaitAndCheck(Callback *callback, const GoogleString &expected_value)
Definition: cache_test_base.h:172
void CheckPut(const GoogleString &key, const GoogleString &value)
Writes a value into the cache.
Definition: cache_test_base.h:137
Definition: cache_test_base.h:39
Definition: cache_test_base.h:44
void CheckGet(CacheInterface *cache, const GoogleString &key, const GoogleString &expected_value)
As above, but specifies which cache to use.
Definition: cache_test_base.h:130