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

Side by Side Diff: components/dom_distiller/core/dom_distiller_database_unittest.cc

Issue 56193004: Re-work the thread restrictions on the DOM distiller database (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month 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 "components/dom_distiller/core/dom_distiller_database.h" 5 #include "components/dom_distiller/core/dom_distiller_database.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/threading/thread.h"
13 #include "components/dom_distiller/core/article_entry.h" 14 #include "components/dom_distiller/core/article_entry.h"
14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 using base::MessageLoop; 18 using base::MessageLoop;
18 using base::ScopedTempDir; 19 using base::ScopedTempDir;
19 using testing::Invoke; 20 using testing::Invoke;
20 using testing::Return; 21 using testing::Return;
21 using testing::_; 22 using testing::_;
22 23
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 std::string serialized_actual = actual[i].SerializeAsString(); 81 std::string serialized_actual = actual[i].SerializeAsString();
81 EXPECT_EQ(serialized_expected, serialized_actual); 82 EXPECT_EQ(serialized_expected, serialized_actual);
82 expected.erase(expected_it); 83 expected.erase(expected_it);
83 } 84 }
84 } 85 }
85 86
86 class DomDistillerDatabaseTest : public testing::Test { 87 class DomDistillerDatabaseTest : public testing::Test {
87 public: 88 public:
88 virtual void SetUp() { 89 virtual void SetUp() {
89 main_loop_.reset(new MessageLoop()); 90 main_loop_.reset(new MessageLoop());
90 db_ = new DomDistillerDatabase(main_loop_->message_loop_proxy()); 91 db_.reset(new DomDistillerDatabase(main_loop_->message_loop_proxy()));
91 } 92 }
92 93
93 virtual void TearDown() { 94 virtual void TearDown() {
94 DestroyDB(); 95 db_.reset();
95 main_loop_.reset(NULL); 96 base::RunLoop().RunUntilIdle();
97 main_loop_.reset();
96 } 98 }
97 99
98 void DestroyDB() { 100 scoped_ptr<DomDistillerDatabase> db_;
99 if (db_) {
100 db_->Destroy();
101 base::RunLoop().RunUntilIdle();
102 db_ = NULL;
103 }
104 }
105
106 DomDistillerDatabase* db_;
107 scoped_ptr<MessageLoop> main_loop_; 101 scoped_ptr<MessageLoop> main_loop_;
108 }; 102 };
109 103
110 // Test that DomDistillerDatabase calls Init on the underlying database and that 104 // Test that DomDistillerDatabase calls Init on the underlying database and that
111 // the caller's InitCallback is called with the correct value. 105 // the caller's InitCallback is called with the correct value.
112 TEST_F(DomDistillerDatabaseTest, TestDBInitSuccess) { 106 TEST_F(DomDistillerDatabaseTest, TestDBInitSuccess) {
113 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 107 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
114 108
115 MockDB* mock_db = new MockDB(); 109 MockDB* mock_db = new MockDB();
116 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(true)); 110 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(true));
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 251
258 EXPECT_CALL(*mock_db, Save(_)).WillOnce(Return(false)); 252 EXPECT_CALL(*mock_db, Save(_)).WillOnce(Return(false));
259 EXPECT_CALL(caller, SaveCallback(false)); 253 EXPECT_CALL(caller, SaveCallback(false));
260 db_->SaveEntries( 254 db_->SaveEntries(
261 entries.Pass(), 255 entries.Pass(),
262 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); 256 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
263 257
264 base::RunLoop().RunUntilIdle(); 258 base::RunLoop().RunUntilIdle();
265 } 259 }
266 260
261 // This tests that normal usage of the real database does not cause any
262 // threading violations.
263 TEST(DomDistillerDatabaseThreadingTest, TestDBDestruction) {
264 base::MessageLoop main_loop;
265
266 ScopedTempDir temp_dir;
267 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
268
269 base::Thread db_thread("dbthread");
270 ASSERT_TRUE(db_thread.Start());
271
272 scoped_ptr<DomDistillerDatabase> db(
273 new DomDistillerDatabase(db_thread.message_loop_proxy()));
Nico 2013/11/04 19:31:56 Can this be on the stack too? (In a local block, s
cjhopman 2013/11/05 19:08:29 It could be. I feel like that is subtle enough tha
274
275 MockDatabaseCaller caller;
276 EXPECT_CALL(caller, InitCallback(_));
277 db->Init(
278 temp_dir.path(),
279 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
280
281 db.reset();
282
283 base::RunLoop run_loop;
284 db_thread.message_loop_proxy()->PostTaskAndReply(
285 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure());
286 run_loop.Run();
287 }
288
267 // Test that the LevelDB properly saves entries and that load returns the saved 289 // Test that the LevelDB properly saves entries and that load returns the saved
268 // entries. If |close_after_save| is true, the database will be closed after 290 // entries. If |close_after_save| is true, the database will be closed after
269 // saving and then re-opened to ensure that the data is properly persisted. 291 // saving and then re-opened to ensure that the data is properly persisted.
270 void TestLevelDBSaveAndLoad(bool close_after_save) { 292 void TestLevelDBSaveAndLoad(bool close_after_save) {
271 ScopedTempDir temp_dir; 293 ScopedTempDir temp_dir;
272 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 294 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
273 295
274 EntryMap model = GetSmallModel(); 296 EntryMap model = GetSmallModel();
275 EntryVector save_entries; 297 EntryVector save_entries;
276 EntryVector load_entries; 298 EntryVector load_entries;
(...skipping 19 matching lines...) Expand all
296 318
297 TEST(DomDistillerDatabaseLevelDBTest, TestDBSaveAndLoad) { 319 TEST(DomDistillerDatabaseLevelDBTest, TestDBSaveAndLoad) {
298 TestLevelDBSaveAndLoad(false); 320 TestLevelDBSaveAndLoad(false);
299 } 321 }
300 322
301 TEST(DomDistillerDatabaseLevelDBTest, TestDBCloseAndReopen) { 323 TEST(DomDistillerDatabaseLevelDBTest, TestDBCloseAndReopen) {
302 TestLevelDBSaveAndLoad(true); 324 TestLevelDBSaveAndLoad(true);
303 } 325 }
304 326
305 } // namespace dom_distiller 327 } // namespace dom_distiller
OLDNEW
« no previous file with comments | « components/dom_distiller/core/dom_distiller_database.cc ('k') | components/dom_distiller/core/dom_distiller_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698