Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 1 | // Copyright 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 | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "buffet/commands/command_queue.h" |
| 6 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 7 | #include <set> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 8 | #include <string> |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 9 | #include <vector> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 10 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 11 | #include <base/bind.h> |
| 12 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 13 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
| 15 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 16 | #include "buffet/commands/command_definition.h" |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 17 | #include "buffet/commands/object_schema.h" |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 18 | |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 19 | namespace buffet { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 20 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 21 | class CommandQueueTest : public testing::Test { |
| 22 | public: |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 23 | std::unique_ptr<CommandInstance> CreateDummyCommandInstance( |
| 24 | const std::string& name, |
| 25 | const std::string& id) { |
| 26 | std::unique_ptr<CommandInstance> cmd{ |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 27 | new CommandInstance{name, "local", &command_definition_, {}}}; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 28 | cmd->SetID(id); |
| 29 | return cmd; |
| 30 | } |
| 31 | |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 32 | bool Remove(const std::string& id) { return queue_.Remove(id); } |
| 33 | |
| 34 | void Cleanup(const base::TimeDelta& interval) { |
| 35 | queue_.SetNowForTest(base::Time::Now() + interval); |
| 36 | return queue_.Cleanup(); |
| 37 | } |
| 38 | |
| 39 | CommandQueue queue_; |
| 40 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 41 | private: |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 42 | CommandDefinition command_definition_{"powerd", |
| 43 | ObjectSchema::Create(), |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 44 | ObjectSchema::Create(), |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 45 | ObjectSchema::Create()}; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 46 | }; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 47 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 48 | // Keeps track of commands being added to and removed from the queue_. |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 49 | // Aborts if duplicate commands are added or non-existent commands are removed. |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 50 | class FakeDispatcher { |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 51 | public: |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 52 | explicit FakeDispatcher(CommandQueue* queue) { |
| 53 | queue->AddOnCommandAddedCallback( |
| 54 | base::Bind(&FakeDispatcher::OnCommandAdded, |
| 55 | weak_ptr_factory_.GetWeakPtr())); |
| 56 | queue->AddOnCommandRemovedCallback( |
| 57 | base::Bind(&FakeDispatcher::OnCommandRemoved, |
| 58 | weak_ptr_factory_.GetWeakPtr())); |
| 59 | } |
| 60 | |
| 61 | void OnCommandAdded(CommandInstance* command_instance) { |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 62 | CHECK(ids_.insert(command_instance->GetID()).second) |
| 63 | << "Command ID already exists: " << command_instance->GetID(); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 64 | CHECK(commands_.insert(command_instance).second) |
| 65 | << "Command instance already exists"; |
| 66 | } |
| 67 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 68 | void OnCommandRemoved(CommandInstance* command_instance) { |
| 69 | CHECK_EQ(1u, ids_.erase(command_instance->GetID())) |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 70 | << "Command ID not found: " << command_instance->GetID(); |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 71 | CHECK_EQ(1u, commands_.erase(command_instance)) |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 72 | << "Command instance not found"; |
| 73 | } |
| 74 | |
| 75 | // Get the comma-separated list of command IDs currently accumulated in the |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 76 | // command queue_. |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 77 | std::string GetIDs() const { |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 78 | using chromeos::string_utils::Join; |
Vitaly Buka | db770e7 | 2015-03-10 19:33:33 -0700 | [diff] [blame] | 79 | return Join(",", std::vector<std::string>(ids_.begin(), ids_.end())); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private: |
| 83 | std::set<std::string> ids_; |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 84 | std::set<CommandInstance*> commands_; |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 85 | base::WeakPtrFactory<FakeDispatcher> weak_ptr_factory_{this}; |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 88 | TEST_F(CommandQueueTest, Empty) { |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(queue_.IsEmpty()); |
| 90 | EXPECT_EQ(0, queue_.GetCount()); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 93 | TEST_F(CommandQueueTest, Add) { |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 94 | queue_.Add(CreateDummyCommandInstance("base.reboot", "id1")); |
| 95 | queue_.Add(CreateDummyCommandInstance("base.reboot", "id2")); |
| 96 | queue_.Add(CreateDummyCommandInstance("base.reboot", "id3")); |
| 97 | EXPECT_EQ(3, queue_.GetCount()); |
| 98 | EXPECT_FALSE(queue_.IsEmpty()); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 101 | TEST_F(CommandQueueTest, Remove) { |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 102 | const std::string id1 = "id1"; |
| 103 | const std::string id2 = "id2"; |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 104 | queue_.Add(CreateDummyCommandInstance("base.reboot", id1)); |
| 105 | queue_.Add(CreateDummyCommandInstance("base.reboot", id2)); |
| 106 | EXPECT_FALSE(queue_.IsEmpty()); |
| 107 | EXPECT_FALSE(Remove("dummy")); |
| 108 | EXPECT_EQ(2, queue_.GetCount()); |
| 109 | EXPECT_TRUE(Remove(id1)); |
| 110 | EXPECT_EQ(1, queue_.GetCount()); |
| 111 | EXPECT_FALSE(Remove(id1)); |
| 112 | EXPECT_EQ(1, queue_.GetCount()); |
| 113 | EXPECT_TRUE(Remove(id2)); |
| 114 | EXPECT_EQ(0, queue_.GetCount()); |
| 115 | EXPECT_FALSE(Remove(id2)); |
| 116 | EXPECT_EQ(0, queue_.GetCount()); |
| 117 | EXPECT_TRUE(queue_.IsEmpty()); |
| 118 | } |
| 119 | |
| 120 | TEST_F(CommandQueueTest, DelayedRemove) { |
| 121 | const std::string id1 = "id1"; |
| 122 | queue_.Add(CreateDummyCommandInstance("base.reboot", id1)); |
| 123 | EXPECT_EQ(1, queue_.GetCount()); |
| 124 | |
| 125 | queue_.DelayedRemove(id1); |
| 126 | EXPECT_EQ(1, queue_.GetCount()); |
| 127 | |
| 128 | Cleanup(base::TimeDelta::FromMinutes(1)); |
| 129 | EXPECT_EQ(1, queue_.GetCount()); |
| 130 | |
| 131 | Cleanup(base::TimeDelta::FromMinutes(15)); |
| 132 | EXPECT_EQ(0, queue_.GetCount()); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 135 | TEST_F(CommandQueueTest, Dispatch) { |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 136 | FakeDispatcher dispatch(&queue_); |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 137 | const std::string id1 = "id1"; |
| 138 | const std::string id2 = "id2"; |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 139 | queue_.Add(CreateDummyCommandInstance("base.reboot", id1)); |
| 140 | queue_.Add(CreateDummyCommandInstance("base.reboot", id2)); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 141 | std::set<std::string> ids{id1, id2}; // Make sure they are sorted properly. |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 142 | std::string expected_set = chromeos::string_utils::Join( |
Vitaly Buka | db770e7 | 2015-03-10 19:33:33 -0700 | [diff] [blame] | 143 | ",", std::vector<std::string>(ids.begin(), ids.end())); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 144 | EXPECT_EQ(expected_set, dispatch.GetIDs()); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 145 | Remove(id1); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 146 | EXPECT_EQ(id2, dispatch.GetIDs()); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 147 | Remove(id2); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 148 | EXPECT_EQ("", dispatch.GetIDs()); |
| 149 | } |
| 150 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 151 | TEST_F(CommandQueueTest, Find) { |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 152 | const std::string id1 = "id1"; |
| 153 | const std::string id2 = "id2"; |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 154 | queue_.Add(CreateDummyCommandInstance("base.reboot", id1)); |
| 155 | queue_.Add(CreateDummyCommandInstance("base.shutdown", id2)); |
| 156 | EXPECT_EQ(nullptr, queue_.Find("dummy")); |
| 157 | auto cmd1 = queue_.Find(id1); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 158 | EXPECT_NE(nullptr, cmd1); |
| 159 | EXPECT_EQ("base.reboot", cmd1->GetName()); |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 160 | EXPECT_EQ(id1, cmd1->GetID()); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 161 | auto cmd2 = queue_.Find(id2); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 162 | EXPECT_NE(nullptr, cmd2); |
| 163 | EXPECT_EQ("base.shutdown", cmd2->GetName()); |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 164 | EXPECT_EQ(id2, cmd2->GetID()); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 165 | } |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 166 | |
| 167 | } // namespace buffet |