summaryrefslogtreecommitdiff
path: root/src/mongo/base/dependency_graph_test.cpp
blob: b79533bc081d5e4826bfd1e275724073a34efacf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
 *    Copyright (C) 2020-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

/**
 * Unit tests of the DependencyGraph type.
 */

#include <algorithm>
#include <string>
#include <vector>

#include <fmt/ranges.h>

#include "mongo/base/dependency_graph.h"
#include "mongo/base/init.h"
#include "mongo/unittest/unittest.h"

namespace mongo {
namespace {

template <typename C, typename T>
size_t count(const C& c, const T& value) {
    return std::count(c.begin(), c.end(), value);
}

TEST(DependencyGraphTest, InsertNullPayloadOkay) {
    DependencyGraph graph;
    graph.addNode("A", {}, {});
}

TEST(DependencyGraphTest, InsertSameNameTwiceFails) {
    DependencyGraph graph;
    graph.addNode("A", {}, {});
    ASSERT_THROWS_CODE(graph.addNode("A", {}, {}), DBException, 50999);
}

TEST(DependencyGraphTest, TopSortEmptyGraph) {
    DependencyGraph graph;
    std::vector<std::string> nodeNames = graph.topSort();
    ASSERT_EQUALS(nodeNames.size(), 0u);
}

TEST(DependencyGraphTest, TopSortGraphNoDeps) {
    DependencyGraph graph;
    graph.addNode("A", {}, {});
    graph.addNode("B", {}, {});
    graph.addNode("C", {}, {});
    auto nodeNames = graph.topSort();
    ASSERT_EQ(nodeNames.size(), 3);
    ASSERT_EQ(count(nodeNames, "A"), 1);
    ASSERT_EQ(count(nodeNames, "B"), 1);
    ASSERT_EQ(count(nodeNames, "C"), 1);
}

/**
 * Verify a node order for the diamond topology used in several tests.
 * Specifically, this graph:
 *
 * A
 * |
 * +->B
 * |  |
 * +---->C
 *    |  |
 *    +--+->D
 *
 * `B` and `C` have no order relative to each other, but both must
 * happen after `A` and before `D`.
 */
void checkDiamondTopology(const std::vector<std::string>& nodeNames) {
    ASSERT_STRING_SEARCH_REGEX(
        fmt::format("{}", fmt::join(nodeNames.begin(), nodeNames.end(), " ")), "^A (B C|C B) D$");
}

TEST(DependencyGraphTest, TopSortWithDiamondPrerequisites) {
    /*
     * Top-sorting a simple diamond specified using prerequisites.
     * See checkDiamondTopology for topology.
     */
    DependencyGraph graph;
    graph.addNode("A", {}, {});
    graph.addNode("B", {"A"}, {});
    graph.addNode("C", {"A"}, {});
    graph.addNode("D", {"B", "C"}, {});
    checkDiamondTopology(graph.topSort());
}

TEST(DependencyGraphTest, TopSortWithDiamondDependents) {
    /*
     * Same diamond topology as preceding test, but specified using dependents.
     */
    DependencyGraph graph;
    graph.addNode("A", {}, {"B", "C"});
    graph.addNode("B", {}, {"D"});
    graph.addNode("C", {}, {"D"});
    graph.addNode("D", {}, {});
    checkDiamondTopology(graph.topSort());
}

TEST(DependencyGraphTest, TopSortWithDiamondGeneral1) {
    /*
     * Same diamond topology, specified completely by prerequisites and
     * dependents declared on B and C.
     */
    DependencyGraph graph;
    graph.addNode("A", {}, {});
    graph.addNode("B", {"A"}, {"D"});
    graph.addNode("C", {"A"}, {"D"});
    graph.addNode("D", {}, {});
    checkDiamondTopology(graph.topSort());
}

TEST(DependencyGraphTest, TopSortWithDiamondGeneral2) {
    /*
     * Same diamond topology, specified completely by prerequisites and
     * dependents declared on A and D.
     */
    DependencyGraph graph;
    graph.addNode("A", {}, {"B", "C"});
    graph.addNode("B", {}, {});
    graph.addNode("C", {}, {});
    graph.addNode("D", {"C", "B"}, {});
    checkDiamondTopology(graph.topSort());
}

TEST(DependencyGraphTest, TopSortWithDiamondGeneral3) {
    /*
     * Same diamond topology, specified by redundant but coherent constraints.
     */
    DependencyGraph graph;
    graph.addNode("A", {}, {"B", "C"});
    graph.addNode("B", {"A"}, {"D"});
    graph.addNode("C", {"A"}, {"D"});
    graph.addNode("D", {"C", "B"}, {});
    checkDiamondTopology(graph.topSort());
}

TEST(DependencyGraphTest, TopSortWithDiamondAndCycle) {
    /*
     * Cyclic graph. Should fail.
     *
     * A
     * |
     * +->B<-------+
     * |  |        |
     * +---->C     |
     *    |  |     |
     *    +--+->D  |
     *          |  |
     *          +->E
     */
    DependencyGraph graph;
    graph.addNode("A", {}, {"B", "C"});
    graph.addNode("B", {}, {});
    graph.addNode("C", {}, {});
    graph.addNode("D", {"C", "B"}, {});
    graph.addNode("E", {"D"}, {"B"});

    std::vector<std::string> cycle;
    auto check = [](auto&& ex) {
        ASSERT_EQ(ex.code(), ErrorCodes::GraphContainsCycle) << ex.toString();
    };
    ASSERT_THROWS_WITH_CHECK(graph.topSort(&cycle), DBException, check);
    ASSERT_EQ(cycle.size(), 3);
    ASSERT_EQ(count(cycle, "B"), 1);
    ASSERT_EQ(count(cycle, "D"), 1);
    ASSERT_EQ(count(cycle, "E"), 1);
}

TEST(DependencyGraphTest, TopSortFailsWhenMissingPrerequisite) {
    /*
     * If a node names a never-declared prerequisite, topSort should fail.
     */
    DependencyGraph graph;
    graph.addNode("B", {"A"}, {});
    auto check = [&](const DBException& ex) {
        ASSERT_EQ(ex.code(), ErrorCodes::BadValue) << ex.toString();
        ASSERT_STRING_CONTAINS(ex.reason(), "node B depends on missing node A");
    };
    ASSERT_THROWS_WITH_CHECK(graph.topSort(), DBException, check);
}

TEST(DependencyGraphTest, TopSortFailsWhenMissingDependent) {
    /*
     * If a node names a never-declared dependent, topSort should fail.
     */
    DependencyGraph graph;
    graph.addNode("A", {}, {"B"});
    auto check = [](const DBException& ex) {
        ASSERT_EQ(ex.code(), ErrorCodes::BadValue) << ex.toString();
        ASSERT_STRING_CONTAINS(ex.reason(), "node B was mentioned but never added");
    };
    ASSERT_THROWS_WITH_CHECK(graph.topSort(), DBException, check);
}

std::vector<std::vector<std::string>> allPermutations(std::vector<std::string> vec,
                                                      size_t first,
                                                      size_t last) {
    std::vector<std::vector<std::string>> out;
    auto i1 = vec.begin() + first;
    auto i2 = vec.begin() + last;
    std::sort(i1, i2);
    do {
        out.push_back(vec);
    } while (std::next_permutation(i1, i2));
    return out;
}

template <typename Expectations, typename F>
void doUntilAllSeen(const Expectations& expected, F&& f) {
    std::vector<int> seen(expected.size(), 0);
    while (std::find(seen.begin(), seen.end(), 0) != seen.end()) {
        auto found = std::find(expected.begin(), expected.end(), f());
        ASSERT_TRUE(found != expected.end());
        ++seen[found - expected.begin()];
    }
}

TEST(DependencyGraphTest, TopSortShufflesNodes) {
    /*
     * Make sure all node orderings can appear as outputs.
     */
    DependencyGraph graph;
    std::vector<std::string> graphNodes;
    for (int i = 0; i < 5; ++i) {
        std::string s = "Node" + std::to_string(i);
        graphNodes.push_back(s);
        graph.addNode(s, {}, {});
    }
    doUntilAllSeen(allPermutations(graphNodes, 0, graphNodes.size()),
                   [&] { return graph.topSort(); });
}

TEST(DependencyGraphTest, TopSortShufflesChildren) {
    /*
     * Make sure all child orderings can appear as outputs.
     */
    DependencyGraph graph;
    std::vector<std::string> graphNodes;
    graphNodes.push_back("Parent");
    graph.addNode("Parent", {}, {});
    for (int i = 0; i < 5; ++i) {
        std::string s = "Child" + std::to_string(i);
        graphNodes.push_back(s);
        graph.addNode(s, {"Parent"}, {});
    }
    // Permute only the children.
    doUntilAllSeen(allPermutations(graphNodes, 1, graphNodes.size()),
                   [&] { return graph.topSort(); });
}

}  // namespace
}  // namespace mongo