Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1555)

Side by Side Diff: chrome/browser/nacl_host/pnacl_translation_cache_unittest.cc

Issue 15647018: Add read support to PNaClTranslationCache (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/nacl_host/pnacl_translation_cache.h" 5 #include "chrome/browser/nacl_host/pnacl_translation_cache.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/test/test_browser_thread.h" 12 #include "content/public/test/test_browser_thread.h"
13 #include "net/base/test_completion_callback.h" 13 #include "net/base/test_completion_callback.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 using content::BrowserThread; 16 using content::BrowserThread;
17 using base::FilePath;
17 18
18 namespace pnacl_cache { 19 namespace pnacl_cache {
19 20
20 class PNaClTranslationCacheTest : public testing::Test { 21 class PNaClTranslationCacheTest : public testing::Test {
21 protected: 22 protected:
22 PNaClTranslationCacheTest() 23 PNaClTranslationCacheTest()
23 : cache_thread_(BrowserThread::CACHE, &message_loop_), 24 : cache_thread_(BrowserThread::CACHE, &message_loop_),
24 io_thread_(BrowserThread::IO, &message_loop_) {} 25 io_thread_(BrowserThread::IO, &message_loop_) {}
25 virtual ~PNaClTranslationCacheTest() {} 26 virtual ~PNaClTranslationCacheTest() {}
26 virtual void SetUp() { cache_ = new PNaClTranslationCache(); } 27 virtual void SetUp() { cache_ = new PNaClTranslationCache(); }
27 virtual void TearDown() { 28 virtual void TearDown() {
28 // The destructor of PNaClTranslationCacheWriteEntry posts a task to the IO 29 // The destructor of PNaClTranslationCacheWriteEntry posts a task to the IO
29 // thread to close the backend cache entry. We want to make sure the entries 30 // thread to close the backend cache entry. We want to make sure the entries
30 // are closed before we delete the backend (and in particular the destructor 31 // are closed before we delete the backend (and in particular the destructor
31 // for the memory backend has a DCHECK to verify this), so we run the loop 32 // for the memory backend has a DCHECK to verify this), so we run the loop
32 // here to ensure the task gets processed. 33 // here to ensure the task gets processed.
33 base::RunLoop().RunUntilIdle(); 34 base::RunLoop().RunUntilIdle();
34 delete cache_; 35 delete cache_;
35 } 36 }
36 37
38 void InitBackend(bool in_mem);
39 void StoreNexe(const std::string& key, const std::string& nexe);
40 std::string GetNexe(const std::string& key);
37 protected: 41 protected:
38 PNaClTranslationCache* cache_; 42 PNaClTranslationCache* cache_;
39 base::MessageLoopForIO message_loop_; 43 base::MessageLoopForIO message_loop_;
40 content::TestBrowserThread cache_thread_; 44 content::TestBrowserThread cache_thread_;
41 content::TestBrowserThread io_thread_; 45 content::TestBrowserThread io_thread_;
46 base::ScopedTempDir temp_dir_;
42 }; 47 };
43 48
44 TEST_F(PNaClTranslationCacheTest, StoreOneInMem) { 49 void PNaClTranslationCacheTest::InitBackend(bool in_mem) {
45 net::TestCompletionCallback init_cb; 50 net::TestCompletionCallback init_cb;
46 int rv = cache_->InitCache(base::FilePath(), true, init_cb.callback()); 51 if (!in_mem) {
47 EXPECT_EQ(net::OK, rv); 52 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
53 }
54 int rv = cache_->InitCache(temp_dir_.path(), in_mem, init_cb.callback());
55 if (in_mem)
56 ASSERT_EQ(net::OK, rv);
48 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); 57 ASSERT_EQ(net::OK, init_cb.GetResult(rv));
58 ASSERT_EQ(0, cache_->Size());
59 }
60
61 void PNaClTranslationCacheTest::StoreNexe(const std::string& key,
62 const std::string& nexe) {
49 net::TestCompletionCallback store_cb; 63 net::TestCompletionCallback store_cb;
50 EXPECT_EQ(0, cache_->Size()); 64 cache_->StoreNexe(key, nexe, store_cb.callback());
51 cache_->StoreNexe("1", "a", store_cb.callback());
52 // Using ERR_IO_PENDING here causes the callback to wait for the result 65 // Using ERR_IO_PENDING here causes the callback to wait for the result
53 // which should be harmless even if it returns OK immediately. This is because 66 // which should be harmless even if it returns OK immediately. This is because
54 // we don't plumb the intermediate writing stages all the way out. 67 // we don't plumb the intermediate writing stages all the way out.
55 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); 68 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING));
69 }
70
71 std::string PNaClTranslationCacheTest::GetNexe(const std::string& key) {
72 net::TestCompletionCallback load_cb;
73 std::string nexe;
74 cache_->GetNexe(key, &nexe, load_cb.callback());
75 EXPECT_EQ(net::OK, load_cb.GetResult(net::ERR_IO_PENDING));
76 return nexe;
77 }
78
79 static const std::string test_key("1");
80 static const std::string test_store_val("testnexe");
81
82 TEST_F(PNaClTranslationCacheTest, StoreSmallInMem) {
83 // Test that a single store puts something in the mem backend
84 InitBackend(true);
85 StoreNexe(test_key, test_store_val);
56 EXPECT_EQ(1, cache_->Size()); 86 EXPECT_EQ(1, cache_->Size());
57 } 87 }
58 88
59 TEST_F(PNaClTranslationCacheTest, StoreOneOnDisk) { 89 TEST_F(PNaClTranslationCacheTest, StoreSmallOnDisk) {
60 base::ScopedTempDir temp_dir; 90 // Test that a single store puts something in the disk backend
61 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 91 InitBackend(false);
62 net::TestCompletionCallback init_cb; 92 StoreNexe(test_key, test_store_val);
63 int rv = cache_->InitCache(temp_dir.path(), false, init_cb.callback()); 93 EXPECT_EQ(1, cache_->Size());
64 EXPECT_TRUE(rv); 94 }
65 ASSERT_EQ(net::OK, init_cb.GetResult(rv)); 95
66 EXPECT_EQ(0, cache_->Size()); 96 TEST_F(PNaClTranslationCacheTest, StoreLargeOnDisk) {
67 net::TestCompletionCallback store_cb; 97 // Test a value too large(?) for a single I/O operation
68 cache_->StoreNexe("1", "a", store_cb.callback()); 98 // TODO(dschuff): we only seem to ever have one operation go through into the
69 EXPECT_EQ(net::OK, store_cb.GetResult(net::ERR_IO_PENDING)); 99 // backend. Find out what the 'offset' field means, and if it can ever require
100 // multiple writes.
101 InitBackend(false);
102 const std::string large_buffer(kMaxMemCacheSize + 1, 'a');
103 StoreNexe(test_key, large_buffer);
70 EXPECT_EQ(1, cache_->Size()); 104 EXPECT_EQ(1, cache_->Size());
71 } 105 }
72 106
73 TEST_F(PNaClTranslationCacheTest, InMemSizeLimit) { 107 TEST_F(PNaClTranslationCacheTest, InMemSizeLimit) {
74 net::TestCompletionCallback init_cb; 108 InitBackend(true);
75 int rv = cache_->InitCache(base::FilePath(), true, init_cb.callback()); 109 const std::string large_buffer(kMaxMemCacheSize + 1, 'a');
76 EXPECT_EQ(rv, net::OK);
77 ASSERT_EQ(init_cb.GetResult(rv), net::OK);
78 EXPECT_EQ(cache_->Size(), 0);
79 std::string large_buffer(kMaxMemCacheSize + 1, 'a');
80 net::TestCompletionCallback store_cb; 110 net::TestCompletionCallback store_cb;
81 cache_->StoreNexe("1", large_buffer, store_cb.callback()); 111 cache_->StoreNexe(test_key, large_buffer, store_cb.callback());
82 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING)); 112 EXPECT_EQ(net::ERR_FAILED, store_cb.GetResult(net::ERR_IO_PENDING));
83 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed. 113 base::RunLoop().RunUntilIdle(); // Ensure the entry is closed.
84 EXPECT_EQ(0, cache_->Size()); 114 EXPECT_EQ(0, cache_->Size());
85 } 115 }
86 116
117 TEST_F(PNaClTranslationCacheTest, GetOneInMem) {
118 InitBackend(true);
119 StoreNexe(test_key, test_store_val);
120 EXPECT_EQ(1, cache_->Size());
121 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
122 }
123
124 TEST_F(PNaClTranslationCacheTest, GetLargeOnDisk) {
125 InitBackend(false);
126 const std::string large_buffer(kMaxMemCacheSize + 1, 'a');
127 StoreNexe(test_key, large_buffer);
128 EXPECT_EQ(1, cache_->Size());
129 EXPECT_EQ(0, GetNexe(test_key).compare(large_buffer));
130 }
131
132 TEST_F(PNaClTranslationCacheTest, StoreTwice) {
133 // Test that storing twice with the same key overwrites
134 InitBackend(true);
135 StoreNexe(test_key, test_store_val);
136 StoreNexe(test_key, test_store_val + "aaa");
137 EXPECT_EQ(1, cache_->Size());
138 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val + "aaa"));
139 }
140
141 TEST_F(PNaClTranslationCacheTest, StoreTwo) {
142 InitBackend(true);
143 StoreNexe(test_key, test_store_val);
144 StoreNexe(test_key + "a", test_store_val + "aaa");
145 EXPECT_EQ(2, cache_->Size());
146 EXPECT_EQ(0, GetNexe(test_key).compare(test_store_val));
147 EXPECT_EQ(0, GetNexe(test_key + "a").compare(test_store_val + "aaa"));
148 }
149
150 TEST_F(PNaClTranslationCacheTest, GetMiss) {
151 InitBackend(true);
152 StoreNexe(test_key, test_store_val);
153 net::TestCompletionCallback load_cb;
154 std::string nexe;
155 cache_->GetNexe("a", &nexe, load_cb.callback());
jvoung (off chromium) 2013/06/05 00:42:28 Maybe use 'test_key + "a"' as the non-existent key
Derek Schuff 2013/06/05 05:01:59 Done.
156 EXPECT_EQ(net::ERR_FAILED, load_cb.GetResult(net::ERR_IO_PENDING));
157 }
158
87 } // namespace nacl_cache 159 } // namespace nacl_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698