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 Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 5 | #include <set> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 6 | #include <string> |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 7 | #include <vector> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 8 | |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 9 | #include <chromeos/string_utils.h> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 12 | #include "buffet/commands/command_dispatch_interface.h" |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 13 | #include "buffet/commands/command_queue.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | std::unique_ptr<const buffet::CommandInstance> CreateDummyCommandInstance( |
| 18 | const std::string& name = "base.reboot") { |
| 19 | return std::unique_ptr<const buffet::CommandInstance>( |
| 20 | new buffet::CommandInstance(name, "powerd", {})); |
| 21 | } |
| 22 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 23 | // Fake implementation of CommandDispachInterface. |
| 24 | // Just keeps track of commands being added to and removed from the queue. |
| 25 | // Aborts if duplicate commands are added or non-existent commands are removed. |
| 26 | class FakeDispatchInterface : public buffet::CommandDispachInterface { |
| 27 | public: |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 28 | void OnCommandAdded( |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 29 | const std::string& command_id, |
| 30 | const buffet::CommandInstance* command_instance) override { |
| 31 | CHECK(ids_.insert(command_id).second) |
| 32 | << "Command ID already exists: " << command_id; |
| 33 | CHECK(commands_.insert(command_instance).second) |
| 34 | << "Command instance already exists"; |
| 35 | } |
| 36 | |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 37 | void OnCommandRemoved( |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 38 | const std::string& command_id, |
| 39 | const buffet::CommandInstance* command_instance) override { |
| 40 | CHECK_EQ(1, ids_.erase(command_id)) |
| 41 | << "Command ID not found: " << command_id; |
| 42 | CHECK_EQ(1, commands_.erase(command_instance)) |
| 43 | << "Command instance not found"; |
| 44 | } |
| 45 | |
| 46 | // Get the comma-separated list of command IDs currently accumulated in the |
| 47 | // command queue. |
| 48 | std::string GetIDs() const { |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 49 | using chromeos::string_utils::Join; |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 50 | return Join(',', std::vector<std::string>(ids_.begin(), ids_.end())); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | std::set<std::string> ids_; |
| 55 | std::set<const buffet::CommandInstance*> commands_; |
| 56 | }; |
| 57 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 58 | } // anonymous namespace |
| 59 | |
| 60 | TEST(CommandQueue, Empty) { |
| 61 | buffet::CommandQueue queue; |
| 62 | EXPECT_TRUE(queue.IsEmpty()); |
| 63 | EXPECT_EQ(0, queue.GetCount()); |
| 64 | } |
| 65 | |
| 66 | TEST(CommandQueue, Add) { |
| 67 | buffet::CommandQueue queue; |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 68 | std::set<std::string> ids; // Using set<> to check that IDs are unique. |
| 69 | ids.insert(queue.Add(CreateDummyCommandInstance())); |
| 70 | ids.insert(queue.Add(CreateDummyCommandInstance())); |
| 71 | ids.insert(queue.Add(CreateDummyCommandInstance())); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 72 | EXPECT_EQ(3, queue.GetCount()); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 73 | EXPECT_EQ(3, ids.size()); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 74 | EXPECT_FALSE(queue.IsEmpty()); |
| 75 | } |
| 76 | |
| 77 | TEST(CommandQueue, Remove) { |
| 78 | buffet::CommandQueue queue; |
| 79 | std::string id1 = queue.Add(CreateDummyCommandInstance()); |
| 80 | std::string id2 = queue.Add(CreateDummyCommandInstance()); |
| 81 | EXPECT_FALSE(queue.IsEmpty()); |
| 82 | EXPECT_EQ(nullptr, queue.Remove("dummy").get()); |
| 83 | EXPECT_EQ(2, queue.GetCount()); |
| 84 | EXPECT_NE(nullptr, queue.Remove(id1).get()); |
| 85 | EXPECT_EQ(1, queue.GetCount()); |
| 86 | EXPECT_EQ(nullptr, queue.Remove(id1).get()); |
| 87 | EXPECT_EQ(1, queue.GetCount()); |
| 88 | EXPECT_NE(nullptr, queue.Remove(id2).get()); |
| 89 | EXPECT_EQ(0, queue.GetCount()); |
| 90 | EXPECT_EQ(nullptr, queue.Remove(id2).get()); |
| 91 | EXPECT_EQ(0, queue.GetCount()); |
| 92 | EXPECT_TRUE(queue.IsEmpty()); |
| 93 | } |
| 94 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 95 | TEST(CommandQueue, Dispatch) { |
| 96 | FakeDispatchInterface dispatch; |
| 97 | buffet::CommandQueue queue; |
| 98 | queue.SetCommandDispachInterface(&dispatch); |
| 99 | std::string id1 = queue.Add(CreateDummyCommandInstance()); |
| 100 | std::string id2 = queue.Add(CreateDummyCommandInstance()); |
| 101 | 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] | 102 | std::string expected_set = chromeos::string_utils::Join( |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 103 | ',', std::vector<std::string>(ids.begin(), ids.end())); |
| 104 | EXPECT_EQ(expected_set, dispatch.GetIDs()); |
| 105 | queue.Remove(id1); |
| 106 | EXPECT_EQ(id2, dispatch.GetIDs()); |
| 107 | queue.Remove(id2); |
| 108 | EXPECT_EQ("", dispatch.GetIDs()); |
| 109 | } |
| 110 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 111 | TEST(CommandQueue, Find) { |
| 112 | buffet::CommandQueue queue; |
| 113 | std::string id1 = queue.Add(CreateDummyCommandInstance("base.reboot")); |
| 114 | std::string id2 = queue.Add(CreateDummyCommandInstance("base.shutdown")); |
| 115 | EXPECT_EQ(nullptr, queue.Find("dummy")); |
| 116 | auto cmd1 = queue.Find(id1); |
| 117 | EXPECT_NE(nullptr, cmd1); |
| 118 | EXPECT_EQ("base.reboot", cmd1->GetName()); |
| 119 | auto cmd2 = queue.Find(id2); |
| 120 | EXPECT_NE(nullptr, cmd2); |
| 121 | EXPECT_EQ("base.shutdown", cmd2->GetName()); |
| 122 | } |