Skip to content

Commit adf3910

Browse files
committed
Extract DAG creation out, address review comments
1 parent db79ebf commit adf3910

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

pkg/epp/requestcontrol/dag.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,23 @@ func buildDAG(plugins []PrepareDataPlugin) map[string][]string {
5353
return dag
5454
}
5555

56-
// prepareDataGraph builds the dependency graph and returns the plugins ordered in topological order.
56+
// sortPlugins builds the dependency graph and returns the plugins ordered in topological order.
5757
// If there is a cycle, it returns an error.
58-
func prepareDataGraph(plugins []PrepareDataPlugin) (map[string][]string, []PrepareDataPlugin, error) {
59-
dag := buildDAG(plugins)
58+
func sortPlugins(dag map[string][]string, plugins []PrepareDataPlugin) ([]PrepareDataPlugin, error) {
6059
nameToPlugin := map[string]PrepareDataPlugin{}
6160
for _, plugin := range plugins {
6261
nameToPlugin[plugin.TypedName().String()] = plugin
6362
}
6463
sortedPlugins, err := topologicalSort(dag)
6564
if err != nil {
66-
return nil, nil, err
65+
return nil, err
6766
}
6867
orderedPlugins := []PrepareDataPlugin{}
6968
for _, pluginName := range sortedPlugins {
7069
orderedPlugins = append(orderedPlugins, nameToPlugin[pluginName])
7170
}
7271

73-
return dag, orderedPlugins, err
72+
return orderedPlugins, err
7473
}
7574

7675
// TopologicalSort performs Kahn's Algorithm on a DAG.

pkg/epp/requestcontrol/dag_test.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,33 @@ func TestPrepareDataGraph(t *testing.T) {
113113

114114
for _, tc := range testCases {
115115
t.Run(tc.name, func(t *testing.T) {
116-
dag, orderedPlugins, err := prepareDataGraph(tc.plugins)
116+
dag := buildDAG(tc.plugins)
117+
orderedPlugins, err := sortPlugins(dag, tc.plugins)
117118

118119
if tc.expectError {
119120
assert.Error(t, err)
120-
assert.Nil(t, dag)
121121
assert.Contains(t, err.Error(), "cycle detected")
122-
} else {
123-
assert.NoError(t, err)
124-
125-
// Normalize the slices in the maps for consistent comparison
126-
normalizedDAG := make(map[string][]string)
127-
maps.Copy(normalizedDAG, dag)
128-
normalizedExpectedDAG := make(map[string][]string)
129-
for k, v := range tc.expectedDAG {
130-
normalizedExpectedDAG[k] = v
131-
}
132-
133-
if diff := cmp.Diff(normalizedExpectedDAG, normalizedDAG); diff != "" {
134-
t.Errorf("prepareDataGraph() mismatch (-want +got):\n%s", diff)
135-
}
136-
137-
orderedPluginNames := make([]string, len(orderedPlugins))
138-
for i, p := range orderedPlugins {
139-
orderedPluginNames[i] = p.TypedName().String()
140-
}
141-
assertTopologicalOrder(t, dag, orderedPlugins)
122+
return
142123
}
124+
assert.NoError(t, err)
125+
126+
// Normalize the slices in the maps for consistent comparison
127+
normalizedDAG := make(map[string][]string)
128+
maps.Copy(normalizedDAG, dag)
129+
normalizedExpectedDAG := make(map[string][]string)
130+
for k, v := range tc.expectedDAG {
131+
normalizedExpectedDAG[k] = v
132+
}
133+
134+
if diff := cmp.Diff(normalizedExpectedDAG, normalizedDAG); diff != "" {
135+
t.Errorf("prepareDataGraph() mismatch (-want +got):\n%s", diff)
136+
}
137+
138+
orderedPluginNames := make([]string, len(orderedPlugins))
139+
for i, p := range orderedPlugins {
140+
orderedPluginNames[i] = p.TypedName().String()
141+
}
142+
assertTopologicalOrder(t, dag, orderedPlugins)
143143
})
144144
}
145145
}

pkg/epp/requestcontrol/request_control_config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ func (c *Config) AddPlugins(pluginObjects ...plugins.Plugin) {
108108
// PrepareDataPluginGraph creates data dependency graph and sorts the plugins in topological order.
109109
// If a cycle is detected, it returns an error.
110110
func (c *Config) PrepareDataPluginGraph() error {
111-
_, plugins, err := prepareDataGraph(c.prepareDataPlugins)
111+
dag := buildDAG(c.prepareDataPlugins)
112+
plugins, err := sortPlugins(dag, c.prepareDataPlugins)
112113
if err != nil {
113114
return err
114115
}

0 commit comments

Comments
 (0)