| 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 |
| 6 // heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to |
| 7 // increase tolerance and reduce observed flakiness (though doing so reduces the |
| 8 // meaningfulness of the test). |
| 9 |
| 5 #include "mojo/edk/system/core.h" | 10 #include "mojo/edk/system/core.h" |
| 6 | 11 |
| 7 #include <stdint.h> | 12 #include <stdint.h> |
| 8 | 13 |
| 9 #include <limits> | 14 #include <limits> |
| 10 | 15 |
| 16 #include "mojo/edk/platform/test_stopwatch.h" |
| 11 #include "mojo/edk/platform/thread_utils.h" | 17 #include "mojo/edk/platform/thread_utils.h" |
| 12 #include "mojo/edk/system/awakable.h" | 18 #include "mojo/edk/system/awakable.h" |
| 13 #include "mojo/edk/system/core_test_base.h" | 19 #include "mojo/edk/system/core_test_base.h" |
| 14 #include "mojo/edk/system/test/timeouts.h" | 20 #include "mojo/edk/system/test/timeouts.h" |
| 15 #include "mojo/public/cpp/system/macros.h" | 21 #include "mojo/public/cpp/system/macros.h" |
| 16 | 22 |
| 23 using mojo::platform::test::Stopwatch; |
| 17 using mojo::platform::ThreadSleep; | 24 using mojo::platform::ThreadSleep; |
| 18 | 25 |
| 19 namespace mojo { | 26 namespace mojo { |
| 20 namespace system { | 27 namespace system { |
| 21 namespace { | 28 namespace { |
| 22 | 29 |
| 23 const MojoHandleRights kDefaultMessagePipeHandleRights = | 30 const MojoHandleRights kDefaultMessagePipeHandleRights = |
| 24 MOJO_HANDLE_RIGHT_TRANSFER | MOJO_HANDLE_RIGHT_READ | | 31 MOJO_HANDLE_RIGHT_TRANSFER | MOJO_HANDLE_RIGHT_READ | |
| 25 MOJO_HANDLE_RIGHT_WRITE | MOJO_HANDLE_RIGHT_GET_OPTIONS | | 32 MOJO_HANDLE_RIGHT_WRITE | MOJO_HANDLE_RIGHT_GET_OPTIONS | |
| 26 MOJO_HANDLE_RIGHT_SET_OPTIONS; | 33 MOJO_HANDLE_RIGHT_SET_OPTIONS; |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 hss.satisfied_signals); | 353 hss.satisfied_signals); |
| 347 EXPECT_EQ(kFullMojoHandleSignalsState.satisfiable_signals, | 354 EXPECT_EQ(kFullMojoHandleSignalsState.satisfiable_signals, |
| 348 hss.satisfiable_signals); | 355 hss.satisfiable_signals); |
| 349 } | 356 } |
| 350 | 357 |
| 351 // |WaitMany()|: | 358 // |WaitMany()|: |
| 352 { | 359 { |
| 353 MojoHandle handles[2] = {MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID}; | 360 MojoHandle handles[2] = {MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID}; |
| 354 MojoHandleSignals signals[2] = {~MOJO_HANDLE_SIGNAL_NONE, | 361 MojoHandleSignals signals[2] = {~MOJO_HANDLE_SIGNAL_NONE, |
| 355 ~MOJO_HANDLE_SIGNAL_NONE}; | 362 ~MOJO_HANDLE_SIGNAL_NONE}; |
| 356 EXPECT_EQ( | |
| 357 MOJO_RESULT_INVALID_ARGUMENT, | |
| 358 core()->WaitMany(MakeUserPointer(handles), MakeUserPointer(signals), 0, | |
| 359 MOJO_DEADLINE_INDEFINITE, NullUserPointer(), | |
| 360 NullUserPointer())); | |
| 361 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | |
| 362 core()->WaitMany(NullUserPointer(), MakeUserPointer(signals), 0, | |
| 363 MOJO_DEADLINE_INDEFINITE, NullUserPointer(), | |
| 364 NullUserPointer())); | |
| 365 // If |num_handles| is invalid, it should leave |result_index| and | |
| 366 // |signals_states| alone. | |
| 367 // (We use -1 internally; make sure that doesn't leak.) | |
| 368 uint32_t result_index = 123; | |
| 369 MojoHandleSignalsState hss = kFullMojoHandleSignalsState; | |
| 370 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | |
| 371 core()->WaitMany(NullUserPointer(), MakeUserPointer(signals), 0, | |
| 372 MOJO_DEADLINE_INDEFINITE, | |
| 373 MakeUserPointer(&result_index), | |
| 374 MakeUserPointer(&hss))); | |
| 375 EXPECT_EQ(123u, result_index); | |
| 376 EXPECT_EQ(kFullMojoHandleSignalsState.satisfied_signals, | |
| 377 hss.satisfied_signals); | |
| 378 EXPECT_EQ(kFullMojoHandleSignalsState.satisfiable_signals, | |
| 379 hss.satisfiable_signals); | |
| 380 | 363 |
| 381 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | |
| 382 core()->WaitMany(MakeUserPointer(handles), NullUserPointer(), 0, | |
| 383 MOJO_DEADLINE_INDEFINITE, NullUserPointer(), | |
| 384 NullUserPointer())); | |
| 385 EXPECT_EQ( | 364 EXPECT_EQ( |
| 386 MOJO_RESULT_INVALID_ARGUMENT, | 365 MOJO_RESULT_INVALID_ARGUMENT, |
| 387 core()->WaitMany(MakeUserPointer(handles), MakeUserPointer(signals), 1, | 366 core()->WaitMany(MakeUserPointer(handles), MakeUserPointer(signals), 1, |
| 388 MOJO_DEADLINE_INDEFINITE, NullUserPointer(), | 367 MOJO_DEADLINE_INDEFINITE, NullUserPointer(), |
| 389 NullUserPointer())); | 368 NullUserPointer())); |
| 369 |
| 390 // But if a handle is bad, then it should set |result_index| but still leave | 370 // But if a handle is bad, then it should set |result_index| but still leave |
| 391 // |signals_states| alone. | 371 // |signals_states| alone. |
| 392 result_index = static_cast<uint32_t>(-1); | 372 uint32_t result_index = static_cast<uint32_t>(-1); |
| 393 hss = kFullMojoHandleSignalsState; | 373 MojoHandleSignalsState hss = kFullMojoHandleSignalsState; |
| 394 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 374 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 395 core()->WaitMany( | 375 core()->WaitMany( |
| 396 MakeUserPointer(handles), MakeUserPointer(signals), 1, | 376 MakeUserPointer(handles), MakeUserPointer(signals), 1, |
| 397 MOJO_DEADLINE_INDEFINITE, MakeUserPointer(&result_index), | 377 MOJO_DEADLINE_INDEFINITE, MakeUserPointer(&result_index), |
| 398 MakeUserPointer(&hss))); | 378 MakeUserPointer(&hss))); |
| 399 EXPECT_EQ(0u, result_index); | 379 EXPECT_EQ(0u, result_index); |
| 400 EXPECT_EQ(kFullMojoHandleSignalsState.satisfied_signals, | 380 EXPECT_EQ(kFullMojoHandleSignalsState.satisfied_signals, |
| 401 hss.satisfied_signals); | 381 hss.satisfied_signals); |
| 402 EXPECT_EQ(kFullMojoHandleSignalsState.satisfiable_signals, | 382 EXPECT_EQ(kFullMojoHandleSignalsState.satisfiable_signals, |
| 403 hss.satisfiable_signals); | 383 hss.satisfiable_signals); |
| (...skipping 1638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2042 uint32_t num_results = 5; | 2022 uint32_t num_results = 5; |
| 2043 MojoWaitSetResult results[5] = {}; | 2023 MojoWaitSetResult results[5] = {}; |
| 2044 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, | 2024 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 2045 core()->WaitSetWait(h, static_cast<MojoDeadline>(0), | 2025 core()->WaitSetWait(h, static_cast<MojoDeadline>(0), |
| 2046 MakeUserPointer(&num_results), | 2026 MakeUserPointer(&num_results), |
| 2047 MakeUserPointer(results), NullUserPointer())); | 2027 MakeUserPointer(results), NullUserPointer())); |
| 2048 | 2028 |
| 2049 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h)); | 2029 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h)); |
| 2050 } | 2030 } |
| 2051 | 2031 |
| 2032 TEST_F(CoreTest, WaitTimeOut) { |
| 2033 Stopwatch stopwatch; |
| 2034 |
| 2035 // Make some handles we can wait on. |
| 2036 MojoHandle h[2] = {MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID}; |
| 2037 EXPECT_EQ(MOJO_RESULT_OK, |
| 2038 core()->CreateMessagePipe(NullUserPointer(), MakeUserPointer(&h[0]), |
| 2039 MakeUserPointer(&h[1]))); |
| 2040 |
| 2041 stopwatch.Start(); |
| 2042 core()->Wait(h[0], MOJO_HANDLE_SIGNAL_READABLE, 2 * test::EpsilonTimeout(), |
| 2043 NullUserPointer()); |
| 2044 MojoDeadline elapsed = stopwatch.Elapsed(); |
| 2045 EXPECT_GT(elapsed, test::EpsilonTimeout()); |
| 2046 EXPECT_LT(elapsed, 3 * test::EpsilonTimeout()); |
| 2047 |
| 2048 // Try |WaitMany()| with one handle. |
| 2049 static const MojoHandleSignals kHandleSignals[2] = { |
| 2050 MOJO_HANDLE_SIGNAL_READABLE, MOJO_HANDLE_SIGNAL_READABLE}; |
| 2051 stopwatch.Start(); |
| 2052 EXPECT_EQ( |
| 2053 MOJO_RESULT_DEADLINE_EXCEEDED, |
| 2054 core()->WaitMany(MakeUserPointer(h), MakeUserPointer(kHandleSignals), 1u, |
| 2055 2 * test::EpsilonTimeout(), NullUserPointer(), |
| 2056 NullUserPointer())); |
| 2057 elapsed = stopwatch.Elapsed(); |
| 2058 EXPECT_GT(elapsed, test::EpsilonTimeout()); |
| 2059 EXPECT_LT(elapsed, 3 * test::EpsilonTimeout()); |
| 2060 |
| 2061 // Try |WaitMany()| with two handles; also make sure it doesn't touch the |
| 2062 // |result_index| argument. |
| 2063 uint32_t result_index = 123u; |
| 2064 stopwatch.Start(); |
| 2065 EXPECT_EQ( |
| 2066 MOJO_RESULT_DEADLINE_EXCEEDED, |
| 2067 core()->WaitMany(MakeUserPointer(h), MakeUserPointer(kHandleSignals), 2u, |
| 2068 2 * test::EpsilonTimeout(), |
| 2069 MakeUserPointer(&result_index), NullUserPointer())); |
| 2070 elapsed = stopwatch.Elapsed(); |
| 2071 EXPECT_GT(elapsed, test::EpsilonTimeout()); |
| 2072 EXPECT_LT(elapsed, 3 * test::EpsilonTimeout()); |
| 2073 EXPECT_EQ(123u, result_index); |
| 2074 |
| 2075 // Try |WaitMany()| with two handles; also make sure it doesn't touch the |
| 2076 // |result_index| and |signals_states| arguments. |
| 2077 result_index = 123u; |
| 2078 MojoHandleSignalsState hss = kFullMojoHandleSignalsState; |
| 2079 stopwatch.Start(); |
| 2080 EXPECT_EQ( |
| 2081 MOJO_RESULT_DEADLINE_EXCEEDED, |
| 2082 core()->WaitMany(NullUserPointer(), NullUserPointer(), 0u, |
| 2083 2 * test::EpsilonTimeout(), |
| 2084 MakeUserPointer(&result_index), MakeUserPointer(&hss))); |
| 2085 elapsed = stopwatch.Elapsed(); |
| 2086 EXPECT_GT(elapsed, test::EpsilonTimeout()); |
| 2087 EXPECT_LT(elapsed, 3 * test::EpsilonTimeout()); |
| 2088 EXPECT_EQ(123u, result_index); |
| 2089 EXPECT_EQ(kFullMojoHandleSignalsState.satisfied_signals, |
| 2090 hss.satisfied_signals); |
| 2091 EXPECT_EQ(kFullMojoHandleSignalsState.satisfiable_signals, |
| 2092 hss.satisfiable_signals); |
| 2093 |
| 2094 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h[0])); |
| 2095 EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h[1])); |
| 2096 } |
| 2097 |
| 2052 } // namespace | 2098 } // namespace |
| 2053 } // namespace system | 2099 } // namespace system |
| 2054 } // namespace mojo | 2100 } // namespace mojo |
| OLD | NEW |