Skip to content

Commit

Permalink
fix: merge dags lost the dependencies of sub-dags (#5403)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf authored Oct 12, 2023
1 parent f43413a commit a428da7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 6 additions & 2 deletions internal/controller/graph/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"errors"
"fmt"
"sort"

"golang.org/x/exp/maps"
)

type DAG struct {
Expand Down Expand Up @@ -260,10 +262,12 @@ func (d *DAG) Root() Vertex {

func (d *DAG) Merge(subDag *DAG) {
for v := range subDag.vertices {
if len(d.inAdj(v)) == 0 {
d.AddConnectRoot(v)
if len(subDag.inAdj(v)) == 0 {
d.Connect(d.Root(), v)
}
}
maps.Copy(d.vertices, subDag.vertices)
maps.Copy(d.edges, subDag.edges)
}

// String returns a string representation of the DAG in topology order
Expand Down
21 changes: 19 additions & 2 deletions internal/controller/graph/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,20 +306,37 @@ func TestEquals(t *testing.T) {
func TestMerge(t *testing.T) {
dag1 := NewDAG()
dag2 := NewDAG()
v1, v2, v3 := 1, 2, 3
v1, v2, v3, v4, v5, v6 := 1, 2, 3, 4, 5, 6
dag1.AddVertex(v1)
dag1.AddVertex(v2)
dag1.Connect(v1, v2)
dag2.AddVertex(v2)
dag2.AddVertex(v3)
dag2.AddVertex(v4)
dag2.AddVertex(v5)
dag2.AddVertex(v6)
dag2.Connect(v2, v3)
dag2.Connect(v2, v4)
dag2.Connect(v3, v5)
dag2.Connect(v4, v5)
dag2.Connect(v6, v4)
dag2.Connect(v6, v5)

dagExpected := NewDAG()
dagExpected.AddVertex(v1)
dagExpected.AddVertex(v2)
dagExpected.AddVertex(v3)
dagExpected.AddVertex(v4)
dagExpected.AddVertex(v5)
dagExpected.AddVertex(v6)
dagExpected.Connect(v1, v2)
dagExpected.Connect(v1, v3)
dagExpected.Connect(v1, v6)
dagExpected.Connect(v2, v3)
dagExpected.Connect(v2, v4)
dagExpected.Connect(v3, v5)
dagExpected.Connect(v4, v5)
dagExpected.Connect(v6, v4)
dagExpected.Connect(v6, v5)

dag1.Merge(dag2)
if !dag1.Equals(dagExpected, less) {
Expand Down

0 comments on commit a428da7

Please sign in to comment.