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