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

Side by Side Diff: chrome/browser/sync/glue/bookmark_model_worker_unittest.cc

Issue 553015: Support for multiple sync ModelSafeWorkers.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 11 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
(Empty)
1 // Copyright (c) 2006-2009 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 "base/thread.h"
6 #include "chrome/browser/sync/engine/syncapi.h"
7 #include "chrome/browser/sync/glue/bookmark_model_worker.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 using browser_sync::BookmarkModelWorker;
11 using namespace sync_api;
12
13 // Various boilerplate, primarily for the StopWithPendingWork test.
14
15 class BookmarkModelWorkerVisitor : public ModelSafeWorkerInterface::Visitor {
16 public:
17 BookmarkModelWorkerVisitor(MessageLoop* faux_ui_loop,
18 base::WaitableEvent* was_run,
19 bool quit_loop)
20 : faux_ui_loop_(faux_ui_loop), quit_loop_when_run_(quit_loop),
21 was_run_(was_run) { }
22 virtual ~BookmarkModelWorkerVisitor() { }
23
24 virtual void DoWork() {
25 EXPECT_EQ(MessageLoop::current(), faux_ui_loop_);
26 was_run_->Signal();
27 if (quit_loop_when_run_)
28 MessageLoop::current()->Quit();
29 }
30
31 private:
32 MessageLoop* faux_ui_loop_;
33 bool quit_loop_when_run_;
34 base::WaitableEvent* was_run_;
35 DISALLOW_COPY_AND_ASSIGN(BookmarkModelWorkerVisitor);
36 };
37
38 // A faux-syncer that only interacts with its model safe worker.
39 class Syncer {
40 public:
41 explicit Syncer(BookmarkModelWorker* worker) : worker_(worker) {}
42 ~Syncer() {}
43
44 void SyncShare(BookmarkModelWorkerVisitor* visitor) {
45 worker_->CallDoWorkFromModelSafeThreadAndWait(visitor);
46 }
47 private:
48 BookmarkModelWorker* worker_;
49 DISALLOW_COPY_AND_ASSIGN(Syncer);
50 };
51
52 // A task run from the SyncerThread to "sync share", ie tell the Syncer to
53 // ask it's ModelSafeWorker to do something.
54 class FakeSyncShareTask : public Task {
55 public:
56 FakeSyncShareTask(Syncer* syncer, BookmarkModelWorkerVisitor* visitor)
57 : syncer_(syncer), visitor_(visitor) {
58 }
59 virtual void Run() {
60 syncer_->SyncShare(visitor_);
61 }
62 private:
63 Syncer* syncer_;
64 BookmarkModelWorkerVisitor* visitor_;
65 DISALLOW_COPY_AND_ASSIGN(FakeSyncShareTask);
66 };
67
68 // A task run from the CoreThread to simulate terminating syncapi.
69 class FakeSyncapiShutdownTask : public Task {
70 public:
71 FakeSyncapiShutdownTask(base::Thread* syncer_thread,
72 BookmarkModelWorker* worker,
73 base::WaitableEvent** jobs,
74 size_t job_count)
75 : syncer_thread_(syncer_thread), worker_(worker), jobs_(jobs),
76 job_count_(job_count), all_jobs_done_(false, false) { }
77 virtual void Run() {
78 // In real life, we would try and close a sync directory, which would
79 // result in the syncer calling it's own destructor, which results in
80 // the SyncerThread::HaltSyncer being called, which sets the
81 // syncer in RequestEarlyExit mode and waits until the Syncer finishes
82 // SyncShare to remove the syncer from it's watch. Here we just manually
83 // wait until all outstanding jobs are done to simulate what happens in
84 // SyncerThread::HaltSyncer.
85 all_jobs_done_.WaitMany(jobs_, job_count_);
86
87 // These two calls are made from SyncBackendHost::Core::DoShutdown.
88 syncer_thread_->Stop();
89 worker_->OnSyncerShutdownComplete();
90 }
91 private:
92 base::Thread* syncer_thread_;
93 BookmarkModelWorker* worker_;
94 base::WaitableEvent** jobs_;
95 size_t job_count_;
96 base::WaitableEvent all_jobs_done_;
97 DISALLOW_COPY_AND_ASSIGN(FakeSyncapiShutdownTask);
98 };
99
100 class BookmarkModelWorkerTest : public testing::Test {
101 public:
102 BookmarkModelWorkerTest() : faux_syncer_thread_("FauxSyncerThread"),
103 faux_core_thread_("FauxCoreThread") { }
104
105 virtual void SetUp() {
106 faux_syncer_thread_.Start();
107 bmw_.reset(new BookmarkModelWorker(&faux_ui_loop_));
108 syncer_.reset(new Syncer(bmw_.get()));
109 }
110
111 Syncer* syncer() { return syncer_.get(); }
112 BookmarkModelWorker* bmw() { return bmw_.get(); }
113 base::Thread* core_thread() { return &faux_core_thread_; }
114 base::Thread* syncer_thread() { return &faux_syncer_thread_; }
115 MessageLoop* ui_loop() { return &faux_ui_loop_; }
116 private:
117 MessageLoop faux_ui_loop_;
118 base::Thread faux_syncer_thread_;
119 base::Thread faux_core_thread_;
120 scoped_ptr<BookmarkModelWorker> bmw_;
121 scoped_ptr<Syncer> syncer_;
122 };
123
124 TEST_F(BookmarkModelWorkerTest, ScheduledWorkRunsOnUILoop) {
125 base::WaitableEvent v_was_run(false, false);
126 scoped_ptr<BookmarkModelWorkerVisitor> v(
127 new BookmarkModelWorkerVisitor(ui_loop(), &v_was_run, true));
128
129 syncer_thread()->message_loop()->PostTask(FROM_HERE,
130 new FakeSyncShareTask(syncer(), v.get()));
131
132 // We are on the UI thread, so run our loop to process the
133 // (hopefully) scheduled task from a SyncShare invocation.
134 MessageLoop::current()->Run();
135
136 bmw()->OnSyncerShutdownComplete();
137 bmw()->Stop();
138 syncer_thread()->Stop();
139 }
140
141 TEST_F(BookmarkModelWorkerTest, StopWithPendingWork) {
142 // What we want to set up is the following:
143 // ("ui_thread" is the thread we are currently executing on)
144 // 1 - simulate the user shutting down the browser, and the ui thread needing
145 // to terminate the core thread.
146 // 2 - the core thread is where the syncapi is accessed from, and so it needs
147 // to shut down the SyncerThread.
148 // 3 - the syncer is waiting on the BookmarkModelWorker to
149 // perform a task for it.
150 // The BookmarkModelWorker's manual shutdown pump will save the day, as the
151 // UI thread is not actually trying to join() the core thread, it is merely
152 // waiting for the SyncerThread to give it work or to finish. After that, it
153 // will join the core thread which should succeed as the SyncerThread has left
154 // the building. Unfortunately this test as written is not provably decidable,
155 // as it will always halt on success, but it may not on failure (namely if
156 // the task scheduled by the Syncer is _never_ run).
157 core_thread()->Start();
158 base::WaitableEvent v_ran(false, false);
159 scoped_ptr<BookmarkModelWorkerVisitor> v(new BookmarkModelWorkerVisitor(
160 ui_loop(), &v_ran, false));
161 base::WaitableEvent* jobs[] = { &v_ran };
162
163 // The current message loop is not running, so queue a task to cause
164 // BookmarkModelWorker::Stop() to play a crucial role. See comment below.
165 syncer_thread()->message_loop()->PostTask(FROM_HERE,
166 new FakeSyncShareTask(syncer(), v.get()));
167
168 // This is what gets the core_thread blocked on the syncer_thread.
169 core_thread()->message_loop()->PostTask(FROM_HERE,
170 new FakeSyncapiShutdownTask(syncer_thread(), bmw(), jobs, 1));
171
172 // This is what gets the UI thread blocked until NotifyExitRequested,
173 // which is called when FakeSyncapiShutdownTask runs and deletes the syncer.
174 bmw()->Stop();
175
176 EXPECT_FALSE(syncer_thread()->IsRunning());
177 core_thread()->Stop();
178 }
179
180 TEST_F(BookmarkModelWorkerTest, HypotheticalManualPumpFlooding) {
181 // This situation should not happen in real life because the Syncer should
182 // never send more than one CallDoWork notification after early_exit_requested
183 // has been set, but our BookmarkModelWorker is built to handle this case
184 // nonetheless. It may be needed in the future, and since we support it and
185 // it is not actually exercised in the wild this test is essential.
186 // It is identical to above except we schedule more than one visitor.
187 core_thread()->Start();
188
189 // Our ammunition.
190 base::WaitableEvent fox1_ran(false, false);
191 scoped_ptr<BookmarkModelWorkerVisitor> fox1(new BookmarkModelWorkerVisitor(
192 ui_loop(), &fox1_ran, false));
193 base::WaitableEvent fox2_ran(false, false);
194 scoped_ptr<BookmarkModelWorkerVisitor> fox2(new BookmarkModelWorkerVisitor(
195 ui_loop(), &fox2_ran, false));
196 base::WaitableEvent fox3_ran(false, false);
197 scoped_ptr<BookmarkModelWorkerVisitor> fox3(new BookmarkModelWorkerVisitor(
198 ui_loop(), &fox3_ran, false));
199 base::WaitableEvent* jobs[] = { &fox1_ran, &fox2_ran, &fox3_ran };
200
201 // The current message loop is not running, so queue a task to cause
202 // BookmarkModelWorker::Stop() to play a crucial role. See comment below.
203 syncer_thread()->message_loop()->PostTask(FROM_HERE,
204 new FakeSyncShareTask(syncer(), fox1.get()));
205 syncer_thread()->message_loop()->PostTask(FROM_HERE,
206 new FakeSyncShareTask(syncer(), fox2.get()));
207
208 // This is what gets the core_thread blocked on the syncer_thread.
209 core_thread()->message_loop()->PostTask(FROM_HERE,
210 new FakeSyncapiShutdownTask(syncer_thread(), bmw(), jobs, 3));
211 syncer_thread()->message_loop()->PostTask(FROM_HERE,
212 new FakeSyncShareTask(syncer(), fox3.get()));
213
214 // This is what gets the UI thread blocked until NotifyExitRequested,
215 // which is called when FakeSyncapiShutdownTask runs and deletes the syncer.
216 bmw()->Stop();
217
218 // Was the thread killed?
219 EXPECT_FALSE(syncer_thread()->IsRunning());
220 core_thread()->Stop();
221 }
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/bookmark_model_worker.cc ('k') | chrome/browser/sync/glue/sync_backend_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698