blob: 69325fe6243459a1674d640651be4b9ba2eb788c [file] [log] [blame]
Alex Deymo53556ec2014-03-17 10:05:57 -07001// 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 Deymo63784a52014-05-28 10:46:14 -07005#include "update_engine/update_manager/event_loop.h"
Alex Deymo53556ec2014-03-17 10:05:57 -07006
7#include <base/bind.h>
8#include <gtest/gtest.h>
9
10#include "update_engine/test_utils.h"
11
12using base::Bind;
13using base::TimeDelta;
14using chromeos_update_engine::RunGMainLoopMaxIterations;
15using chromeos_update_engine::RunGMainLoopUntil;
16
17namespace {
18
19// Sets the value of the passed pointer to true.
20void SetTrue(bool* value) {
21 *value = true;
22}
23
24bool GetBoolean(bool* value) {
25 return *value;
26}
27
28} // namespace
29
Alex Deymo63784a52014-05-28 10:46:14 -070030namespace chromeos_update_manager {
Alex Deymo53556ec2014-03-17 10:05:57 -070031
32class EventLoopTest : public ::testing::Test {};
33
34TEST(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.
43TEST(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
52TEST(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 Deymo63784a52014-05-28 10:46:14 -070062} // namespace chromeos_update_manager