Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS 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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 5 | #include "update_engine/update_manager/event_loop.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 6 | |
| 7 | #include <base/bind.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | #include "update_engine/test_utils.h" |
| 11 | |
| 12 | using base::Bind; |
| 13 | using base::TimeDelta; |
| 14 | using chromeos_update_engine::RunGMainLoopMaxIterations; |
| 15 | using chromeos_update_engine::RunGMainLoopUntil; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // Sets the value of the passed pointer to true. |
| 20 | void SetTrue(bool* value) { |
| 21 | *value = true; |
| 22 | } |
| 23 | |
| 24 | bool GetBoolean(bool* value) { |
| 25 | return *value; |
| 26 | } |
| 27 | |
| 28 | } // namespace |
| 29 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | namespace chromeos_update_manager { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 31 | |
| 32 | class EventLoopTest : public ::testing::Test {}; |
| 33 | |
| 34 | TEST(EventLoopTest, RunFromMainLoopTest) { |
| 35 | bool called = false; |
| 36 | EventId ev = RunFromMainLoop(Bind(SetTrue, &called)); |
| 37 | EXPECT_NE(0, ev); |
| 38 | RunGMainLoopMaxIterations(100); |
| 39 | EXPECT_TRUE(called); |
| 40 | } |
| 41 | |
| 42 | // Tests that we can cancel events right after we schedule them. |
| 43 | TEST(EventLoopTest, RunFromMainLoopCancelTest) { |
| 44 | bool called = false; |
| 45 | EventId ev = RunFromMainLoop(Bind(SetTrue, &called)); |
| 46 | EXPECT_NE(0, ev); |
| 47 | EXPECT_TRUE(CancelMainLoopEvent(ev)); |
| 48 | RunGMainLoopMaxIterations(100); |
| 49 | EXPECT_FALSE(called); |
| 50 | } |
| 51 | |
| 52 | TEST(EventLoopTest, RunFromMainLoopAfterTimeoutTest) { |
| 53 | bool called = false; |
| 54 | EventId ev = RunFromMainLoopAfterTimeout(Bind(SetTrue, &called), |
| 55 | TimeDelta::FromSeconds(1)); |
| 56 | EXPECT_NE(0, ev); |
| 57 | RunGMainLoopUntil(10000, Bind(GetBoolean, &called)); |
| 58 | // Check that the main loop finished before the 10 seconds timeout. |
| 59 | EXPECT_TRUE(called); |
| 60 | } |
| 61 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 62 | } // namespace chromeos_update_manager |