Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [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 | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 7 | #include <base/bind.h> |
| 8 | #include <base/memory/scoped_ptr.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 11 | #include "update_engine/policy_manager/evaluation_context.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 12 | #include "update_engine/policy_manager/fake_variable.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 13 | #include "update_engine/policy_manager/generic_variables.h" |
| 14 | #include "update_engine/policy_manager/pmtest_utils.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 15 | #include "update_engine/test_utils.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 17 | using base::Bind; |
| 18 | using base::TimeDelta; |
| 19 | using chromeos_update_engine::RunGMainLoopMaxIterations; |
| 20 | using chromeos_update_engine::RunGMainLoopUntil; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 21 | using std::string; |
| 22 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | void DoNothing() {} |
| 26 | |
| 27 | // Sets the value of the passed pointer to true. |
| 28 | void SetTrue(bool* value) { |
| 29 | *value = true; |
| 30 | } |
| 31 | |
| 32 | bool GetBoolean(bool* value) { |
| 33 | return *value; |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 38 | namespace chromeos_policy_manager { |
| 39 | |
| 40 | class PmEvaluationContextTest : public ::testing::Test { |
| 41 | public: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 42 | PmEvaluationContextTest() {} |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 43 | |
| 44 | protected: |
| 45 | virtual void SetUp() { |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 46 | eval_ctx_ = new EvaluationContext(); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 49 | virtual void TearDown() { |
| 50 | eval_ctx_ = NULL; |
| 51 | // Check that the evaluation context removed all the observers. |
| 52 | EXPECT_TRUE(fake_int_var_.observer_list_.empty()); |
| 53 | EXPECT_TRUE(fake_async_var_.observer_list_.empty()); |
| 54 | EXPECT_TRUE(fake_const_var_.observer_list_.empty()); |
| 55 | EXPECT_TRUE(fake_poll_var_.observer_list_.empty()); |
| 56 | } |
| 57 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 58 | scoped_refptr<EvaluationContext> eval_ctx_; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 59 | |
| 60 | // FakeVariables used for testing the EvaluationContext. These are required |
| 61 | // here to prevent them from going away *before* the EvaluationContext under |
| 62 | // test does, which keeps a reference to them. |
| 63 | FakeVariable<int> fake_int_var_ = {"fake_int", kVariableModePoll}; |
| 64 | FakeVariable<string> fake_async_var_ = {"fake_async", kVariableModeAsync}; |
| 65 | FakeVariable<string> fake_const_var_ = {"fake_const", kVariableModeConst}; |
| 66 | FakeVariable<string> fake_poll_var_ = {"fake_poll", |
| 67 | TimeDelta::FromSeconds(1)}; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | TEST_F(PmEvaluationContextTest, GetValueFails) { |
| 71 | // FakeVariable is initialized as returning NULL. |
| 72 | PMTEST_EXPECT_NULL(eval_ctx_->GetValue(&fake_int_var_)); |
| 73 | } |
| 74 | |
| 75 | TEST_F(PmEvaluationContextTest, GetValueFailsWithInvalidVar) { |
| 76 | PMTEST_EXPECT_NULL(eval_ctx_->GetValue( |
| 77 | reinterpret_cast<Variable<int>*>(NULL))); |
| 78 | } |
| 79 | |
| 80 | TEST_F(PmEvaluationContextTest, GetValueReturns) { |
| 81 | const int* p_fake_int; |
| 82 | |
| 83 | fake_int_var_.reset(new int(42)); |
| 84 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 85 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 86 | EXPECT_EQ(42, *p_fake_int); |
| 87 | } |
| 88 | |
| 89 | TEST_F(PmEvaluationContextTest, GetValueCached) { |
| 90 | const int* p_fake_int; |
| 91 | |
| 92 | fake_int_var_.reset(new int(42)); |
| 93 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 94 | |
| 95 | // Check that if the variable changes, the EvaluationContext keeps returning |
| 96 | // the cached value. |
| 97 | fake_int_var_.reset(new int(5)); |
| 98 | |
| 99 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 100 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 101 | EXPECT_EQ(42, *p_fake_int); |
| 102 | } |
| 103 | |
| 104 | TEST_F(PmEvaluationContextTest, GetValueDontCacheNULL) { |
| 105 | const int* p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 106 | PMTEST_EXPECT_NULL(p_fake_int); |
| 107 | |
| 108 | fake_int_var_.reset(new int(42)); |
| 109 | // A second attempt to read the variable should work even on the same |
| 110 | // EvaluationContext. |
| 111 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
| 112 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 113 | EXPECT_EQ(42, *p_fake_int); |
| 114 | } |
| 115 | |
| 116 | TEST_F(PmEvaluationContextTest, GetValueMixedTypes) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 117 | const int* p_fake_int; |
| 118 | const string* p_fake_string; |
| 119 | |
| 120 | fake_int_var_.reset(new int(42)); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 121 | fake_poll_var_.reset(new string("Hello world!")); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 122 | // Check that the EvaluationContext can handle multiple Variable types. This |
| 123 | // is mostly a compile-time check due to the template nature of this method. |
| 124 | p_fake_int = eval_ctx_->GetValue(&fake_int_var_); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 125 | p_fake_string = eval_ctx_->GetValue(&fake_poll_var_); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 126 | |
| 127 | PMTEST_ASSERT_NOT_NULL(p_fake_int); |
| 128 | EXPECT_EQ(42, *p_fake_int); |
| 129 | |
| 130 | PMTEST_ASSERT_NOT_NULL(p_fake_string); |
| 131 | EXPECT_EQ("Hello world!", *p_fake_string); |
| 132 | } |
| 133 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 134 | // Test that we don't schedule an event if there's no variable to wait for. |
| 135 | TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutWithoutVariablesTest) { |
| 136 | fake_const_var_.reset(new string("Hello world!")); |
| 137 | EXPECT_EQ(*eval_ctx_->GetValue(&fake_const_var_), "Hello world!"); |
| 138 | |
| 139 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 140 | } |
| 141 | |
| 142 | // Test that we don't schedule an event if there's no variable to wait for. |
| 143 | TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutWithVariablesTest) { |
| 144 | fake_async_var_.reset(new string("Async value")); |
| 145 | eval_ctx_->GetValue(&fake_async_var_); |
| 146 | |
| 147 | bool value = false; |
| 148 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 149 | // Check that the scheduled callback isn't run until we signal a ValueChaged. |
| 150 | RunGMainLoopMaxIterations(100); |
| 151 | EXPECT_FALSE(value); |
| 152 | |
| 153 | fake_async_var_.NotifyValueChanged(); |
| 154 | EXPECT_FALSE(value); |
| 155 | // Ensure that the scheduled callback isn't run until we are back on the main |
| 156 | // loop. |
| 157 | RunGMainLoopMaxIterations(100); |
| 158 | EXPECT_TRUE(value); |
| 159 | } |
| 160 | |
| 161 | // Test that we don't re-schedule the events if we are attending one. |
| 162 | TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutCalledTwiceTest) { |
| 163 | fake_async_var_.reset(new string("Async value")); |
| 164 | eval_ctx_->GetValue(&fake_async_var_); |
| 165 | |
| 166 | bool value = false; |
| 167 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 168 | EXPECT_FALSE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 169 | |
| 170 | // The scheduled event should still work. |
| 171 | fake_async_var_.NotifyValueChanged(); |
| 172 | RunGMainLoopMaxIterations(100); |
| 173 | EXPECT_TRUE(value); |
| 174 | } |
| 175 | |
| 176 | // Test that we clear the events when destroying the EvaluationContext. |
| 177 | TEST_F(PmEvaluationContextTest, RemoveObserversAndTimeoutTest) { |
| 178 | fake_async_var_.reset(new string("Async value")); |
| 179 | eval_ctx_->GetValue(&fake_async_var_); |
| 180 | |
| 181 | bool value = false; |
| 182 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 183 | eval_ctx_ = NULL; |
| 184 | |
| 185 | // This should not trigger the callback since the EvaluationContext waiting |
| 186 | // for it is gone, and it should have remove all its observers. |
| 187 | fake_async_var_.NotifyValueChanged(); |
| 188 | RunGMainLoopMaxIterations(100); |
| 189 | EXPECT_FALSE(value); |
| 190 | } |
| 191 | |
| 192 | // Test that we don't schedule an event if there's no variable to wait for. |
| 193 | TEST_F(PmEvaluationContextTest, RunOnValueChangeOrTimeoutRunsFromTimeoutTest) { |
| 194 | fake_poll_var_.reset(new string("Polled value")); |
| 195 | eval_ctx_->GetValue(&fake_poll_var_); |
| 196 | |
| 197 | bool value = false; |
| 198 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 199 | // Check that the scheduled callback isn't run until the timeout occurs. |
| 200 | RunGMainLoopMaxIterations(10); |
| 201 | EXPECT_FALSE(value); |
| 202 | RunGMainLoopUntil(10000, Bind(&GetBoolean, &value)); |
| 203 | EXPECT_TRUE(value); |
| 204 | } |
| 205 | |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame^] | 206 | // Test that we can delete the EvaluationContext while having pending events. |
| 207 | TEST_F(PmEvaluationContextTest, ObjectDeletedWithPendingEventsTest) { |
| 208 | fake_async_var_.reset(new string("Async value")); |
| 209 | fake_poll_var_.reset(new string("Polled value")); |
| 210 | eval_ctx_->GetValue(&fake_async_var_); |
| 211 | eval_ctx_->GetValue(&fake_poll_var_); |
| 212 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&DoNothing))); |
| 213 | // TearDown() checks for leaked observers on this async_variable, which means |
| 214 | // that our object is still alive after removing its reference. |
| 215 | } |
| 216 | |
| 217 | // Test that timed events fired after removal of the EvaluationContext don't |
| 218 | // crash. |
| 219 | TEST_F(PmEvaluationContextTest, TimeoutEventAfterDeleteTest) { |
| 220 | FakeVariable<string> fake_short_poll_var = {"fake_short_poll", TimeDelta()}; |
| 221 | fake_short_poll_var.reset(new string("Polled value")); |
| 222 | eval_ctx_->GetValue(&fake_short_poll_var); |
| 223 | bool value = false; |
| 224 | EXPECT_TRUE(eval_ctx_->RunOnValueChangeOrTimeout(Bind(&SetTrue, &value))); |
| 225 | // Remove the last reference to the EvaluationContext and run the loop for |
| 226 | // 1 second to give time to the main loop to trigger the timeout Event (of 0 |
| 227 | // seconds). Our callback should not be called because the EvaluationContext |
| 228 | // was removed before the timeout event is attended. |
| 229 | eval_ctx_ = NULL; |
| 230 | RunGMainLoopUntil(1000, Bind(&GetBoolean, &value)); |
| 231 | EXPECT_FALSE(value); |
| 232 | } |
| 233 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 234 | } // namespace chromeos_policy_manager |