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: chrome/common/net/thread_blocker_unittest.cc

Issue 2802015: Massively simplify the NetworkChangeNotifier infrastructure:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "chrome/common/net/thread_blocker.h"
6
7 #include "base/basictypes.h"
8 #include "base/lock.h"
9 #include "base/logging.h"
10 #include "base/message_loop.h"
11 #include "base/scoped_ptr.h"
12 #include "base/task.h"
13 #include "base/thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace chrome_common_net {
17 class Flag;
18 }; // namespace chrome_common_net
19
20 // We manage the lifetime of chrome_common_net::Flag ourselves.
21 DISABLE_RUNNABLE_METHOD_REFCOUNT(chrome_common_net::Flag);
22
23 namespace chrome_common_net {
24
25 // Utility class that is basically just a thread-safe boolean.
26 class Flag {
27 public:
28 Flag() : flag_(false) {}
29
30 bool IsSet() const {
31 AutoLock auto_lock(lock_);
32 return flag_;
33 }
34
35 void Set() {
36 AutoLock auto_lock(lock_);
37 flag_ = true;
38 }
39
40 void Unset() {
41 AutoLock auto_lock(lock_);
42 flag_ = false;
43 }
44
45 private:
46 mutable Lock lock_;
47 bool flag_;
48
49 DISALLOW_COPY_AND_ASSIGN(Flag);
50 };
51
52 namespace {
53
54 class ThreadBlockerTest : public testing::Test {
55 protected:
56 ThreadBlockerTest() : target_thread_("Target Thread") {}
57
58 virtual ~ThreadBlockerTest() {
59 CHECK(!thread_blocker_.get());
60 }
61
62 virtual void SetUp() {
63 CHECK(target_thread_.Start());
64 thread_blocker_.reset(new ThreadBlocker(&target_thread_));
65 }
66
67 virtual void TearDown() {
68 target_thread_.Stop();
69 thread_blocker_.reset();
70 }
71
72 base::Thread target_thread_;
73 scoped_ptr<ThreadBlocker> thread_blocker_;
74 Flag flag_;
75
76 private:
77 DISALLOW_COPY_AND_ASSIGN(ThreadBlockerTest);
78 };
79
80 TEST_F(ThreadBlockerTest, Basic) {
81 thread_blocker_->Block();
82 target_thread_.message_loop()->PostTask(
83 FROM_HERE, NewRunnableMethod(&flag_, &Flag::Set));
84 EXPECT_FALSE(flag_.IsSet());
85 thread_blocker_->Unblock();
86 // Need to block again to make sure this thread waits for the posted
87 // method to run.
88 thread_blocker_->Block();
89 EXPECT_TRUE(flag_.IsSet());
90 thread_blocker_->Unblock();
91 }
92
93 TEST_F(ThreadBlockerTest, SetUnset) {
94 thread_blocker_->Block();
95 target_thread_.message_loop()->PostTask(
96 FROM_HERE, NewRunnableMethod(&flag_, &Flag::Set));
97 target_thread_.message_loop()->PostTask(
98 FROM_HERE, NewRunnableMethod(&flag_, &Flag::Unset));
99 EXPECT_FALSE(flag_.IsSet());
100 thread_blocker_->Unblock();
101 // Need to block again here too.
102 thread_blocker_->Block();
103 EXPECT_FALSE(flag_.IsSet());
104 thread_blocker_->Unblock();
105 }
106
107 } // namespace
108
109 } // namespace chrome_common_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698