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

Side by Side Diff: components/sync/engine/browser_thread_model_worker_unittest.cc

Issue 2942773002: Task Scheduler API migration: change BrowserThreadModelWorker to SequencedModelWorker (Closed)
Patch Set: Updated more comments to reflect thread -> sequence migration. Created 3 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
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/engine/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/test/test_timeouts.h"
14 #include "base/threading/thread.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "base/timer/timer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using base::Thread;
20 using base::TimeDelta;
21
22 namespace syncer {
23
24 namespace {
25
26 class SyncBrowserThreadModelWorkerTest : public testing::Test {
27 public:
28 SyncBrowserThreadModelWorkerTest()
29 : db_thread_("DB_Thread"), did_do_work_(false), weak_factory_(this) {}
30
31 bool did_do_work() { return did_do_work_; }
32 BrowserThreadModelWorker* worker() { return worker_.get(); }
33 base::OneShotTimer* timer() { return &timer_; }
34 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest>* factory() {
35 return &weak_factory_;
36 }
37
38 // Schedule DoWork to be executed on the DB thread and have the test fail if
39 // DoWork hasn't executed within action_timeout().
40 void ScheduleWork() {
41 // We wait until the callback is done. So it is safe to use unretained.
42 timer()->Start(FROM_HERE, TestTimeouts::action_timeout(), this,
43 &SyncBrowserThreadModelWorkerTest::Timeout);
44 worker()->DoWorkAndWaitUntilDone(base::BindOnce(
45 &SyncBrowserThreadModelWorkerTest::DoWork, base::Unretained(this)));
46 }
47
48 // This is the work that will be scheduled to be done on the DB thread.
49 SyncerError DoWork() {
50 EXPECT_TRUE(db_thread_.task_runner()->BelongsToCurrentThread());
51 main_message_loop_.task_runner()->PostTask(
52 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
53 did_do_work_ = true;
54 return SYNCER_OK;
55 }
56
57 // This will be called by the OneShotTimer and make the test fail unless
58 // DoWork is called first.
59 void Timeout() {
60 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread.";
61 main_message_loop_.task_runner()->PostTask(
62 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
63 }
64
65 protected:
66 void SetUp() override {
67 db_thread_.Start();
68 worker_ = new BrowserThreadModelWorker(db_thread_.task_runner(), GROUP_DB);
69 }
70
71 virtual void Teardown() {
72 worker_ = nullptr;
73 db_thread_.Stop();
74 }
75
76 private:
77 base::MessageLoop main_message_loop_;
78 Thread db_thread_;
79 bool did_do_work_;
80 scoped_refptr<BrowserThreadModelWorker> worker_;
81 base::OneShotTimer timer_;
82
83 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest> weak_factory_;
84 };
85
86 TEST_F(SyncBrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) {
87 base::ThreadTaskRunnerHandle::Get()->PostTask(
88 FROM_HERE, base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork,
89 factory()->GetWeakPtr()));
90 base::RunLoop().Run();
91 EXPECT_TRUE(did_do_work());
92 }
93
94 } // namespace
95
96 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/engine/browser_thread_model_worker.cc ('k') | components/sync/engine/model_safe_worker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698