blob: 59d68256666ea982acb6e2da795d5b551fed033a [file] [log] [blame]
Andrew de los Reyes35a7af12010-03-10 16:33:26 -08001// Copyright (c) 2010 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
5#include <set>
6#include <string>
7#include <utility>
8#include <vector>
9#include <gtest/gtest.h>
10#include "chromeos/obsolete_logging.h"
11#include "update_engine/cycle_breaker.h"
12#include "update_engine/graph_types.h"
13#include "update_engine/utils.h"
14
15using std::make_pair;
16using std::pair;
17using std::set;
18using std::string;
19using std::vector;
20
21namespace chromeos_update_engine {
22
23class CycleBreakerTest : public ::testing::Test {};
24
25TEST(CycleBreakerTest, SimpleTest) {
26 int counter = 0;
27 const Vertex::Index n_a = counter++;
28 const Vertex::Index n_b = counter++;
29 const Vertex::Index n_c = counter++;
30 const Vertex::Index n_d = counter++;
31 const Vertex::Index n_e = counter++;
32 const Vertex::Index n_f = counter++;
33 const Vertex::Index n_g = counter++;
34 const Vertex::Index n_h = counter++;
35 const Graph::size_type kNodeCount = counter++;
36
37 Graph graph(kNodeCount);
38
39 graph[n_a].out_edges.insert(make_pair(n_e, EdgeProperties()));
40 graph[n_a].out_edges.insert(make_pair(n_f, EdgeProperties()));
41 graph[n_b].out_edges.insert(make_pair(n_a, EdgeProperties()));
42 graph[n_c].out_edges.insert(make_pair(n_d, EdgeProperties()));
43 graph[n_d].out_edges.insert(make_pair(n_e, EdgeProperties()));
44 graph[n_d].out_edges.insert(make_pair(n_f, EdgeProperties()));
45 graph[n_e].out_edges.insert(make_pair(n_b, EdgeProperties()));
46 graph[n_e].out_edges.insert(make_pair(n_c, EdgeProperties()));
47 graph[n_e].out_edges.insert(make_pair(n_f, EdgeProperties()));
48 graph[n_f].out_edges.insert(make_pair(n_g, EdgeProperties()));
49 graph[n_g].out_edges.insert(make_pair(n_h, EdgeProperties()));
50 graph[n_h].out_edges.insert(make_pair(n_g, EdgeProperties()));
51
52 CycleBreaker breaker;
53
54 set<Edge> broken_edges;
55 breaker.BreakCycles(graph, &broken_edges);
56
57 // The following cycles must be cut:
58 // A->E->B
59 // C->D->E
60 // G->H
61
62 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_e)) ||
63 utils::SetContainsKey(broken_edges, make_pair(n_e, n_b)) ||
64 utils::SetContainsKey(broken_edges, make_pair(n_b, n_a)));
65 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_c, n_d)) ||
66 utils::SetContainsKey(broken_edges, make_pair(n_d, n_e)) ||
67 utils::SetContainsKey(broken_edges, make_pair(n_e, n_c)));
68 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_g, n_h)) ||
69 utils::SetContainsKey(broken_edges, make_pair(n_h, n_g)));
70 EXPECT_EQ(3, broken_edges.size());
71}
72
73namespace {
74pair<Vertex::Index, EdgeProperties> EdgeWithWeight(Vertex::Index dest,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070075uint64_t weight) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080076 EdgeProperties props;
77 props.extents.resize(1);
78 props.extents[0].set_num_blocks(weight);
79 return make_pair(dest, props);
80}
81} // namespace {}
82
Andrew de los Reyes45109892010-07-26 13:49:31 -070083
84// This creates a bunch of cycles like this:
85//
86// root <------.
87// (t)-> / | \ |
88// V V V |
89// N N N |
90// \ | / |
91// VVV |
92// N |
93// / | \ |
94// V V V |
95// N N N |
96// ... |
97// (s)-> \ | / |
98// VVV |
99// N |
100// \_________/
101//
102// such that the original cutting algo would cut edges (s). We changed
103// the algorithm to cut cycles (t) instead, since they are closer to the
104// root, and that can massively speed up cycle cutting.
105TEST(CycleBreakerTest, AggressiveCutTest) {
106 int counter = 0;
107
108 const int kNodesPerGroup = 4;
109 const int kGroups = 33;
110
111 Graph graph(kGroups * kNodesPerGroup + 1); // + 1 for the root node
112
113 const Vertex::Index n_root = counter++;
114
115 Vertex::Index last_hub = n_root;
116 for (int i = 0; i < kGroups; i++) {
117 uint64_t weight = 5;
118 if (i == 0)
119 weight = 2;
120 else if (i == (kGroups - 1))
121 weight = 1;
122
123 const Vertex::Index next_hub = counter++;
124
125 for (int j = 0; j < (kNodesPerGroup - 1); j++) {
126 const Vertex::Index node = counter++;
127 graph[last_hub].out_edges.insert(EdgeWithWeight(node, weight));
128 graph[node].out_edges.insert(EdgeWithWeight(next_hub, weight));
129 }
130 last_hub = next_hub;
131 }
132
133 graph[last_hub].out_edges.insert(EdgeWithWeight(n_root, 5));
134
135
136 EXPECT_EQ(counter, graph.size());
137
138 CycleBreaker breaker;
139
140 set<Edge> broken_edges;
141 LOG(INFO) << "If this hangs for more than 1 second, the test has failed.";
142 breaker.BreakCycles(graph, &broken_edges);
143
144 set<Edge> expected_cuts;
145
146 for (Vertex::EdgeMap::const_iterator it = graph[n_root].out_edges.begin(),
147 e = graph[n_root].out_edges.end(); it != e; ++it) {
148 expected_cuts.insert(make_pair(n_root, it->first));
149 }
150
151 EXPECT_TRUE(broken_edges == expected_cuts);
152}
153
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800154TEST(CycleBreakerTest, WeightTest) {
155 int counter = 0;
156 const Vertex::Index n_a = counter++;
157 const Vertex::Index n_b = counter++;
158 const Vertex::Index n_c = counter++;
159 const Vertex::Index n_d = counter++;
160 const Vertex::Index n_e = counter++;
161 const Vertex::Index n_f = counter++;
162 const Vertex::Index n_g = counter++;
163 const Vertex::Index n_h = counter++;
164 const Vertex::Index n_i = counter++;
165 const Vertex::Index n_j = counter++;
166 const Graph::size_type kNodeCount = counter++;
167
168 Graph graph(kNodeCount);
169
170 graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 4));
171 graph[n_a].out_edges.insert(EdgeWithWeight(n_f, 3));
172 graph[n_a].out_edges.insert(EdgeWithWeight(n_h, 2));
173 graph[n_b].out_edges.insert(EdgeWithWeight(n_a, 3));
174 graph[n_b].out_edges.insert(EdgeWithWeight(n_c, 4));
175 graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 5));
176 graph[n_c].out_edges.insert(EdgeWithWeight(n_d, 3));
177 graph[n_d].out_edges.insert(EdgeWithWeight(n_a, 6));
178 graph[n_d].out_edges.insert(EdgeWithWeight(n_e, 3));
179 graph[n_e].out_edges.insert(EdgeWithWeight(n_d, 4));
180 graph[n_e].out_edges.insert(EdgeWithWeight(n_g, 5));
181 graph[n_f].out_edges.insert(EdgeWithWeight(n_g, 2));
182 graph[n_g].out_edges.insert(EdgeWithWeight(n_f, 3));
183 graph[n_g].out_edges.insert(EdgeWithWeight(n_d, 5));
184 graph[n_h].out_edges.insert(EdgeWithWeight(n_i, 8));
185 graph[n_i].out_edges.insert(EdgeWithWeight(n_e, 4));
186 graph[n_i].out_edges.insert(EdgeWithWeight(n_h, 9));
187 graph[n_i].out_edges.insert(EdgeWithWeight(n_j, 6));
188
189 CycleBreaker breaker;
190
191 set<Edge> broken_edges;
192 breaker.BreakCycles(graph, &broken_edges);
193
194 // These are required to be broken:
195 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_b, n_a)));
196 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_b, n_c)));
197 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_d, n_e)));
198 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_f, n_g)));
199 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_h, n_i)));
200}
201
Andrew de los Reyes8a51e5c2010-07-26 13:40:03 -0700202TEST(CycleBreakerTest, UnblockGraphTest) {
203 int counter = 0;
204 const Vertex::Index n_a = counter++;
205 const Vertex::Index n_b = counter++;
206 const Vertex::Index n_c = counter++;
207 const Vertex::Index n_d = counter++;
208 const Graph::size_type kNodeCount = counter++;
209
210 Graph graph(kNodeCount);
211
212 graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 1));
213 graph[n_a].out_edges.insert(EdgeWithWeight(n_c, 1));
214 graph[n_b].out_edges.insert(EdgeWithWeight(n_c, 2));
215 graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 2));
216 graph[n_b].out_edges.insert(EdgeWithWeight(n_d, 2));
217 graph[n_d].out_edges.insert(EdgeWithWeight(n_a, 2));
218
219 CycleBreaker breaker;
220
221 set<Edge> broken_edges;
222 breaker.BreakCycles(graph, &broken_edges);
223
224 // These are required to be broken:
225 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_b)));
226 EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_c)));
227}
228
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800229} // namespace chromeos_update_engine