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

Side by Side Diff: components/sync/driver/glue/browser_thread_model_worker_unittest.cc

Issue 2503423004: [Sync] Move sync's ModelSafeWorker implementations to engine/. (Closed)
Patch Set: Rebase. Created 4 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
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/sync/driver/glue/browser_thread_model_worker.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/test/test_timeouts.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_task_runner_handle.h"
17 #include "base/timer/timer.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using base::Thread;
21 using base::TimeDelta;
22
23 namespace syncer {
24
25 namespace {
26
27 class SyncBrowserThreadModelWorkerTest : public testing::Test {
28 public:
29 SyncBrowserThreadModelWorkerTest()
30 : db_thread_("DB_Thread"), did_do_work_(false), weak_factory_(this) {}
31
32 bool did_do_work() { return did_do_work_; }
33 BrowserThreadModelWorker* worker() { return worker_.get(); }
34 base::OneShotTimer* timer() { return &timer_; }
35 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest>* factory() {
36 return &weak_factory_;
37 }
38
39 // Schedule DoWork to be executed on the DB thread and have the test fail if
40 // DoWork hasn't executed within action_timeout().
41 void ScheduleWork() {
42 // We wait until the callback is done. So it is safe to use unretained.
43 WorkCallback c = base::Bind(&SyncBrowserThreadModelWorkerTest::DoWork,
44 base::Unretained(this));
45 timer()->Start(FROM_HERE, TestTimeouts::action_timeout(), this,
46 &SyncBrowserThreadModelWorkerTest::Timeout);
47 worker()->DoWorkAndWaitUntilDone(c);
48 }
49
50 // This is the work that will be scheduled to be done on the DB thread.
51 SyncerError DoWork() {
52 EXPECT_TRUE(db_thread_.task_runner()->BelongsToCurrentThread());
53 timer_.Stop(); // Stop the failure timer so the test succeeds.
54 main_message_loop_.task_runner()->PostTask(
55 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
56 did_do_work_ = true;
57 return SYNCER_OK;
58 }
59
60 // This will be called by the OneShotTimer and make the test fail unless
61 // DoWork is called first.
62 void Timeout() {
63 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread.";
64 main_message_loop_.task_runner()->PostTask(
65 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
66 }
67
68 protected:
69 void SetUp() override {
70 db_thread_.Start();
71 worker_ = new BrowserThreadModelWorker(db_thread_.task_runner(), GROUP_DB);
72 }
73
74 virtual void Teardown() {
75 worker_ = nullptr;
76 db_thread_.Stop();
77 }
78
79 private:
80 base::MessageLoop main_message_loop_;
81 Thread db_thread_;
82 bool did_do_work_;
83 scoped_refptr<BrowserThreadModelWorker> worker_;
84 base::OneShotTimer timer_;
85
86 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest> weak_factory_;
87 };
88
89 TEST_F(SyncBrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) {
90 base::ThreadTaskRunnerHandle::Get()->PostTask(
91 FROM_HERE, base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork,
92 factory()->GetWeakPtr()));
93 base::RunLoop().Run();
94 EXPECT_TRUE(did_do_work());
95 }
96
97 } // namespace
98
99 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698