| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a | 5 // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a |
| 6 // heavily-loaded system). Sorry. |kEpsilonMicros| may be increased to increase | 6 // heavily-loaded system). Sorry. |kEpsilonMicros| may be increased to increase |
| 7 // tolerance and reduce observed flakiness. | 7 // tolerance and reduce observed flakiness. |
| 8 | 8 |
| 9 #include "mojo/system/message_pipe_dispatcher.h" | 9 #include "mojo/system/message_pipe_dispatcher.h" |
| 10 | 10 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 // Shouldn't need to remove the waiter (it was not added). | 56 // Shouldn't need to remove the waiter (it was not added). |
| 57 | 57 |
| 58 // Add a readable waiter to |d_0|, then make it readable (by writing to | 58 // Add a readable waiter to |d_0|, then make it readable (by writing to |
| 59 // |d_1|), then wait. | 59 // |d_1|), then wait. |
| 60 w.Init(); | 60 w.Init(); |
| 61 EXPECT_EQ(MOJO_RESULT_OK, | 61 EXPECT_EQ(MOJO_RESULT_OK, |
| 62 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 1)); | 62 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 1)); |
| 63 buffer[0] = 123456789; | 63 buffer[0] = 123456789; |
| 64 EXPECT_EQ(MOJO_RESULT_OK, | 64 EXPECT_EQ(MOJO_RESULT_OK, |
| 65 d_1->WriteMessage(buffer, kBufferSize, | 65 d_1->WriteMessage(buffer, kBufferSize, |
| 66 NULL, 0, | 66 NULL, |
| 67 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 67 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 68 stopwatch.Start(); | 68 stopwatch.Start(); |
| 69 EXPECT_EQ(1, w.Wait(MOJO_DEADLINE_INDEFINITE)); | 69 EXPECT_EQ(1, w.Wait(MOJO_DEADLINE_INDEFINITE)); |
| 70 elapsed_micros = stopwatch.Elapsed(); | 70 elapsed_micros = stopwatch.Elapsed(); |
| 71 EXPECT_LT(elapsed_micros, kEpsilonMicros); | 71 EXPECT_LT(elapsed_micros, kEpsilonMicros); |
| 72 d_0->RemoveWaiter(&w); | 72 d_0->RemoveWaiter(&w); |
| 73 | 73 |
| 74 // Try adding a readable waiter when already readable (from above). | 74 // Try adding a readable waiter when already readable (from above). |
| 75 w.Init(); | 75 w.Init(); |
| 76 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 76 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 77 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 2)); | 77 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 2)); |
| 78 // Shouldn't need to remove the waiter (it was not added). | 78 // Shouldn't need to remove the waiter (it was not added). |
| 79 | 79 |
| 80 // Make |d_0| no longer readable (by reading from it). | 80 // Make |d_0| no longer readable (by reading from it). |
| 81 buffer[0] = 0; | 81 buffer[0] = 0; |
| 82 buffer_size = kBufferSize; | 82 buffer_size = kBufferSize; |
| 83 EXPECT_EQ(MOJO_RESULT_OK, | 83 EXPECT_EQ(MOJO_RESULT_OK, |
| 84 d_0->ReadMessage(buffer, &buffer_size, | 84 d_0->ReadMessage(buffer, &buffer_size, |
| 85 NULL, NULL, | 85 0, NULL, |
| 86 MOJO_READ_MESSAGE_FLAG_NONE)); | 86 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 87 EXPECT_EQ(kBufferSize, buffer_size); | 87 EXPECT_EQ(kBufferSize, buffer_size); |
| 88 EXPECT_EQ(123456789, buffer[0]); | 88 EXPECT_EQ(123456789, buffer[0]); |
| 89 | 89 |
| 90 // Wait for zero time for readability on |d_0| (will time out). | 90 // Wait for zero time for readability on |d_0| (will time out). |
| 91 w.Init(); | 91 w.Init(); |
| 92 EXPECT_EQ(MOJO_RESULT_OK, | 92 EXPECT_EQ(MOJO_RESULT_OK, |
| 93 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); | 93 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); |
| 94 stopwatch.Start(); | 94 stopwatch.Start(); |
| 95 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0)); | 95 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, w.Wait(0)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 108 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); | 108 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); |
| 109 d_0->RemoveWaiter(&w); | 109 d_0->RemoveWaiter(&w); |
| 110 | 110 |
| 111 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); | 111 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); |
| 112 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); | 112 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST(MessagePipeDispatcherTest, InvalidParams) { | 116 TEST(MessagePipeDispatcherTest, InvalidParams) { |
| 117 char buffer[1]; | 117 char buffer[1]; |
| 118 MojoHandle handles[1]; | |
| 119 | 118 |
| 120 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); | 119 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); |
| 121 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); | 120 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); |
| 122 { | 121 { |
| 123 scoped_refptr<MessagePipe> mp(new MessagePipe()); | 122 scoped_refptr<MessagePipe> mp(new MessagePipe()); |
| 124 d_0->Init(mp, 0); | 123 d_0->Init(mp, 0); |
| 125 d_1->Init(mp, 1); | 124 d_1->Init(mp, 1); |
| 126 } | 125 } |
| 127 | 126 |
| 128 // |WriteMessage|: | 127 // |WriteMessage|: |
| 129 // Null buffer with nonzero buffer size. | 128 // Null buffer with nonzero buffer size. |
| 130 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 129 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 131 d_0->WriteMessage(NULL, 1, | 130 d_0->WriteMessage(NULL, 1, |
| 132 NULL, 0, | 131 NULL, |
| 133 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 132 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 134 // Huge buffer size. | 133 // Huge buffer size. |
| 135 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, | 134 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, |
| 136 d_0->WriteMessage(buffer, std::numeric_limits<uint32_t>::max(), | 135 d_0->WriteMessage(buffer, std::numeric_limits<uint32_t>::max(), |
| 137 NULL, 0, | 136 NULL, |
| 138 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 139 | |
| 140 // Null handles with nonzero handle count. | |
| 141 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | |
| 142 d_0->WriteMessage(buffer, sizeof(buffer), | |
| 143 NULL, 1, | |
| 144 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 145 // Huge handle count (implausibly big on some systems -- more than can be | |
| 146 // stored in a 32-bit address space). | |
| 147 // Note: This may return either |MOJO_RESULT_INVALID_ARGUMENT| or | |
| 148 // |MOJO_RESULT_RESOURCE_EXHAUSTED|, depending on whether it's plausible or | |
| 149 // not. | |
| 150 EXPECT_NE(MOJO_RESULT_OK, | |
| 151 d_0->WriteMessage(buffer, sizeof(buffer), | |
| 152 handles, std::numeric_limits<uint32_t>::max(), | |
| 153 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 154 // Huge handle count (plausibly big). | |
| 155 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, | |
| 156 d_0->WriteMessage(buffer, sizeof(buffer), | |
| 157 handles, std::numeric_limits<uint32_t>::max() / | |
| 158 sizeof(handles[0]), | |
| 159 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 137 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 160 | 138 |
| 161 // |ReadMessage|: | 139 // |ReadMessage|: |
| 162 // Null buffer with nonzero buffer size. | 140 // Null buffer with nonzero buffer size. |
| 163 uint32_t buffer_size = 1; | 141 uint32_t buffer_size = 1; |
| 164 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 142 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 165 d_0->ReadMessage(NULL, &buffer_size, | 143 d_0->ReadMessage(NULL, &buffer_size, |
| 166 NULL, NULL, | 144 0, NULL, |
| 167 MOJO_READ_MESSAGE_FLAG_NONE)); | |
| 168 // Null handles with nonzero handle count. | |
| 169 buffer_size = static_cast<uint32_t>(sizeof(buffer)); | |
| 170 uint32_t handle_count = 1; | |
| 171 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | |
| 172 d_0->ReadMessage(buffer, &buffer_size, | |
| 173 NULL, &handle_count, | |
| 174 MOJO_READ_MESSAGE_FLAG_NONE)); | 145 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 175 | 146 |
| 176 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); | 147 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); |
| 177 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); | 148 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); |
| 178 } | 149 } |
| 179 | 150 |
| 180 // Test what happens when one end is closed (single-threaded test). | 151 // Test what happens when one end is closed (single-threaded test). |
| 181 TEST(MessagePipeDispatcherTest, BasicClosed) { | 152 TEST(MessagePipeDispatcherTest, BasicClosed) { |
| 182 int32_t buffer[1]; | 153 int32_t buffer[1]; |
| 183 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); | 154 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); |
| 184 uint32_t buffer_size; | 155 uint32_t buffer_size; |
| 185 | 156 |
| 186 // Run this test both with |d_0| as port 0, |d_1| as port 1 and vice versa. | 157 // Run this test both with |d_0| as port 0, |d_1| as port 1 and vice versa. |
| 187 for (unsigned i = 0; i < 2; i++) { | 158 for (unsigned i = 0; i < 2; i++) { |
| 188 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); | 159 scoped_refptr<MessagePipeDispatcher> d_0(new MessagePipeDispatcher()); |
| 189 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); | 160 scoped_refptr<MessagePipeDispatcher> d_1(new MessagePipeDispatcher()); |
| 190 { | 161 { |
| 191 scoped_refptr<MessagePipe> mp(new MessagePipe()); | 162 scoped_refptr<MessagePipe> mp(new MessagePipe()); |
| 192 d_0->Init(mp, i); // 0, 1. | 163 d_0->Init(mp, i); // 0, 1. |
| 193 d_1->Init(mp, i ^ 1); // 1, 0. | 164 d_1->Init(mp, i ^ 1); // 1, 0. |
| 194 } | 165 } |
| 195 Waiter w; | 166 Waiter w; |
| 196 | 167 |
| 197 // Write (twice) to |d_1|. | 168 // Write (twice) to |d_1|. |
| 198 buffer[0] = 123456789; | 169 buffer[0] = 123456789; |
| 199 EXPECT_EQ(MOJO_RESULT_OK, | 170 EXPECT_EQ(MOJO_RESULT_OK, |
| 200 d_1->WriteMessage(buffer, kBufferSize, | 171 d_1->WriteMessage(buffer, kBufferSize, |
| 201 NULL, 0, | 172 NULL, |
| 202 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 173 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 203 buffer[0] = 234567890; | 174 buffer[0] = 234567890; |
| 204 EXPECT_EQ(MOJO_RESULT_OK, | 175 EXPECT_EQ(MOJO_RESULT_OK, |
| 205 d_1->WriteMessage(buffer, kBufferSize, | 176 d_1->WriteMessage(buffer, kBufferSize, |
| 206 NULL, 0, | 177 NULL, |
| 207 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 178 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 208 | 179 |
| 209 // Try waiting for readable on |d_0|; should fail (already satisfied). | 180 // Try waiting for readable on |d_0|; should fail (already satisfied). |
| 210 w.Init(); | 181 w.Init(); |
| 211 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 182 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 212 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 0)); | 183 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 0)); |
| 213 | 184 |
| 214 // Try reading from |d_1|; should fail (nothing to read). | 185 // Try reading from |d_1|; should fail (nothing to read). |
| 215 buffer[0] = 0; | 186 buffer[0] = 0; |
| 216 buffer_size = kBufferSize; | 187 buffer_size = kBufferSize; |
| 217 EXPECT_EQ(MOJO_RESULT_NOT_FOUND, | 188 EXPECT_EQ(MOJO_RESULT_NOT_FOUND, |
| 218 d_1->ReadMessage(buffer, &buffer_size, | 189 d_1->ReadMessage(buffer, &buffer_size, |
| 219 NULL, NULL, | 190 0, NULL, |
| 220 MOJO_READ_MESSAGE_FLAG_NONE)); | 191 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 221 | 192 |
| 222 // Close |d_1|. | 193 // Close |d_1|. |
| 223 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); | 194 EXPECT_EQ(MOJO_RESULT_OK, d_1->Close()); |
| 224 | 195 |
| 225 // Try waiting for readable on |d_0|; should fail (already satisfied). | 196 // Try waiting for readable on |d_0|; should fail (already satisfied). |
| 226 w.Init(); | 197 w.Init(); |
| 227 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 198 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 228 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 1)); | 199 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 1)); |
| 229 | 200 |
| 230 // Read from |d_0|. | 201 // Read from |d_0|. |
| 231 buffer[0] = 0; | 202 buffer[0] = 0; |
| 232 buffer_size = kBufferSize; | 203 buffer_size = kBufferSize; |
| 233 EXPECT_EQ(MOJO_RESULT_OK, | 204 EXPECT_EQ(MOJO_RESULT_OK, |
| 234 d_0->ReadMessage(buffer, &buffer_size, | 205 d_0->ReadMessage(buffer, &buffer_size, |
| 235 NULL, NULL, | 206 0, NULL, |
| 236 MOJO_READ_MESSAGE_FLAG_NONE)); | 207 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 237 EXPECT_EQ(kBufferSize, buffer_size); | 208 EXPECT_EQ(kBufferSize, buffer_size); |
| 238 EXPECT_EQ(123456789, buffer[0]); | 209 EXPECT_EQ(123456789, buffer[0]); |
| 239 | 210 |
| 240 // Try waiting for readable on |d_0|; should fail (already satisfied). | 211 // Try waiting for readable on |d_0|; should fail (already satisfied). |
| 241 w.Init(); | 212 w.Init(); |
| 242 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 213 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 243 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 2)); | 214 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 2)); |
| 244 | 215 |
| 245 // Read again from |d_0|. | 216 // Read again from |d_0|. |
| 246 buffer[0] = 0; | 217 buffer[0] = 0; |
| 247 buffer_size = kBufferSize; | 218 buffer_size = kBufferSize; |
| 248 EXPECT_EQ(MOJO_RESULT_OK, | 219 EXPECT_EQ(MOJO_RESULT_OK, |
| 249 d_0->ReadMessage(buffer, &buffer_size, | 220 d_0->ReadMessage(buffer, &buffer_size, |
| 250 NULL, NULL, | 221 0, NULL, |
| 251 MOJO_READ_MESSAGE_FLAG_NONE)); | 222 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 252 EXPECT_EQ(kBufferSize, buffer_size); | 223 EXPECT_EQ(kBufferSize, buffer_size); |
| 253 EXPECT_EQ(234567890, buffer[0]); | 224 EXPECT_EQ(234567890, buffer[0]); |
| 254 | 225 |
| 255 // Try waiting for readable on |d_0|; should fail (unsatisfiable). | 226 // Try waiting for readable on |d_0|; should fail (unsatisfiable). |
| 256 w.Init(); | 227 w.Init(); |
| 257 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 228 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 258 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); | 229 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_READABLE, 3)); |
| 259 | 230 |
| 260 // Try waiting for writable on |d_0|; should fail (unsatisfiable). | 231 // Try waiting for writable on |d_0|; should fail (unsatisfiable). |
| 261 w.Init(); | 232 w.Init(); |
| 262 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 233 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 263 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4)); | 234 d_0->AddWaiter(&w, MOJO_WAIT_FLAG_WRITABLE, 4)); |
| 264 | 235 |
| 265 // Try reading from |d_0|; should fail (nothing to read and other end | 236 // Try reading from |d_0|; should fail (nothing to read and other end |
| 266 // closed). | 237 // closed). |
| 267 buffer[0] = 0; | 238 buffer[0] = 0; |
| 268 buffer_size = kBufferSize; | 239 buffer_size = kBufferSize; |
| 269 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 240 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 270 d_0->ReadMessage(buffer, &buffer_size, | 241 d_0->ReadMessage(buffer, &buffer_size, |
| 271 NULL, NULL, | 242 0, NULL, |
| 272 MOJO_READ_MESSAGE_FLAG_NONE)); | 243 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 273 | 244 |
| 274 // Try writing to |d_0|; should fail (other end closed). | 245 // Try writing to |d_0|; should fail (other end closed). |
| 275 buffer[0] = 345678901; | 246 buffer[0] = 345678901; |
| 276 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 247 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 277 d_0->WriteMessage(buffer, kBufferSize, | 248 d_0->WriteMessage(buffer, kBufferSize, |
| 278 NULL, 0, | 249 NULL, |
| 279 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 250 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 280 | 251 |
| 281 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); | 252 EXPECT_EQ(MOJO_RESULT_OK, d_0->Close()); |
| 282 } | 253 } |
| 283 } | 254 } |
| 284 | 255 |
| 285 TEST(MessagePipeDispatcherTest, BasicThreaded) { | 256 TEST(MessagePipeDispatcherTest, BasicThreaded) { |
| 286 test::Stopwatch stopwatch; | 257 test::Stopwatch stopwatch; |
| 287 int32_t buffer[1]; | 258 int32_t buffer[1]; |
| 288 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); | 259 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 309 0, | 280 0, |
| 310 &did_wait, &result); | 281 &did_wait, &result); |
| 311 stopwatch.Start(); | 282 stopwatch.Start(); |
| 312 thread.Start(); | 283 thread.Start(); |
| 313 base::PlatformThread::Sleep( | 284 base::PlatformThread::Sleep( |
| 314 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); | 285 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); |
| 315 // Wake it up by writing to |d_0|. | 286 // Wake it up by writing to |d_0|. |
| 316 buffer[0] = 123456789; | 287 buffer[0] = 123456789; |
| 317 EXPECT_EQ(MOJO_RESULT_OK, | 288 EXPECT_EQ(MOJO_RESULT_OK, |
| 318 d_0->WriteMessage(buffer, kBufferSize, | 289 d_0->WriteMessage(buffer, kBufferSize, |
| 319 NULL, 0, | 290 NULL, |
| 320 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 291 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 321 } // Joins the thread. | 292 } // Joins the thread. |
| 322 elapsed_micros = stopwatch.Elapsed(); | 293 elapsed_micros = stopwatch.Elapsed(); |
| 323 EXPECT_TRUE(did_wait); | 294 EXPECT_TRUE(did_wait); |
| 324 EXPECT_EQ(0, result); | 295 EXPECT_EQ(0, result); |
| 325 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); | 296 EXPECT_GT(elapsed_micros, (2-1) * kEpsilonMicros); |
| 326 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); | 297 EXPECT_LT(elapsed_micros, (2+1) * kEpsilonMicros); |
| 327 | 298 |
| 328 // Now |d_1| is already readable. Try waiting for it again. | 299 // Now |d_1| is already readable. Try waiting for it again. |
| 329 { | 300 { |
| 330 test::WaiterThread thread(d_1, | 301 test::WaiterThread thread(d_1, |
| 331 MOJO_WAIT_FLAG_READABLE, | 302 MOJO_WAIT_FLAG_READABLE, |
| 332 MOJO_DEADLINE_INDEFINITE, | 303 MOJO_DEADLINE_INDEFINITE, |
| 333 1, | 304 1, |
| 334 &did_wait, &result); | 305 &did_wait, &result); |
| 335 stopwatch.Start(); | 306 stopwatch.Start(); |
| 336 thread.Start(); | 307 thread.Start(); |
| 337 } // Joins the thread. | 308 } // Joins the thread. |
| 338 elapsed_micros = stopwatch.Elapsed(); | 309 elapsed_micros = stopwatch.Elapsed(); |
| 339 EXPECT_FALSE(did_wait); | 310 EXPECT_FALSE(did_wait); |
| 340 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, result); | 311 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, result); |
| 341 EXPECT_LT(elapsed_micros, kEpsilonMicros); | 312 EXPECT_LT(elapsed_micros, kEpsilonMicros); |
| 342 | 313 |
| 343 // Consume what we wrote to |d_0|. | 314 // Consume what we wrote to |d_0|. |
| 344 buffer[0] = 0; | 315 buffer[0] = 0; |
| 345 buffer_size = kBufferSize; | 316 buffer_size = kBufferSize; |
| 346 EXPECT_EQ(MOJO_RESULT_OK, | 317 EXPECT_EQ(MOJO_RESULT_OK, |
| 347 d_1->ReadMessage(buffer, &buffer_size, | 318 d_1->ReadMessage(buffer, &buffer_size, |
| 348 NULL, NULL, | 319 0, NULL, |
| 349 MOJO_READ_MESSAGE_FLAG_NONE)); | 320 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 350 EXPECT_EQ(kBufferSize, buffer_size); | 321 EXPECT_EQ(kBufferSize, buffer_size); |
| 351 EXPECT_EQ(123456789, buffer[0]); | 322 EXPECT_EQ(123456789, buffer[0]); |
| 352 | 323 |
| 353 // Wait for readable on |d_1| and close |d_0| after some time, which should | 324 // Wait for readable on |d_1| and close |d_0| after some time, which should |
| 354 // cancel that wait. | 325 // cancel that wait. |
| 355 { | 326 { |
| 356 test::WaiterThread thread(d_1, | 327 test::WaiterThread thread(d_1, |
| 357 MOJO_WAIT_FLAG_READABLE, | 328 MOJO_WAIT_FLAG_READABLE, |
| 358 MOJO_DEADLINE_INDEFINITE, | 329 MOJO_DEADLINE_INDEFINITE, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 | 408 |
| 438 // Number of messages to write. | 409 // Number of messages to write. |
| 439 *messages_written_ = static_cast<size_t>(base::RandInt(1000, 6000)); | 410 *messages_written_ = static_cast<size_t>(base::RandInt(1000, 6000)); |
| 440 | 411 |
| 441 // Write messages. | 412 // Write messages. |
| 442 for (size_t i = 0; i < *messages_written_; i++) { | 413 for (size_t i = 0; i < *messages_written_; i++) { |
| 443 uint32_t bytes_to_write = static_cast<uint32_t>( | 414 uint32_t bytes_to_write = static_cast<uint32_t>( |
| 444 base::RandInt(1, static_cast<int>(kMaxMessageSize))); | 415 base::RandInt(1, static_cast<int>(kMaxMessageSize))); |
| 445 EXPECT_EQ(MOJO_RESULT_OK, | 416 EXPECT_EQ(MOJO_RESULT_OK, |
| 446 write_dispatcher_->WriteMessage(buffer, bytes_to_write, | 417 write_dispatcher_->WriteMessage(buffer, bytes_to_write, |
| 447 NULL, 0, | 418 NULL, |
| 448 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 419 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 449 *bytes_written_ += bytes_to_write; | 420 *bytes_written_ += bytes_to_write; |
| 450 } | 421 } |
| 451 | 422 |
| 452 // Write one last "quit" message. | 423 // Write one last "quit" message. |
| 453 EXPECT_EQ(MOJO_RESULT_OK, | 424 EXPECT_EQ(MOJO_RESULT_OK, |
| 454 write_dispatcher_->WriteMessage("quit", 4, NULL, 0, | 425 write_dispatcher_->WriteMessage("quit", 4, |
| 426 NULL, |
| 455 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 427 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 456 } | 428 } |
| 457 | 429 |
| 458 const scoped_refptr<Dispatcher> write_dispatcher_; | 430 const scoped_refptr<Dispatcher> write_dispatcher_; |
| 459 size_t* const messages_written_; | 431 size_t* const messages_written_; |
| 460 size_t* const bytes_written_; | 432 size_t* const bytes_written_; |
| 461 | 433 |
| 462 DISALLOW_COPY_AND_ASSIGN(WriterThread); | 434 DISALLOW_COPY_AND_ASSIGN(WriterThread); |
| 463 }; | 435 }; |
| 464 | 436 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 495 if (result == MOJO_RESULT_OK) { | 467 if (result == MOJO_RESULT_OK) { |
| 496 // Actually need to wait. | 468 // Actually need to wait. |
| 497 EXPECT_EQ(0, w.Wait(MOJO_DEADLINE_INDEFINITE)); | 469 EXPECT_EQ(0, w.Wait(MOJO_DEADLINE_INDEFINITE)); |
| 498 read_dispatcher_->RemoveWaiter(&w); | 470 read_dispatcher_->RemoveWaiter(&w); |
| 499 } | 471 } |
| 500 | 472 |
| 501 // Now, try to do the read. | 473 // Now, try to do the read. |
| 502 // Clear the buffer so that we can check the result. | 474 // Clear the buffer so that we can check the result. |
| 503 memset(buffer, 0, sizeof(buffer)); | 475 memset(buffer, 0, sizeof(buffer)); |
| 504 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 476 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 505 result = read_dispatcher_->ReadMessage(buffer, &buffer_size, NULL, NULL, | 477 result = read_dispatcher_->ReadMessage(buffer, &buffer_size, |
| 478 0, NULL, |
| 506 MOJO_READ_MESSAGE_FLAG_NONE); | 479 MOJO_READ_MESSAGE_FLAG_NONE); |
| 507 EXPECT_TRUE(result == MOJO_RESULT_OK || | 480 EXPECT_TRUE(result == MOJO_RESULT_OK || |
| 508 result == MOJO_RESULT_NOT_FOUND) << "result: " << result; | 481 result == MOJO_RESULT_NOT_FOUND) << "result: " << result; |
| 509 // We're racing with others to read, so maybe we failed. | 482 // We're racing with others to read, so maybe we failed. |
| 510 if (result == MOJO_RESULT_NOT_FOUND) | 483 if (result == MOJO_RESULT_NOT_FOUND) |
| 511 continue; // In which case, try again. | 484 continue; // In which case, try again. |
| 512 // Check for quit. | 485 // Check for quit. |
| 513 if (buffer_size == 4 && memcmp("quit", buffer, 4) == 0) | 486 if (buffer_size == 4 && memcmp("quit", buffer, 4) == 0) |
| 514 return; | 487 return; |
| 515 EXPECT_GE(buffer_size, 1u); | 488 EXPECT_GE(buffer_size, 1u); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 EXPECT_EQ(total_messages_written, total_messages_read); | 578 EXPECT_EQ(total_messages_written, total_messages_read); |
| 606 EXPECT_EQ(total_bytes_written, total_bytes_read); | 579 EXPECT_EQ(total_bytes_written, total_bytes_read); |
| 607 | 580 |
| 608 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); | 581 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); |
| 609 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); | 582 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); |
| 610 } | 583 } |
| 611 | 584 |
| 612 } // namespace | 585 } // namespace |
| 613 } // namespace system | 586 } // namespace system |
| 614 } // namespace mojo | 587 } // namespace mojo |
| OLD | NEW |