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

Side by Side Diff: extensions/browser/content_hash_fetcher_unittest.cc

Issue 2824033002: Ref CL: Post Task synchronously executing from blocking pool. (Closed)
Patch Set: remove dummy lock as it is not necessary Created 3 years, 8 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <memory> 5 #include <memory>
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/callback_helpers.h" 8 #include "base/callback_helpers.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 EXPECT_TRUE(result->force); 273 EXPECT_TRUE(result->force);
274 EXPECT_TRUE( 274 EXPECT_TRUE(
275 base::ContainsKey(result->mismatch_paths, script_path.BaseName())); 275 base::ContainsKey(result->mismatch_paths, script_path.BaseName()));
276 276
277 // Make sure the verified_contents.json file was written into the extension's 277 // Make sure the verified_contents.json file was written into the extension's
278 // install dir. 278 // install dir.
279 EXPECT_TRUE( 279 EXPECT_TRUE(
280 base::PathExists(file_util::GetVerifiedContentsPath(extension->path()))); 280 base::PathExists(file_util::GetVerifiedContentsPath(extension->path())));
281 } 281 }
282 282
283 class Job : public base::RefCountedThreadSafe<Job> {
284 public:
285 using JobCallback = base::Callback<void(bool)>;
286 explicit Job(const JobCallback& callback) : job_callback_(callback) {
287 bool got_id =
288 content::BrowserThread::GetCurrentThreadIdentifier(&creation_thread_);
289 DCHECK(got_id);
290 }
291
292 void Start() {
293 content::BrowserThread::PostBlockingPoolSequencedTask(
294 "Job", FROM_HERE, base::Bind(&Job::Task1OnBlockingPool, this));
295 }
296
297 void Task1OnBlockingPool() {
298 printf("Task1OnBlockingPool BEG\n");
299
300 content::BrowserThread::PostTask(
301 creation_thread_, FROM_HERE,
302 base::Bind(&Job::Task2OnCreationThread, this));
303 printf("Task1OnBlockingPool END\n");
304 post_task_finished_ = true;
305 }
306
307 void Task2OnCreationThread() {
308 printf("Task2OnCreationThread\n");
309 job_callback_.Run(true);
310 }
311
312 bool post_task_finished() { return post_task_finished_; }
313
314 private:
315 friend class base::RefCountedThreadSafe<Job>;
316 ~Job() {}
317
318 bool post_task_finished_ = false;
319 JobCallback job_callback_;
320 content::BrowserThread::ID creation_thread_;
321 DISALLOW_COPY_AND_ASSIGN(Job);
322 };
323
324 class JobWaiter {
325 public:
326 JobWaiter() : weak_factory_(this) {}
327
328 Job::JobCallback CreateCallback() {
329 return base::Bind(&JobWaiter::Callback, weak_factory_.GetWeakPtr());
330 }
331
332 void WaitForCallback() {
333 if (seen_) {
334 ADD_FAILURE() << "Already seen_";
335 return;
336 }
337 base::RunLoop run_loop;
338 run_loop_quit_ = run_loop.QuitClosure();
339 run_loop.Run();
340 }
341
342 private:
343 void Callback(bool unused) {
344 printf("JobWaiter::Callback\n");
345 seen_ = true;
346 if (run_loop_quit_)
347 base::ResetAndReturn(&run_loop_quit_).Run();
348 }
349
350 bool seen_ = false;
351 base::Closure run_loop_quit_;
352 base::WeakPtrFactory<JobWaiter> weak_factory_;
353 DISALLOW_COPY_AND_ASSIGN(JobWaiter);
354 };
355
356 TEST_F(ContentHashFetcherTest, TestCase) {
357 JobWaiter waiter;
358 scoped_refptr<Job> job(new Job(waiter.CreateCallback()));
359 job->Start();
360 waiter.WaitForCallback();
361 { ASSERT_TRUE(job->post_task_finished()); }
362 }
363
283 } // namespace extensions 364 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698