summaryrefslogtreecommitdiffstats
path: root/net/disk_cache
diff options
context:
space:
mode:
authorrsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-07 02:12:38 +0000
committerrsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-07 02:12:38 +0000
commit243757602999594320350e28ade4d797ef0340cd (patch)
tree117b5a54a23c8d3166930b55ae497e66d4862742 /net/disk_cache
parent227bc7c44aa9f5a56fdd4da578a572183c5071f5 (diff)
downloadchromium_src-243757602999594320350e28ade4d797ef0340cd.zip
chromium_src-243757602999594320350e28ade4d797ef0340cd.tar.gz
chromium_src-243757602999594320350e28ade4d797ef0340cd.tar.bz2
Allow DiskCache tests to be shuffled/independent of eachother
BUG=none TEST=existing unittests Review URL: http://codereview.chromium.org/7308003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91655 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache')
-rw-r--r--net/disk_cache/disk_cache_perftest.cc30
-rw-r--r--net/disk_cache/disk_cache_test_util.cc61
-rw-r--r--net/disk_cache/disk_cache_test_util.h60
-rw-r--r--net/disk_cache/entry_unittest.cc66
-rw-r--r--net/disk_cache/mapped_file_unittest.cc38
5 files changed, 129 insertions, 126 deletions
diff --git a/net/disk_cache/disk_cache_perftest.cc b/net/disk_cache/disk_cache_perftest.cc
index 14f15c6..2adc29b 100644
--- a/net/disk_cache/disk_cache_perftest.cc
+++ b/net/disk_cache/disk_cache_perftest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -24,9 +24,6 @@
using base::Time;
-extern volatile int g_cache_tests_received;
-extern volatile bool g_cache_tests_error;
-
typedef PlatformTest DiskCacheTest;
namespace {
@@ -41,7 +38,7 @@ const int kMaxSize = 16 * 1024 - 1;
// Creates num_entries on the cache, and writes 200 bytes of metadata and up
// to kMaxSize of data to each entry.
-int TimeWrite(int num_entries, disk_cache::Backend* cache,
+bool TimeWrite(int num_entries, disk_cache::Backend* cache,
TestEntries* entries) {
const int kSize1 = 200;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
@@ -50,12 +47,10 @@ int TimeWrite(int num_entries, disk_cache::Backend* cache,
CacheTestFillBuffer(buffer1->data(), kSize1, false);
CacheTestFillBuffer(buffer2->data(), kMaxSize, false);
- CallbackTest callback(true);
- g_cache_tests_error = false;
- g_cache_tests_received = 0;
int expected = 0;
MessageLoopHelper helper;
+ CallbackTest callback(&helper, true);
PerfTimeLogger timer("Write disk cache entries");
@@ -88,11 +83,11 @@ int TimeWrite(int num_entries, disk_cache::Backend* cache,
helper.WaitUntilCacheIoFinished(expected);
timer.Done();
- return expected;
+ return (expected == helper.callbacks_called());
}
// Reads the data and metadata from each entry listed on |entries|.
-int TimeRead(int num_entries, disk_cache::Backend* cache,
+bool TimeRead(int num_entries, disk_cache::Backend* cache,
const TestEntries& entries, bool cold) {
const int kSize1 = 200;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
@@ -101,12 +96,10 @@ int TimeRead(int num_entries, disk_cache::Backend* cache,
CacheTestFillBuffer(buffer1->data(), kSize1, false);
CacheTestFillBuffer(buffer2->data(), kMaxSize, false);
- CallbackTest callback(true);
- g_cache_tests_error = false;
- g_cache_tests_received = 0;
int expected = 0;
MessageLoopHelper helper;
+ CallbackTest callback(&helper, true);
const char* message = cold ? "Read disk cache entries (cold)" :
"Read disk cache entries (warm)";
@@ -135,7 +128,7 @@ int TimeRead(int num_entries, disk_cache::Backend* cache,
helper.WaitUntilCacheIoFinished(expected);
timer.Done();
- return expected;
+ return (expected == helper.callbacks_called());
}
int BlockSize() {
@@ -179,8 +172,7 @@ TEST_F(DiskCacheTest, CacheBackendPerformance) {
TestEntries entries;
int num_entries = 1000;
- int ret = TimeWrite(num_entries, cache, &entries);
- EXPECT_EQ(ret, g_cache_tests_received);
+ EXPECT_TRUE(TimeWrite(num_entries, cache, &entries));
MessageLoop::current()->RunAllPending();
delete cache;
@@ -201,11 +193,9 @@ TEST_F(DiskCacheTest, CacheBackendPerformance) {
NULL, &cache, &cb);
ASSERT_EQ(net::OK, cb.GetResult(rv));
- ret = TimeRead(num_entries, cache, entries, true);
- EXPECT_EQ(ret, g_cache_tests_received);
+ EXPECT_TRUE(TimeRead(num_entries, cache, entries, true));
- ret = TimeRead(num_entries, cache, entries, false);
- EXPECT_EQ(ret, g_cache_tests_received);
+ EXPECT_TRUE(TimeRead(num_entries, cache, entries, false));
MessageLoop::current()->RunAllPending();
delete cache;
diff --git a/net/disk_cache/disk_cache_test_util.cc b/net/disk_cache/disk_cache_test_util.cc
index aa3efef..296636a 100644
--- a/net/disk_cache/disk_cache_test_util.cc
+++ b/net/disk_cache/disk_cache_test_util.cc
@@ -122,41 +122,20 @@ ScopedTestCache::~ScopedTestCache() {
// -----------------------------------------------------------------------
-volatile int g_cache_tests_received = 0;
-volatile bool g_cache_tests_error = 0;
-
-CallbackTest::CallbackTest(bool reuse) : result_(-1), reuse_(reuse ? 0 : 1) {}
-
-CallbackTest::~CallbackTest() {}
-
-// On the actual callback, increase the number of tests received and check for
-// errors (an unexpected test received)
-void CallbackTest::RunWithParams(const Tuple1<int>& params) {
- if (reuse_) {
- DCHECK_EQ(1, reuse_);
- if (2 == reuse_)
- g_cache_tests_error = true;
- reuse_++;
- }
-
- result_ = params.a;
- g_cache_tests_received++;
-}
-
-// -----------------------------------------------------------------------
-
MessageLoopHelper::MessageLoopHelper()
: num_callbacks_(0),
num_iterations_(0),
last_(0),
- completed_(false) {
+ completed_(false),
+ callback_reused_error_(false),
+ callbacks_called_(0) {
}
MessageLoopHelper::~MessageLoopHelper() {
}
bool MessageLoopHelper::WaitUntilCacheIoFinished(int num_callbacks) {
- if (num_callbacks == g_cache_tests_received)
+ if (num_callbacks == callbacks_called_)
return true;
ExpectCallbacks(num_callbacks);
@@ -171,17 +150,41 @@ bool MessageLoopHelper::WaitUntilCacheIoFinished(int num_callbacks) {
// Quits the message loop when all callbacks are called or we've been waiting
// too long for them (2 secs without a callback).
void MessageLoopHelper::TimerExpired() {
- CHECK_LE(g_cache_tests_received, num_callbacks_);
- if (g_cache_tests_received == num_callbacks_) {
+ CHECK_LE(callbacks_called_, num_callbacks_);
+ if (callbacks_called_ == num_callbacks_) {
completed_ = true;
MessageLoop::current()->Quit();
} else {
// Not finished yet. See if we have to abort.
- if (last_ == g_cache_tests_received)
+ if (last_ == callbacks_called_)
num_iterations_++;
else
- last_ = g_cache_tests_received;
+ last_ = callbacks_called_;
if (40 == num_iterations_)
MessageLoop::current()->Quit();
}
}
+
+// -----------------------------------------------------------------------
+
+CallbackTest::CallbackTest(MessageLoopHelper* helper,
+ bool reuse)
+ : helper_(helper),
+ reuse_(reuse ? 0 : 1) {
+}
+
+CallbackTest::~CallbackTest() {
+}
+
+// On the actual callback, increase the number of tests received and check for
+// errors (an unexpected test received)
+void CallbackTest::RunWithParams(const Tuple1<int>& params) {
+ if (reuse_) {
+ DCHECK_EQ(1, reuse_);
+ if (2 == reuse_)
+ helper_->set_callback_reused_error(true);
+ reuse_++;
+ }
+
+ helper_->CallbackWasCalled();
+}
diff --git a/net/disk_cache/disk_cache_test_util.h b/net/disk_cache/disk_cache_test_util.h
index 13549c7..0bd937c 100644
--- a/net/disk_cache/disk_cache_test_util.h
+++ b/net/disk_cache/disk_cache_test_util.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -55,24 +55,6 @@ class ScopedTestCache {
// -----------------------------------------------------------------------
-// Simple callback to process IO completions from the cache. It allows tests
-// with multiple simultaneous IO operations.
-class CallbackTest : public CallbackRunner< Tuple1<int> > {
- public:
- explicit CallbackTest(bool reuse);
- virtual ~CallbackTest();
-
- int result() const { return result_; }
- virtual void RunWithParams(const Tuple1<int>& params);
-
- private:
- int result_;
- int reuse_;
- DISALLOW_COPY_AND_ASSIGN(CallbackTest);
-};
-
-// -----------------------------------------------------------------------
-
// Simple helper to deal with the message loop on a test.
class MessageLoopHelper {
public:
@@ -80,9 +62,22 @@ class MessageLoopHelper {
~MessageLoopHelper();
// Run the message loop and wait for num_callbacks before returning. Returns
- // false if we are waiting to long.
+ // false if we are waiting to long. Each callback that will be waited on is
+ // required to call CallbackWasCalled() to indicate when it was called.
bool WaitUntilCacheIoFinished(int num_callbacks);
+ // True if a given callback was called more times than it expected.
+ bool callback_reused_error() const { return callback_reused_error_; }
+ void set_callback_reused_error(bool error) {
+ callback_reused_error_ = error;
+ }
+
+ int callbacks_called() const { return callbacks_called_; }
+ // Report that a callback was called. Each callback that will be waited on
+ // via WaitUntilCacheIoFinished() is expected to call this method to
+ // indicate when it has been executed.
+ void CallbackWasCalled() { ++callbacks_called_; }
+
private:
// Sets the number of callbacks that can be received so far.
void ExpectCallbacks(int num_callbacks) {
@@ -100,7 +95,32 @@ class MessageLoopHelper {
int last_;
bool completed_;
+ // True if a callback was called/reused more than expected.
+ bool callback_reused_error_;
+ int callbacks_called_;
+
DISALLOW_COPY_AND_ASSIGN(MessageLoopHelper);
};
+// -----------------------------------------------------------------------
+
+// Simple callback to process IO completions from the cache. It allows tests
+// with multiple simultaneous IO operations.
+class CallbackTest : public CallbackRunner< Tuple1<int> > {
+ public:
+ // Creates a new CallbackTest object. When the callback is called, it will
+ // update |helper| with the result of the call. If |reuse| is false and a
+ // callback is called more than once, or if |reuse| is true and a callback
+ // is called more than twice, an error will be reported to |helper|.
+ CallbackTest(MessageLoopHelper* helper, bool reuse);
+ virtual ~CallbackTest();
+
+ virtual void RunWithParams(const Tuple1<int>& params);
+
+ private:
+ MessageLoopHelper* helper_;
+ int reuse_;
+ DISALLOW_COPY_AND_ASSIGN(CallbackTest);
+};
+
#endif // NET_DISK_CACHE_DISK_CACHE_TEST_UTIL_H_
diff --git a/net/disk_cache/entry_unittest.cc b/net/disk_cache/entry_unittest.cc
index 9c6b28a..6382779 100644
--- a/net/disk_cache/entry_unittest.cc
+++ b/net/disk_cache/entry_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -18,9 +18,6 @@
using base::Time;
-extern volatile int g_cache_tests_received;
-extern volatile bool g_cache_tests_error;
-
// Tests that can run with different types of caches.
class DiskCacheEntryTest : public DiskCacheTestWithCache {
public:
@@ -157,25 +154,21 @@ void DiskCacheEntryTest::InternalAsyncIO() {
entry->Close();
ASSERT_EQ(net::OK, OpenEntry("the first key", &entry));
- // Let's verify that each IO goes to the right callback object.
- CallbackTest callback1(false);
- CallbackTest callback2(false);
- CallbackTest callback3(false);
- CallbackTest callback4(false);
- CallbackTest callback5(false);
- CallbackTest callback6(false);
- CallbackTest callback7(false);
- CallbackTest callback8(false);
- CallbackTest callback9(false);
- CallbackTest callback10(false);
- CallbackTest callback11(false);
- CallbackTest callback12(false);
- CallbackTest callback13(false);
-
- g_cache_tests_error = false;
- g_cache_tests_received = 0;
-
MessageLoopHelper helper;
+ // Let's verify that each IO goes to the right callback object.
+ CallbackTest callback1(&helper, false);
+ CallbackTest callback2(&helper, false);
+ CallbackTest callback3(&helper, false);
+ CallbackTest callback4(&helper, false);
+ CallbackTest callback5(&helper, false);
+ CallbackTest callback6(&helper, false);
+ CallbackTest callback7(&helper, false);
+ CallbackTest callback8(&helper, false);
+ CallbackTest callback9(&helper, false);
+ CallbackTest callback10(&helper, false);
+ CallbackTest callback11(&helper, false);
+ CallbackTest callback12(&helper, false);
+ CallbackTest callback13(&helper, false);
const int kSize1 = 10;
const int kSize2 = 5000;
@@ -264,8 +257,7 @@ void DiskCacheEntryTest::InternalAsyncIO() {
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
- EXPECT_FALSE(g_cache_tests_error);
- EXPECT_EQ(expected, g_cache_tests_received);
+ EXPECT_FALSE(helper.callback_reused_error());
entry->Doom();
entry->Close();
@@ -364,22 +356,19 @@ void DiskCacheEntryTest::ExternalAsyncIO() {
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
- // Let's verify that each IO goes to the right callback object.
- CallbackTest callback1(false);
- CallbackTest callback2(false);
- CallbackTest callback3(false);
- CallbackTest callback4(false);
- CallbackTest callback5(false);
- CallbackTest callback6(false);
- CallbackTest callback7(false);
- CallbackTest callback8(false);
- CallbackTest callback9(false);
-
- g_cache_tests_error = false;
- g_cache_tests_received = 0;
int expected = 0;
MessageLoopHelper helper;
+ // Let's verify that each IO goes to the right callback object.
+ CallbackTest callback1(&helper, false);
+ CallbackTest callback2(&helper, false);
+ CallbackTest callback3(&helper, false);
+ CallbackTest callback4(&helper, false);
+ CallbackTest callback5(&helper, false);
+ CallbackTest callback6(&helper, false);
+ CallbackTest callback7(&helper, false);
+ CallbackTest callback8(&helper, false);
+ CallbackTest callback9(&helper, false);
const int kSize1 = 17000;
const int kSize2 = 25000;
@@ -448,8 +437,7 @@ void DiskCacheEntryTest::ExternalAsyncIO() {
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_EQ(37000, entry->GetDataSize(1));
- EXPECT_FALSE(g_cache_tests_error);
- EXPECT_EQ(expected, g_cache_tests_received);
+ EXPECT_FALSE(helper.callback_reused_error());
entry->Doom();
entry->Close();
diff --git a/net/disk_cache/mapped_file_unittest.cc b/net/disk_cache/mapped_file_unittest.cc
index 5802671..fefc383 100644
--- a/net/disk_cache/mapped_file_unittest.cc
+++ b/net/disk_cache/mapped_file_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -12,28 +12,33 @@
namespace {
-int g_cache_tests_max_id;
-volatile int g_cache_tests_received;
-volatile bool g_cache_tests_error;
-
// Implementation of FileIOCallback for the tests.
class FileCallbackTest: public disk_cache::FileIOCallback {
public:
- explicit FileCallbackTest(int id) : id_(id) {}
- ~FileCallbackTest() {}
+ explicit FileCallbackTest(int id,
+ MessageLoopHelper* helper,
+ int* max_id)
+ : id_(id),
+ helper_(helper),
+ max_id_(max_id) {
+ }
+ virtual ~FileCallbackTest() {}
virtual void OnFileIOComplete(int bytes_copied);
+
private:
int id_;
+ MessageLoopHelper* helper_;
+ int* max_id_;
};
void FileCallbackTest::OnFileIOComplete(int bytes_copied) {
- if (id_ > g_cache_tests_max_id) {
+ if (id_ > *max_id_) {
NOTREACHED();
- g_cache_tests_error = true;
+ helper_->set_callback_reused_error(true);
}
- g_cache_tests_received++;
+ helper_->CallbackWasCalled();
}
} // namespace
@@ -59,12 +64,9 @@ TEST_F(DiskCacheTest, MappedFile_AsyncIO) {
ASSERT_TRUE(CreateCacheTestFile(filename));
ASSERT_TRUE(file->Init(filename, 8192));
- FileCallbackTest callback(1);
- g_cache_tests_error = false;
- g_cache_tests_max_id = 0;
- g_cache_tests_received = 0;
-
+ int max_id = 0;
MessageLoopHelper helper;
+ FileCallbackTest callback(1, &helper, &max_id);
char buffer1[20];
char buffer2[20];
@@ -75,7 +77,7 @@ TEST_F(DiskCacheTest, MappedFile_AsyncIO) {
&completed));
int expected = completed ? 0 : 1;
- g_cache_tests_max_id = 1;
+ max_id = 1;
helper.WaitUntilCacheIoFinished(expected);
EXPECT_TRUE(file->Read(buffer2, sizeof(buffer2), 1024 * 1024, &callback,
@@ -85,7 +87,7 @@ TEST_F(DiskCacheTest, MappedFile_AsyncIO) {
helper.WaitUntilCacheIoFinished(expected);
- EXPECT_EQ(expected, g_cache_tests_received);
- EXPECT_FALSE(g_cache_tests_error);
+ EXPECT_EQ(expected, helper.callbacks_called());
+ EXPECT_FALSE(helper.callback_reused_error());
EXPECT_STREQ(buffer1, buffer2);
}