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

Side by Side Diff: mojo/edk/system/message_pipe_dispatcher_unittest.cc

Issue 1639093002: Add //mojo/edk/platform/thread_utils.* containing "yield" and "sleep". (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 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/edk/system/data_pipe_impl_unittest.cc ('k') | mojo/edk/system/message_pipe_test_utils.cc » ('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 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. |test::EpsilonTimeout()| may be increased to 6 // heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to
7 // increase tolerance and reduce observed flakiness (though doing so reduces the 7 // increase tolerance and reduce observed flakiness (though doing so reduces the
8 // meaningfulness of the test). 8 // meaningfulness of the test).
9 9
10 #include "mojo/edk/system/message_pipe_dispatcher.h" 10 #include "mojo/edk/system/message_pipe_dispatcher.h"
11 11
12 #include <string.h> 12 #include <string.h>
13 13
14 #include <limits> 14 #include <limits>
15 #include <memory> 15 #include <memory>
16 #include <utility> 16 #include <utility>
17 #include <vector> 17 #include <vector>
18 18
19 #include "mojo/edk/platform/thread_utils.h"
19 #include "mojo/edk/system/message_pipe.h" 20 #include "mojo/edk/system/message_pipe.h"
20 #include "mojo/edk/system/test/random.h" 21 #include "mojo/edk/system/test/random.h"
21 #include "mojo/edk/system/test/simple_test_thread.h" 22 #include "mojo/edk/system/test/simple_test_thread.h"
22 #include "mojo/edk/system/test/sleep.h"
23 #include "mojo/edk/system/test/stopwatch.h" 23 #include "mojo/edk/system/test/stopwatch.h"
24 #include "mojo/edk/system/test/timeouts.h" 24 #include "mojo/edk/system/test/timeouts.h"
25 #include "mojo/edk/system/waiter.h" 25 #include "mojo/edk/system/waiter.h"
26 #include "mojo/edk/system/waiter_test_utils.h" 26 #include "mojo/edk/system/waiter_test_utils.h"
27 #include "mojo/edk/util/make_unique.h" 27 #include "mojo/edk/util/make_unique.h"
28 #include "mojo/edk/util/ref_ptr.h" 28 #include "mojo/edk/util/ref_ptr.h"
29 #include "mojo/public/cpp/system/macros.h" 29 #include "mojo/public/cpp/system/macros.h"
30 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
31 31
32 using mojo::platform::ThreadSleep;
32 using mojo::util::MakeUnique; 33 using mojo::util::MakeUnique;
33 using mojo::util::RefPtr; 34 using mojo::util::RefPtr;
34 35
35 namespace mojo { 36 namespace mojo {
36 namespace system { 37 namespace system {
37 namespace { 38 namespace {
38 39
39 const MojoHandleSignals kAllSignals = MOJO_HANDLE_SIGNAL_READABLE | 40 const MojoHandleSignals kAllSignals = MOJO_HANDLE_SIGNAL_READABLE |
40 MOJO_HANDLE_SIGNAL_WRITABLE | 41 MOJO_HANDLE_SIGNAL_WRITABLE |
41 MOJO_HANDLE_SIGNAL_PEER_CLOSED; 42 MOJO_HANDLE_SIGNAL_PEER_CLOSED;
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 d1->Init(std::move(mp), i ^ 1); // 1, 0. 371 d1->Init(std::move(mp), i ^ 1); // 1, 0.
371 } 372 }
372 373
373 // Wait for readable on |d1|, which will become readable after some time. 374 // Wait for readable on |d1|, which will become readable after some time.
374 { 375 {
375 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE, 376 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE,
376 MOJO_DEADLINE_INDEFINITE, 1, &did_wait, &result, 377 MOJO_DEADLINE_INDEFINITE, 1, &did_wait, &result,
377 &context, &hss); 378 &context, &hss);
378 stopwatch.Start(); 379 stopwatch.Start();
379 thread.Start(); 380 thread.Start();
380 test::Sleep(2 * test::EpsilonTimeout()); 381 ThreadSleep(2 * test::EpsilonTimeout());
381 // Wake it up by writing to |d0|. 382 // Wake it up by writing to |d0|.
382 buffer[0] = 123456789; 383 buffer[0] = 123456789;
383 EXPECT_EQ(MOJO_RESULT_OK, 384 EXPECT_EQ(MOJO_RESULT_OK,
384 d0->WriteMessage(UserPointer<const void>(buffer), kBufferSize, 385 d0->WriteMessage(UserPointer<const void>(buffer), kBufferSize,
385 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE)); 386 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE));
386 } // Joins the thread. 387 } // Joins the thread.
387 elapsed = stopwatch.Elapsed(); 388 elapsed = stopwatch.Elapsed();
388 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout()); 389 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout());
389 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout()); 390 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout());
390 EXPECT_TRUE(did_wait); 391 EXPECT_TRUE(did_wait);
(...skipping 29 matching lines...) Expand all
420 EXPECT_EQ(123456789, buffer[0]); 421 EXPECT_EQ(123456789, buffer[0]);
421 422
422 // Wait for readable on |d1| and close |d0| after some time, which should 423 // Wait for readable on |d1| and close |d0| after some time, which should
423 // cancel that wait. 424 // cancel that wait.
424 { 425 {
425 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE, 426 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE,
426 MOJO_DEADLINE_INDEFINITE, 3, &did_wait, &result, 427 MOJO_DEADLINE_INDEFINITE, 3, &did_wait, &result,
427 &context, &hss); 428 &context, &hss);
428 stopwatch.Start(); 429 stopwatch.Start();
429 thread.Start(); 430 thread.Start();
430 test::Sleep(2 * test::EpsilonTimeout()); 431 ThreadSleep(2 * test::EpsilonTimeout());
431 EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); 432 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
432 } // Joins the thread. 433 } // Joins the thread.
433 elapsed = stopwatch.Elapsed(); 434 elapsed = stopwatch.Elapsed();
434 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout()); 435 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout());
435 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout()); 436 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout());
436 EXPECT_TRUE(did_wait); 437 EXPECT_TRUE(did_wait);
437 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); 438 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
438 EXPECT_EQ(3u, context); 439 EXPECT_EQ(3u, context);
439 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); 440 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals);
440 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); 441 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals);
(...skipping 13 matching lines...) Expand all
454 } 455 }
455 456
456 // Wait for readable on |d1| and close |d1| after some time, which should 457 // Wait for readable on |d1| and close |d1| after some time, which should
457 // cancel that wait. 458 // cancel that wait.
458 { 459 {
459 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE, 460 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE,
460 MOJO_DEADLINE_INDEFINITE, 4, &did_wait, &result, 461 MOJO_DEADLINE_INDEFINITE, 4, &did_wait, &result,
461 &context, &hss); 462 &context, &hss);
462 stopwatch.Start(); 463 stopwatch.Start();
463 thread.Start(); 464 thread.Start();
464 test::Sleep(2 * test::EpsilonTimeout()); 465 ThreadSleep(2 * test::EpsilonTimeout());
465 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); 466 EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
466 } // Joins the thread. 467 } // Joins the thread.
467 elapsed = stopwatch.Elapsed(); 468 elapsed = stopwatch.Elapsed();
468 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout()); 469 EXPECT_GT(elapsed, (2 - 1) * test::EpsilonTimeout());
469 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout()); 470 EXPECT_LT(elapsed, (2 + 1) * test::EpsilonTimeout());
470 EXPECT_TRUE(did_wait); 471 EXPECT_TRUE(did_wait);
471 EXPECT_EQ(MOJO_RESULT_CANCELLED, result); 472 EXPECT_EQ(MOJO_RESULT_CANCELLED, result);
472 EXPECT_EQ(4u, context); 473 EXPECT_EQ(4u, context);
473 EXPECT_EQ(0u, hss.satisfied_signals); 474 EXPECT_EQ(0u, hss.satisfied_signals);
474 EXPECT_EQ(0u, hss.satisfiable_signals); 475 EXPECT_EQ(0u, hss.satisfiable_signals);
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 EXPECT_EQ(total_messages_written, total_messages_read); 682 EXPECT_EQ(total_messages_written, total_messages_read);
682 EXPECT_EQ(total_bytes_written, total_bytes_read); 683 EXPECT_EQ(total_bytes_written, total_bytes_read);
683 684
684 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); 685 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close());
685 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); 686 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close());
686 } 687 }
687 688
688 } // namespace 689 } // namespace
689 } // namespace system 690 } // namespace system
690 } // namespace mojo 691 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/data_pipe_impl_unittest.cc ('k') | mojo/edk/system/message_pipe_test_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698