21 #ifndef PAGESPEED_KERNEL_CACHE_CACHE_TEST_BASE_H_
22 #define PAGESPEED_KERNEL_CACHE_CACHE_TEST_BASE_H_
37 namespace net_instaweb {
47 Callback() : test_(NULL) { 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;
61 validate_called_ =
true;
62 if ((invalid_value_ != NULL) && (value_str() == invalid_value_)) {
65 if ((invalid_key_ != NULL) && (key == invalid_key_)) {
72 SynchronousCallback::Done(state);
73 EXPECT_TRUE(validate_called_);
83 noop_wait_called_ =
true;
84 value_of_called_when_wait_was_invoked_ = called();
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(); }
91 bool validate_called_;
92 bool noop_wait_called_;
93 bool value_of_called_when_wait_was_invoked_;
97 const char* invalid_value_;
98 const char* invalid_key_;
105 : invalid_value_(NULL),
107 mutex_(new NullMutex),
108 outstanding_fetches_(0) {
111 STLDeleteElements(&callbacks_);
126 CheckGet(Cache(), key, expected_value);
148 Cache()->Delete(key);
159 WaitAndCheckNotFound(callback);
166 callback->set_invalid_value(invalid_value_);
167 callback->set_invalid_key(invalid_key_);
168 callbacks_.push_back(callback);
180 if (callback->noop_wait_called_) {
181 EXPECT_TRUE(callback->value_of_called_when_wait_was_invoked_);
187 ASSERT_TRUE(callback->called());
188 EXPECT_STREQ(expected_value, callback->value_str());
193 void WaitAndCheckNotFound(Callback* callback) {
195 ASSERT_TRUE(callback->called());
200 void IssueMultiGet(Callback* c0,
const GoogleString& key0,
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));
211 void TestMultiGet() {
216 IssueMultiGet(n0,
"n0", not_found,
"not_found", n1,
"n1");
218 WaitAndCheckNotFound(not_found);
225 for (
int i = 0; i < num; ++i) {
226 CheckPut(StringPrintf(
"n%d", i), StringPrintf(
"v%d", i));
230 void set_invalid_value(
const char* v) { invalid_value_ = v; }
231 void set_invalid_key(
const char* k) { invalid_key_ = k; }
242 ++outstanding_fetches_;
245 cache->
Get(key, callback);
256 return outstanding_fetches_;
267 --outstanding_fetches_;
270 const char* invalid_value_;
271 const char* invalid_key_;
272 std::vector<Callback*> callbacks_;
273 scoped_ptr<AbstractMutex> mutex_;
274 int outstanding_fetches_;
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
Definition: cache_interface.h:92
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