Skip to content

Commit db2a424

Browse files
authored
optimize(api): make condition task precede more user-friendly #103
Optimize ConditionTask.Precede by replacing i with len(cond.mapper) to resolve abnormal flow execution results caused by multiple calls to precede (#103)
1 parent f8e1652 commit db2a424

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

task.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ type Task struct {
99
// In Addition, order of tasks is correspond to predict result, ranging from 0...len(tasks)
1010
func (t *Task) Precede(tasks ...*Task) {
1111
if cond, ok := t.node.ptr.(*Condition); ok {
12-
for i, task := range tasks {
13-
cond.mapper[uint(i)] = task.node
12+
for _, task := range tasks {
13+
index := len(cond.mapper)
14+
cond.mapper[uint(index)] = task.node
1415
}
1516
}
1617

taskflow_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,58 @@ func TestTaskflowCondition(t *testing.T) {
380380

381381
})
382382

383+
t.Run("normal-1", func(t *testing.T) {
384+
tf := gotaskflow.NewTaskFlow("G")
385+
386+
A, B, C :=
387+
tf.NewTask("A", func() {
388+
fmt.Println("A")
389+
q.Put("A")
390+
}),
391+
tf.NewTask("B", func() {
392+
fmt.Println("B")
393+
q.Put("B")
394+
}),
395+
tf.NewTask("C", func() {
396+
fmt.Println("C")
397+
q.Put("C")
398+
})
399+
A.Precede(B)
400+
C.Precede(B)
401+
402+
fail, success := tf.NewTask("failed", func() {
403+
fmt.Println("Failed")
404+
q.Put("failed")
405+
t.Fail()
406+
}), tf.NewTask("success", func() {
407+
fmt.Println("success")
408+
q.Put("success")
409+
})
410+
411+
cond := tf.NewCondition("cond", func() uint {
412+
q.Put("cond")
413+
return 0
414+
})
415+
B.Precede(cond)
416+
cond.Precede(success)
417+
cond.Precede(fail)
418+
419+
// success.Precede(suc)
420+
if err := tf.Dump(os.Stdout); err != nil {
421+
fmt.Errorf("%v", err)
422+
}
423+
executor.Run(tf).Wait()
424+
425+
executor.Profile(os.Stdout)
426+
chain.grouping("A", "C")
427+
chain.grouping("B")
428+
chain.grouping("cond")
429+
chain.grouping("success")
430+
431+
checkTopology(t, q, chain)
432+
433+
})
434+
383435
t.Run("start with condition node", func(t *testing.T) {
384436
i := 0
385437
tf := gotaskflow.NewTaskFlow("G")

0 commit comments

Comments
 (0)