Skip to content

Commit 9c19691

Browse files
fix: report error if end node has been skipped (cloudwego#411)
1 parent efa9b18 commit 9c19691

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

compose/graph_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ func (c *channelManager) reportBranch(from string, skippedNodes []string) error
231231

232232
for i := 0; i < len(nKeys); i++ {
233233
key := nKeys[i]
234+
235+
if key == END {
236+
continue
237+
}
234238
if _, ok := c.successors[key]; !ok {
235239
return fmt.Errorf("unknown node: %s", key)
236240
}

compose/graph_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,3 +1974,43 @@ func TestPrintTasks(t *testing.T) {
19741974
ts = []*task{{nodeKey: "1"}, {nodeKey: "2"}, {nodeKey: "3"}}
19751975
assert.Equal(t, "[1, 2, 3]", printTask(ts))
19761976
}
1977+
1978+
func TestSkipBranch(t *testing.T) {
1979+
g := NewGraph[string, string]()
1980+
_ = g.AddLambdaNode("1", InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
1981+
return input, nil
1982+
}))
1983+
_ = g.AddLambdaNode("2", InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
1984+
return input, nil
1985+
}))
1986+
_ = g.AddEdge(START, "1")
1987+
_ = g.AddBranch("1", NewGraphMultiBranch(func(ctx context.Context, in string) (endNode map[string]bool, err error) {
1988+
return map[string]bool{}, nil
1989+
}, map[string]bool{"2": true}))
1990+
_ = g.AddEdge("2", END)
1991+
1992+
ctx := context.Background()
1993+
r, err := g.Compile(ctx, WithNodeTriggerMode(AllPredecessor))
1994+
assert.NoError(t, err)
1995+
_, err = r.Invoke(ctx, "input")
1996+
assert.ErrorContains(t, err, "[GraphRunError] no tasks to execute, last completed nodes: [1]")
1997+
1998+
g = NewGraph[string, string]()
1999+
_ = g.AddLambdaNode("1", InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
2000+
return input, nil
2001+
}))
2002+
_ = g.AddLambdaNode("2", InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
2003+
return input, nil
2004+
}))
2005+
_ = g.AddEdge(START, "1")
2006+
_ = g.AddBranch("1", NewGraphMultiBranch(func(ctx context.Context, in string) (endNode map[string]bool, err error) {
2007+
return map[string]bool{}, nil
2008+
}, map[string]bool{"2": true}))
2009+
_ = g.AddEdge("2", END)
2010+
_ = g.AddEdge(START, "2")
2011+
r, err = g.Compile(ctx, WithNodeTriggerMode(AllPredecessor))
2012+
assert.NoError(t, err)
2013+
result, err := r.Invoke(ctx, "input")
2014+
assert.NoError(t, err)
2015+
assert.Equal(t, "input", result)
2016+
}

0 commit comments

Comments
 (0)