Browse Source

pipeline: Directed graph execution model

Our current pipelinelib based jobs require repos to conform to a number
of rigid conventions: assume the repo contains source for only a single
application, build "test" variant, run "test" variant, build
"production" variant, helm deploy/test, publish under a single tag name.
These jobs also assume all of these operations need to be performed
linearly.

While this design was sufficient for our very first use cases, its
convention based design it already proving prohibitively inflexible. For
example, teams maintaining repos that contain multiple interrelated
applications cannot build and test these applications as independent
images; Teams wanting to execute multiple test suites would have to wrap
them in a single entrypoint and implement their own concurrency should
they need it; Etc.

Instead of Release Engineering maintaining a new specialized pipeline
job for each team that performs only slightly different permutations of
the same operations (resulting in duplication of job definitions and a
large maintenance burden), we can instead establish a configuration
format and interface by which teams provide their own pipeline
compositions.

This initial commit in a series of pipeline related commits implements
two fundamental components to support a CI/CD pipeline that can execute
any number of user-defined variant build/test/publish/deploy stages and
steps in a safely concurrent model: a directed-graph based execution
model, and name bindings for stage outputs. The former provides the
model for composing stage execution, and the latter provides a decoupled
system for defining what outputs each subsequent stage operates upon.

First, an `ExecutionGraph` class that can represent a directed acyclic
graph given a number of linearly defined arcs (aka branches/edges). This
component will allow users to provide the overall execution flow as
separate linear processes but allow parallel branches of the execution
graph to be scheduled concurrently.

Example:

    /* To represent a graph with separate parallel branches like:
     *
     *   a       x
     *     ⇘   ⇙
     *       b
     *     ⇙   ⇘
     *   y       c
     *     ⇘   ⇙
     *       z
     *
     * One only needs to provides each linear execution arc
     */
    def graph = new ExecutionGraph([["a", "b", "c", "z"], ["x", "b", "y", "z"]])

    /* The ExecutionGraph can solve how those arcs intersect and how the
     * nodes can be scheduled with a degree of concurrency that Jenkins
     * allows.
     */
    graph.stack() // => [["a", "x"], ["b"], ["y", "c"], ["z"]]

Second, a set of context classes for managing immutable global and local
name/value bindings between nodes in the graph. Effectively this will
provide a way for pipeline stages to safely and deterministically
consume inputs from previous stages along the same branch, and to
provide their own outputs for subsequent stages to consume.

For example, one stage called "build" that builds a container image will
save the image ID in a predetermined local binding called `.imageID` and
a subsequent "publish" stage configured by the user can reference that
image by `${build.imageID}`.

Once a value is bound to a name, that name cannot be reused; bindings
are immutable. Node contexts are only allowed to access namespaces for
nodes that precede them in same branch of the graph, ensuring
deterministic behavior during parallel graph branch execution. See unit
tests for `ExecutionContext` for details on expected behavior.

Put together, these two data structures can constitute an execution
"stack" of sorts that can be safely mapped to Jenkins Pipeline stages,
and make use of parallel execution for graph branches. Specifically, the
`ExecutionGraph.stack()` method is implemented to yield each set of
independent stack "frames" in topological sort order which can safely be
scheduled to run in parallel.

Bug: T210267
Change-Id: Ic5d01bf54c703eaf14434a36f1e2b3e276b48b6f
master
Dan Duvall 2 years ago
committed by Thcipriani
parent
commit
858c26317b
5 changed files with 950 additions and 1 deletions
  1. +25
    -1
      build.gradle
  2. +232
    -0
      src/org/wikimedia/integration/ExecutionContext.groovy
  3. +342
    -0
      src/org/wikimedia/integration/ExecutionGraph.groovy
  4. +173
    -0
      test/org/wikimedia/integration/ExecutionContextTest.groovy
  5. +178
    -0
      test/org/wikimedia/integration/ExecutionGraphTest.groovy

+ 25
- 1
build.gradle View File

@ -1,3 +1,6 @@
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
apply plugin: 'groovy'
repositories {
@ -6,6 +9,7 @@ repositories {
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.11'
compile 'com.cloudbees:groovy-cps:1.24'
testCompile 'junit:junit:4.12'
}
@ -29,7 +33,27 @@ groovydoc {
test {
testLogging {
exceptionFormat = 'full'
exceptionFormat TestExceptionFormat.FULL
events TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.FAILED
info {
events TestLogEvent.STARTED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.FAILED,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STANDARD_ERROR
}
debug.events = info.events
}
// Can be used for remote debugging in IntelliJ
if (System.getProperty('DEBUG', '0') == '1') {
jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9099'
}
}


+ 232
- 0
src/org/wikimedia/integration/ExecutionContext.groovy View File

@ -0,0 +1,232 @@
package org.wikimedia.integration
import com.cloudbees.groovy.cps.NonCPS
import org.codehaus.groovy.GroovyException
import org.wikimedia.integration.ExecutionGraph
/**
* Provides execution context and value bindings to graph nodes. Each node's
* context (see {@link ofNode()}) provides methods for saving its own values
* and bindings for accessing values saved by ancestor nodes.
*
* These contexts can be used during graph stack evaluation to safely pass
* values from ancestor nodes to descendent nodes, and to keep nodes from
* different branches of execution to access each other's values.
*/
class ExecutionContext implements Serializable {
private Map globals = [:]
private ExecutionGraph graph
ExecutionContext(executionGraph) {
graph = executionGraph
}
/**
* Create and return an execution context for the given node.
*/
NodeContext ofNode(node) {
new NodeContext(node, graph.ancestorsOf(node))
}
/**
* Returns the names of all values bound by node contexts.
*/
List getAllKeys() {
def keys = []
for (def ns in globals) {
for (def key in globals[ns]) {
keys.add("${ns}.${key}")
}
}
keys
}
/**
* Provides an execution context for a single given node that can resolve
* bindings for values stored by ancestor nodes, set its own values, and
* safely interpolate user-provided strings.
*
* @example
* Given a graph:
* <pre><code>
* def context = new ExecutionContext(new ExecutionGraph([
* ["a", "b", "c", "d", "e", "f"],
* ["x", "d", "y", "f"],
* ]))
* </code></pre>
*
* @example
* Values can be bound to any existing node.
* <pre><code>
* context.ofNode("a")["foo"] = "afoo"
* context.ofNode("a")["bar"] = "abar"
* context.ofNode("b")["foo"] = "bfoo"
* context.ofNode("x")["bar"] = "xbar"
* </code></pre>
*
* @example
* Those same values can be accessed in contexts of descendent nodes, but
* not in contexts of unrelated nodes.
* <pre><code>
* assert context.ofNode("c").binding("a", "foo") == "afoo"
* assert context.ofNode("c").binding("a", "bar") == "abar"
* assert context.ofNode("c").binding("b", "foo") == "bfoo"
*
* assert context.ofNode("c").binding("x", "bar") == null
*
* assert context.ofNode("e").binding("a", "foo") == "afoo"
* assert context.ofNode("e").binding("a", "bar") == "abar"
* assert context.ofNode("e").binding("b", "foo") == "bfoo"
* assert context.ofNode("e").binding("x", "bar") == "xbar"
* </code></pre>
*
* @example
* Leveraging all of the above, user-provided configuration can be safely
* interpolated.
* <pre><code>
* assert (context.ofNode("c") % 'w-t-${a.foo} ${b.foo}') == "w-t-afoo bfoo"
* assert (context.ofNode("c") % 'w-t-${a.bar}') == "w-t-abar"
* assert (context.ofNode("x") % 'w-t-${x.bar}') == 'w-t-${x.bar}'
* </code></pre>
*/
class NodeContext implements Serializable {
final VAR_EXPRESSION = /\$\{(\w*\.\w+)\}/
final def node
final Set ancestors
NodeContext(contextNode, contextAncestors) {
globals[contextNode] = globals[contextNode] ?: [:]
node = contextNode
ancestors = contextAncestors
}
/**
* Binds a value to a name in the globals store under the node's
* namespace. The value may later be retrieved by any descendent node's
* context using {@link binding()}.
*/
@NonCPS
void bind(String key, value)
throws NameAlreadyBoundException {
if (globals[node].containsKey(key)) {
throw new NameAlreadyBoundException(key: key)
}
globals[node][key] = value
}
/**
* Retrieves a value previously bound using {@link bind()} to this node's
* context. If the given key is not found under this node's namespace, a
* {@link NameNotFoundException} is thrown.
*/
def binding(String key)
throws NameNotFoundException {
if (!globals[node].containsKey(key)) {
throw new NameNotFoundException(ns: node, key: key)
}
globals[node][key]
}
/**
* Retrieves a value previously bound using {@link bind()} under the given
* ancestor node's namespace and name.
*/
def binding(ancestorNode, String key)
throws NameNotFoundException, AncestorNotFoundException {
if (!(ancestorNode in ancestors)) {
throw new AncestorNotFoundException(ancestor: ancestorNode, node: node)
}
if (!globals[ancestorNode].containsKey(key)) {
throw new NameNotFoundException(ns: ancestorNode, key: key)
}
globals[ancestorNode][key]
}
/**
* Returns all objects bound to the given name under any node namespace.
* This should only be used at the end of an execution graph.
*/
List getAll(String key) {
globals.findAll { it.value[key] != null }.collect { it.value[key] }
}
/**
* Operator alias for {@link binding(String)} or, if a "namespace.key" is
* given, {@link binding(def, String)}.
*/
def getAt(String key) {
def keys = key.split(/\./)
if (keys.size() > 1) {
if (keys[0] == "") {
return binding(keys[1])
}
return binding(keys[0], keys[1])
}
return binding(key)
}
/**
* Interpolates the given string by substituting all symbol expressions
* with values previously bound by ancestor nodes.
*/
@NonCPS
String interpolate(String str) {
str.replaceAll(VAR_EXPRESSION) { _, key ->
this[key]
}
}
/**
* Operator alias for {@link interpolate()}.
*/
String mod(String str) {
interpolate(str)
}
/**
* Operator alias for {@link bind()}.
*/
void putAt(String key, value) {
bind(key, value)
}
}
class AncestorNotFoundException extends GroovyException {
def ancestor, node
String getMessage() {
"cannot access '${ancestor}.*' values since '${node}' does not follow it in the graph '${graph}'"
}
}
class NameNotFoundException extends GroovyException {
def ns, key
String getMessage() {
"no value bound for '${ns}.${key}'; all bound names are: ${getAllKeys().join(", ")}"
}
}
class NameAlreadyBoundException extends GroovyException {
def key
String getMessage() {
"'${node}' already has a value assigned to '${key}'"
}
}
}

+ 342
- 0
src/org/wikimedia/integration/ExecutionGraph.groovy View File

@ -0,0 +1,342 @@
package org.wikimedia.integration
import com.cloudbees.groovy.cps.NonCPS
/**
* Represents a directed acyclic graph (DAG) for defining pipeline stage
* dependencies and scheduling them in a parallel topological-sort order.
*
* An {@link ExecutionGraph} is constructed by passing sets of arcs (aka
* edges/branches) that may or may not intersect on common nodes.
*
* @example
* A graph such as:
* <pre><code>
* a w z
*
* b x
*
* c y
*
* d
* </code></pre>
*
* Can be constructed any number of ways as long as all the arcs are
* represented in the given sets.
*
* <pre><code>
* def graph = ExecutionGraph([
* ["a", "b", "c", "d"], // defines edges a b, b c, c d
* ["w", "b", "y"], // defines edges w b, b y
* ["x", "y", "d"], // defines edges x y, y d
* ["z"], // defines no edge but an isolate node
* ])
* </code></pre>
*
* @example
* The same graph could be constructed this way.
*
* <pre><code>
* def graph = ExecutionGraph([
* ["a", "b", "y"],
* ["w", "b", "c", "d"],
* ["x", "y"],
* ["z"],
* ])
* </code></pre>
*
* @example
* {@link ExecutionGraph#stack()} will return concurrent "frames" of the graph
* in a topological sort order, meaning that nodes are always traversed before
* any of their successor nodes, and nodes of independent branches can be
* scheduled in parallel.
*
* For the same example graph:
*
* <pre><code>
* graph.stack().each { println it.join("|") }
* </code></pre>
*
* Would output:
*
* <pre>
* a|w|z
* b|x
* c|y
* d
* </pre>
*/
class ExecutionGraph implements Serializable {
/**
* Map of graph progression, nodes and their successor (out) nodes.
*
* @example
* An example graph and its <code>progression</code>.
*
* <pre><code>
* a w z [
* a:[b], w:[b],
* b x
* b:[c, y], x:[y],
* c y
* c:[d], y:[d],
* d ]
* </code></pre>
*/
protected Map progression
/**
* Map of graph recession, nodes and their predecessor (in) nodes. Allows
* for efficient backward traversal.
*
* @example
* An example graph and its <code>recession</code>.
*
* <pre><code>
* a w z [
* b:[a, w],
* b x
* c:[b], y:[b, x],
* c y
* d:[c, y],
* d ]
* </code></pre>
*/
protected Map recession
/**
* Set of graph isolates, nodes that are unconnected from all other nodes.
*/
protected Set isolates
/**
* Constructs a directed execution graph using the given sets of edge
* sequences (arcs).
*
* @example
* See {@link ExecutionGraph} for examples.
*/
ExecutionGraph(List arcs) {
progression = [:]
recession = [:]
isolates = [] as Set
arcs.each { addArc(it as List) }
}
/**
* All ancestors of (nodes eventually leading to) the given node.
*/
Set ancestorsOf(node) {
def parents = inTo(node)
parents + parents.inject([] as Set) { ancestors, parent -> ancestors + ancestorsOf(parent) }
}
/**
* Whether the given graph is equal to this one.
*/
boolean equals(ExecutionGraph other) {
progression == other.progression && isolates == other.isolates
}
/**
* Returns all nodes that have no outgoing edges.
*/
Set leaves() {
(recession.keySet() - progression.keySet()) + isolates
}
/**
* The number of nodes that lead directly to the given one.
*/
int inDegreeOf(node) {
inTo(node).size()
}
/**
* The nodes that lead directly to the given one.
*/
Set inTo(node) {
recession[node] ?: [] as Set
}
/**
* All nodes in the graph.
*/
Set nodes() {
progression.keySet() + recession.keySet() + isolates
}
/**
* Returns a union of this graph and the given one.
*/
ExecutionGraph or(ExecutionGraph other) {
def newGraph = new ExecutionGraph()
[this, other].each { source ->
source.progression.each { newGraph.addSuccession(it.key, it.value) }
source.isolates.each { newGraph.addIsolate(it) }
}
newGraph
}
/**
* The number of nodes the given one directly leads to.
*/
int outDegreeOf(node) {
outOf(node).size()
}
/**
* The nodes the given one directly leads to.
*/
Set outOf(node) {
progression[node] ?: [] as Set
}
/**
* Returns a concatenation of this graph and the given one.
*/
ExecutionGraph plus(ExecutionGraph other) {
def newGraph = this | other
leaves().each { leaf ->
newGraph.addSuccession(leaf, other.roots())
}
newGraph
}
/**
* Returns all nodes that have no incoming edges.
*/
Set roots() {
(progression.keySet() - recession.keySet()) + isolates
}
/**
* Returns each concurrent node "frames" of the graph in a topological sort
* order. See {@link ExecutionGraph} for examples. A {@link RuntimeException}
* will be thrown in the event a graph cycle is detected.
*/
List stack() throws RuntimeException {
def concurrentFrames = []
def graphSize = (progression.keySet() + recession.keySet() + isolates).size()
def traversed = [] as Set
def prevNodes
while (traversed.size() < graphSize) {
def nextNodes
if (!prevNodes) {
nextNodes = roots()
} else {
nextNodes = [] as Set
prevNodes.each { prev ->
outOf(prev).each { outNode ->
if ((inTo(outNode) - traversed).isEmpty()) {
nextNodes.add(outNode)
}
}
}
}
if (!nextNodes && traversed.size() < graphSize) {
throw new RuntimeException("cycle detected in graph (${this})")
}
traversed.addAll(nextNodes)
prevNodes = nextNodes
concurrentFrames.add(nextNodes as List)
}
concurrentFrames
}
/**
* A string representation of the graph compatible with <code>dot</code>.
*
* @example
* Render the graph with dot
*
* <pre><code>
* $ echo "[graph.toString() value]" | dot -Tsvg &gt; graph.svg
* </code></pre>
*/
String toString() {
def allEdges = progression.inject([]) { edges, predecessor, successors ->
edges + successors.collect { successor ->
"${predecessor} -> ${successor}"
}
}
'digraph { ' + (allEdges + isolates).join("; ") + ' }'
}
protected
/**
* Appends a new arc of nodes to the graph.
*
* @example
* An existing graph.
* <pre><code>
* a
*
* b
*
* c
* </code></pre>
*
* Appended with <code>graph &lt;&lt; ["x", "b", "y", "z"]</code> becomes.
* <pre><code>
* a x
*
* b
*
* y c
*
* z
* </code></pre>
*/
@NonCPS
void addArc(List arc) {
if (arc.size() == 1) {
addIsolate(arc[0])
} else {
arc.eachWithIndex { node, i ->
if (i < (arc.size() - 1)) {
addSuccession(node, [arc[i+1]])
}
}
}
}
@NonCPS
void addIsolate(isolate) {
isolates.add(isolate)
}
@NonCPS
void addSuccession(predecessor, successors) {
if (!progression.containsKey(predecessor)) {
progression[predecessor] = [] as Set
}
progression[predecessor].addAll(successors)
successors.each { successor ->
if (!recession.containsKey(successor)) {
recession[successor] = [] as Set
}
recession[successor].add(predecessor)
}
isolates -= (successors + predecessor)
}
}

+ 173
- 0
test/org/wikimedia/integration/ExecutionContextTest.groovy View File

@ -0,0 +1,173 @@
import groovy.mock.interceptor.MockFor
import static groovy.test.GroovyAssert.*
import groovy.util.GroovyTestCase
import org.wikimedia.integration.ExecutionGraph
import org.wikimedia.integration.ExecutionContext
class ExecutionContextTest extends GroovyTestCase {
void testNodeContextBindings() {
/*
* a
*
* b
*
* c x
*
* d
*
* y e
*
* f
*/
def context = new ExecutionContext(new ExecutionGraph([
["a", "b", "c", "d", "e", "f"],
["x", "d", "y", "f"],
]))
def cContext = context.ofNode("c")
def eContext = context.ofNode("e")
context.ofNode("a").bind("foo", "afoo")
context.ofNode("a").bind("bar", "abar")
context.ofNode("b").bind("foo", "bfoo")
context.ofNode("x").bind("bar", "xbar")
assert cContext.binding("a", "foo") == "afoo"
assert cContext.binding("a", "bar") == "abar"
assert cContext.binding("b", "foo") == "bfoo"
shouldFail(ExecutionContext.AncestorNotFoundException) {
cContext.binding("x", "bar")
}
shouldFail(ExecutionContext.NameNotFoundException) {
cContext.binding("baz")
}
assert eContext.binding("a", "foo") == "afoo"
assert eContext.binding("a", "bar") == "abar"
assert eContext.binding("b", "foo") == "bfoo"
assert eContext.binding("x", "bar") == "xbar"
}
void testNodeContextBindings_immutable() {
/*
* a
*
* b
*
* c
*/
def context = new ExecutionContext(new ExecutionGraph([
["a", "b", "c"],
]))
context.ofNode("b").bind("foo", "bfoo")
shouldFail(ExecutionContext.NameAlreadyBoundException) {
context.ofNode("b").bind("foo", "newfoo")
}
assert context.ofNode("b").binding("foo") == "bfoo"
}
void testStringInterpolation() {
/*
* a
*
* b
*
* c x
*
* d
*
* y e
*
* f
*/
def context = new ExecutionContext(new ExecutionGraph([
["a", "b", "c", "d", "e", "f"],
["x", "d", "y", "f"],
]))
context.ofNode("a").bind("foo", "afoo")
context.ofNode("a").bind("bar", "abar")
context.ofNode("b").bind("foo", "bfoo")
context.ofNode("x").bind("bar", "xbar")
assert (context.ofNode("b") % 'w-t-${a.foo} ${.foo}') == "w-t-afoo bfoo"
assert (context.ofNode("c") % 'w-t-${a.foo} ${b.foo}') == "w-t-afoo bfoo"
assert (context.ofNode("c") % 'w-t-${a.bar}') == "w-t-abar"
shouldFail(ExecutionContext.AncestorNotFoundException) {
context.ofNode("x") % 'w-t-${b.bar}'
}
shouldFail(ExecutionContext.NameNotFoundException) {
context.ofNode("b") % 'w-t-${.bar}'
}
}
void testStringInterpolation_selfScope() {
/*
* a
*
* b
*
* c
*/
def context = new ExecutionContext(new ExecutionGraph([
["a", "b", "c"],
]))
context.ofNode("c").bind("foo", "cfoo")
assert (context.ofNode("c") % 'w-t-${.foo}') == "w-t-cfoo"
}
void testNodeContextGetAll() {
def context = new ExecutionContext(new ExecutionGraph([
["a", "b", "c", "z"],
["x", "b", "y", "z"],
]))
context.ofNode("a").bind("foo", "afoo")
context.ofNode("c").bind("foo", "cfoo")
context.ofNode("y").bind("foo", "yfoo")
assert context.ofNode("z").getAll("foo") == ["afoo", "cfoo", "yfoo"]
}
void testNodeContextGetAt() {
def context = new ExecutionContext(new ExecutionGraph([
["a", "b", "c"],
]))
context.ofNode("a").bind("foo", "afoo")
context.ofNode("c").bind("foo", "cfoo")
assert context.ofNode("c")["a.foo"] == "afoo"
assert context.ofNode("c")["foo"] == "cfoo"
}
void testNodeContextGetAt_selfScope() {
/*
* a
*
* b
*
* c
*/
def context = new ExecutionContext(new ExecutionGraph([
["a", "b", "c"],
]))
def bContext = context.ofNode("b")
bContext["foo"] = "bfoo"
assert bContext[".foo"] == "bfoo"
}
}

+ 178
- 0
test/org/wikimedia/integration/ExecutionGraphTest.groovy View File

@ -0,0 +1,178 @@
import groovy.mock.interceptor.MockFor
import static groovy.test.GroovyAssert.*
import groovy.util.GroovyTestCase
import org.wikimedia.integration.ExecutionGraph
class ExecutionGraphTest extends GroovyTestCase {
void testAncestorsOf() {
def graph = new ExecutionGraph([
["a", "b", "c", "d", "e", "f"],
["x", "d", "y", "f"],
])
assert graph.ancestorsOf("c") == ["b", "a"] as Set
assert graph.ancestorsOf("d") == ["c", "b", "a", "x"] as Set
assert graph.ancestorsOf("f") == ["e", "d", "c", "b", "a", "x", "y"] as Set
}
void testLeaves() {
def graph = new ExecutionGraph([
["a", "b", "c", "d"],
["f", "b", "g"],
])
assert graph.leaves() == ["g", "d"] as Set
}
void testInDegreeOf() {
def graph = new ExecutionGraph([
["a", "b", "c"],
["d", "b", "e"],
["f", "b"],
])
assert graph.inDegreeOf("a") == 0
assert graph.inDegreeOf("d") == 0
assert graph.inDegreeOf("f") == 0
assert graph.inDegreeOf("b") == 3
assert graph.inDegreeOf("e") == 1
assert graph.inDegreeOf("c") == 1
}
void testInTo() {
def graph = new ExecutionGraph([
["a", "b", "c"],
["d", "b", "e"],
["f", "b"],
])
assert graph.inTo("a") == [] as Set
assert graph.inTo("d") == [] as Set
assert graph.inTo("f") == [] as Set
assert graph.inTo("b") == ["a", "d", "f"] as Set
assert graph.inTo("c") == ["b"] as Set
assert graph.inTo("e") == ["b"] as Set
}
void testNodes() {
def graph = new ExecutionGraph([
["a", "b", "c"],
["x", "b", "y"],
["z"],
])
assert graph.nodes() == ["a", "b", "c", "x", "y", "z"] as Set
}
void testOr() {
def graph1 = new ExecutionGraph([
["x", "y", "z"],
])
def graph2 = new ExecutionGraph([
["a", "b", "c"],
["d", "b", "e"],
])
assert (graph1 | graph2) == new ExecutionGraph([
["x", "y", "z"],
["a", "b", "c"],
["d", "b", "e"],
])
}
void testOutDegreeOf() {
def graph = new ExecutionGraph([
["a", "b", "c"],
["d", "b", "e"],
["f", "b"],
])
assert graph.outDegreeOf("a") == 1
assert graph.outDegreeOf("d") == 1
assert graph.outDegreeOf("f") == 1
assert graph.outDegreeOf("b") == 2
assert graph.outDegreeOf("e") == 0
assert graph.outDegreeOf("c") == 0
}
void testOutOf() {
def graph = new ExecutionGraph([
["a", "b", "c"],
["d", "b", "e"],
["f", "b"],
])
assert graph.outOf("a") == ["b"] as Set
assert graph.outOf("d") == ["b"] as Set
assert graph.outOf("f") == ["b"] as Set
assert graph.outOf("b") == ["c", "e"] as Set
assert graph.outOf("c") == [] as Set
assert graph.outOf("e") == [] as Set
}
void testPlus() {
def graph1 = new ExecutionGraph([
["x", "y", "z"],
])
def graph2 = new ExecutionGraph([
["a", "b", "c"],
["d", "b", "e"],
])
assert (graph1 + graph2) == new ExecutionGraph([
["x", "y", "z", "a", "b", "c"],
["x", "y", "z", "d", "b", "e"],
])
}
void testRoots() {
def graph = new ExecutionGraph([
["a", "b", "c", "d", "e"],
["f", "b", "g", "e"],
])
assert graph.roots() == ["a", "f"] as Set
}
void testRootsOfIsolates() {
def graph = new ExecutionGraph([["a"], ["z"]])
assert graph.roots() == ["a", "z"] as Set
}
void testStack() {
def graph = new ExecutionGraph([
["a", "b", "c", "d", "e", "f"],
["x", "d", "y", "f"],
])
assert graph.stack() == [
["a", "x"],
["b"],
["c"],
["d"],
["e", "y"],
["f"],
]
}
void testStack_cycleDetection() {
def graph = new ExecutionGraph([
["a", "b", "c"],
["x", "b", "y", "a"],
])
shouldFail(Exception) {
graph.stack()
}
}
void testToString() {
def graph = new ExecutionGraph([["a", "b", "c"], ["x"]])
assert graph.toString() == "digraph { a -> b; b -> c; x }"
}
}

Loading…
Cancel
Save