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

Side by Side Diff: mojo/public/cpp/system/tests/watcher_unittest.cc

Issue 2623263005: Tag some of Mojo heap allocations for the heap profiler. (Closed)
Patch Set: Synced Created 3 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
« no previous file with comments | « mojo/public/cpp/bindings/lib/multiplex_router.cc ('k') | mojo/public/cpp/system/watcher.h » ('j') | 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 "mojo/public/cpp/system/watcher.h" 5 #include "mojo/public/cpp/system/watcher.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 30 matching lines...) Expand all
41 41
42 DISALLOW_COPY_AND_ASSIGN(WatcherTest); 42 DISALLOW_COPY_AND_ASSIGN(WatcherTest);
43 }; 43 };
44 44
45 TEST_F(WatcherTest, WatchBasic) { 45 TEST_F(WatcherTest, WatchBasic) {
46 ScopedMessagePipeHandle a, b; 46 ScopedMessagePipeHandle a, b;
47 CreateMessagePipe(nullptr, &a, &b); 47 CreateMessagePipe(nullptr, &a, &b);
48 48
49 bool notified = false; 49 bool notified = false;
50 base::RunLoop run_loop; 50 base::RunLoop run_loop;
51 Watcher b_watcher; 51 Watcher b_watcher(FROM_HERE);
52 EXPECT_EQ(MOJO_RESULT_OK, 52 EXPECT_EQ(MOJO_RESULT_OK,
53 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 53 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
54 OnReady([&] (MojoResult result) { 54 OnReady([&] (MojoResult result) {
55 EXPECT_EQ(MOJO_RESULT_OK, result); 55 EXPECT_EQ(MOJO_RESULT_OK, result);
56 notified = true; 56 notified = true;
57 run_loop.Quit(); 57 run_loop.Quit();
58 }))); 58 })));
59 EXPECT_TRUE(b_watcher.IsWatching()); 59 EXPECT_TRUE(b_watcher.IsWatching());
60 60
61 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0, 61 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
62 MOJO_WRITE_MESSAGE_FLAG_NONE)); 62 MOJO_WRITE_MESSAGE_FLAG_NONE));
63 run_loop.Run(); 63 run_loop.Run();
64 EXPECT_TRUE(notified); 64 EXPECT_TRUE(notified);
65 65
66 b_watcher.Cancel(); 66 b_watcher.Cancel();
67 } 67 }
68 68
69 TEST_F(WatcherTest, WatchUnsatisfiable) { 69 TEST_F(WatcherTest, WatchUnsatisfiable) {
70 ScopedMessagePipeHandle a, b; 70 ScopedMessagePipeHandle a, b;
71 CreateMessagePipe(nullptr, &a, &b); 71 CreateMessagePipe(nullptr, &a, &b);
72 a.reset(); 72 a.reset();
73 73
74 Watcher b_watcher; 74 Watcher b_watcher(FROM_HERE);
75 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 75 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
76 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 76 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
77 NotReached())); 77 NotReached()));
78 EXPECT_FALSE(b_watcher.IsWatching()); 78 EXPECT_FALSE(b_watcher.IsWatching());
79 } 79 }
80 80
81 TEST_F(WatcherTest, WatchInvalidHandle) { 81 TEST_F(WatcherTest, WatchInvalidHandle) {
82 ScopedMessagePipeHandle a, b; 82 ScopedMessagePipeHandle a, b;
83 CreateMessagePipe(nullptr, &a, &b); 83 CreateMessagePipe(nullptr, &a, &b);
84 a.reset(); 84 a.reset();
85 b.reset(); 85 b.reset();
86 86
87 Watcher b_watcher; 87 Watcher b_watcher(FROM_HERE);
88 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 88 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
89 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 89 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
90 NotReached())); 90 NotReached()));
91 EXPECT_FALSE(b_watcher.IsWatching()); 91 EXPECT_FALSE(b_watcher.IsWatching());
92 } 92 }
93 93
94 TEST_F(WatcherTest, Cancel) { 94 TEST_F(WatcherTest, Cancel) {
95 ScopedMessagePipeHandle a, b; 95 ScopedMessagePipeHandle a, b;
96 CreateMessagePipe(nullptr, &a, &b); 96 CreateMessagePipe(nullptr, &a, &b);
97 97
98 base::RunLoop run_loop; 98 base::RunLoop run_loop;
99 Watcher b_watcher; 99 Watcher b_watcher(FROM_HERE);
100 EXPECT_EQ(MOJO_RESULT_OK, 100 EXPECT_EQ(MOJO_RESULT_OK,
101 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 101 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
102 NotReached())); 102 NotReached()));
103 EXPECT_TRUE(b_watcher.IsWatching()); 103 EXPECT_TRUE(b_watcher.IsWatching());
104 b_watcher.Cancel(); 104 b_watcher.Cancel();
105 EXPECT_FALSE(b_watcher.IsWatching()); 105 EXPECT_FALSE(b_watcher.IsWatching());
106 106
107 // This should never trigger the watcher. 107 // This should never trigger the watcher.
108 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0, 108 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
109 MOJO_WRITE_MESSAGE_FLAG_NONE)); 109 MOJO_WRITE_MESSAGE_FLAG_NONE));
110 110
111 base::ThreadTaskRunnerHandle::Get()->PostTask( 111 base::ThreadTaskRunnerHandle::Get()->PostTask(
112 FROM_HERE, run_loop.QuitClosure()); 112 FROM_HERE, run_loop.QuitClosure());
113 run_loop.Run(); 113 run_loop.Run();
114 } 114 }
115 115
116 TEST_F(WatcherTest, CancelOnClose) { 116 TEST_F(WatcherTest, CancelOnClose) {
117 ScopedMessagePipeHandle a, b; 117 ScopedMessagePipeHandle a, b;
118 CreateMessagePipe(nullptr, &a, &b); 118 CreateMessagePipe(nullptr, &a, &b);
119 119
120 base::RunLoop run_loop; 120 base::RunLoop run_loop;
121 Watcher b_watcher; 121 Watcher b_watcher(FROM_HERE);
122 EXPECT_EQ(MOJO_RESULT_OK, 122 EXPECT_EQ(MOJO_RESULT_OK,
123 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 123 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
124 OnReady([&] (MojoResult result) { 124 OnReady([&] (MojoResult result) {
125 EXPECT_EQ(MOJO_RESULT_CANCELLED, result); 125 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
126 run_loop.Quit(); 126 run_loop.Quit();
127 }))); 127 })));
128 EXPECT_TRUE(b_watcher.IsWatching()); 128 EXPECT_TRUE(b_watcher.IsWatching());
129 129
130 // This should trigger the watcher above. 130 // This should trigger the watcher above.
131 b.reset(); 131 b.reset();
132 132
133 run_loop.Run(); 133 run_loop.Run();
134 134
135 EXPECT_FALSE(b_watcher.IsWatching()); 135 EXPECT_FALSE(b_watcher.IsWatching());
136 } 136 }
137 137
138 TEST_F(WatcherTest, CancelOnDestruction) { 138 TEST_F(WatcherTest, CancelOnDestruction) {
139 ScopedMessagePipeHandle a, b; 139 ScopedMessagePipeHandle a, b;
140 CreateMessagePipe(nullptr, &a, &b); 140 CreateMessagePipe(nullptr, &a, &b);
141 base::RunLoop run_loop; 141 base::RunLoop run_loop;
142 { 142 {
143 Watcher b_watcher; 143 Watcher b_watcher(FROM_HERE);
144 EXPECT_EQ(MOJO_RESULT_OK, 144 EXPECT_EQ(MOJO_RESULT_OK,
145 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 145 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
146 NotReached())); 146 NotReached()));
147 EXPECT_TRUE(b_watcher.IsWatching()); 147 EXPECT_TRUE(b_watcher.IsWatching());
148 148
149 // |b_watcher| should be cancelled when it goes out of scope. 149 // |b_watcher| should be cancelled when it goes out of scope.
150 } 150 }
151 151
152 // This should never trigger the watcher above. 152 // This should never trigger the watcher above.
153 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0, 153 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
154 MOJO_WRITE_MESSAGE_FLAG_NONE)); 154 MOJO_WRITE_MESSAGE_FLAG_NONE));
155 base::ThreadTaskRunnerHandle::Get()->PostTask( 155 base::ThreadTaskRunnerHandle::Get()->PostTask(
156 FROM_HERE, run_loop.QuitClosure()); 156 FROM_HERE, run_loop.QuitClosure());
157 run_loop.Run(); 157 run_loop.Run();
158 } 158 }
159 159
160 TEST_F(WatcherTest, CloseAndCancel) { 160 TEST_F(WatcherTest, CloseAndCancel) {
161 ScopedMessagePipeHandle a, b; 161 ScopedMessagePipeHandle a, b;
162 CreateMessagePipe(nullptr, &a, &b); 162 CreateMessagePipe(nullptr, &a, &b);
163 163
164 Watcher b_watcher; 164 Watcher b_watcher(FROM_HERE);
165 EXPECT_EQ(MOJO_RESULT_OK, 165 EXPECT_EQ(MOJO_RESULT_OK,
166 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE, 166 b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
167 OnReady([](MojoResult result) { FAIL(); }))); 167 OnReady([](MojoResult result) { FAIL(); })));
168 EXPECT_TRUE(b_watcher.IsWatching()); 168 EXPECT_TRUE(b_watcher.IsWatching());
169 169
170 // This should trigger the watcher above... 170 // This should trigger the watcher above...
171 b.reset(); 171 b.reset();
172 // ...but the watcher is cancelled first. 172 // ...but the watcher is cancelled first.
173 b_watcher.Cancel(); 173 b_watcher.Cancel();
174 174
175 EXPECT_FALSE(b_watcher.IsWatching()); 175 EXPECT_FALSE(b_watcher.IsWatching());
176 176
177 base::RunLoop().RunUntilIdle(); 177 base::RunLoop().RunUntilIdle();
178 } 178 }
179 179
180 } // namespace 180 } // namespace
181 } // namespace mojo 181 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/multiplex_router.cc ('k') | mojo/public/cpp/system/watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698