2022-09-05 13:29:11 +08:00
|
|
|
package rootcoord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-09-21 15:46:51 +08:00
|
|
|
func newTestUndoTask() *baseUndoTask {
|
|
|
|
stepExecutor := newMockStepExecutor()
|
|
|
|
stepExecutor.AddStepsFunc = func(s *stepStack) {
|
|
|
|
// no schedule, execute directly.
|
|
|
|
s.Execute(context.Background())
|
|
|
|
}
|
|
|
|
undoTask := newBaseUndoTask(stepExecutor)
|
|
|
|
return undoTask
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:29:11 +08:00
|
|
|
func Test_baseUndoTask_Execute(t *testing.T) {
|
|
|
|
t.Run("should not happen", func(t *testing.T) {
|
2022-09-21 15:46:51 +08:00
|
|
|
undoTask := newTestUndoTask()
|
2022-09-05 13:29:11 +08:00
|
|
|
undoTask.todoStep = append(undoTask.todoStep, newMockNormalStep())
|
|
|
|
err := undoTask.Execute(context.Background())
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("normal case, no undo step will be called", func(t *testing.T) {
|
2022-09-21 15:46:51 +08:00
|
|
|
undoTask := newTestUndoTask()
|
2022-09-05 13:29:11 +08:00
|
|
|
n := 10
|
2022-09-21 15:46:51 +08:00
|
|
|
todoSteps, undoSteps := make([]nestedStep, 0, n), make([]nestedStep, 0, n)
|
2022-09-05 13:29:11 +08:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
normalTodoStep := newMockNormalStep()
|
|
|
|
normalUndoStep := newMockNormalStep()
|
|
|
|
todoSteps = append(todoSteps, normalTodoStep)
|
|
|
|
undoSteps = append(undoSteps, normalUndoStep)
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
undoTask.AddStep(todoSteps[i], undoSteps[i])
|
|
|
|
}
|
|
|
|
err := undoTask.Execute(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// make sure no undo steps will be called.
|
|
|
|
for _, step := range undoSteps {
|
|
|
|
assert.False(t, step.(*mockNormalStep).called)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("partial error, undo from last finished", func(t *testing.T) {
|
2022-09-21 15:46:51 +08:00
|
|
|
undoTask := newTestUndoTask()
|
|
|
|
todoSteps := []nestedStep{
|
2022-09-05 13:29:11 +08:00
|
|
|
newMockNormalStep(),
|
|
|
|
newMockFailStep(),
|
|
|
|
newMockNormalStep(),
|
|
|
|
}
|
2022-09-21 15:46:51 +08:00
|
|
|
undoSteps := []nestedStep{
|
2022-09-05 13:29:11 +08:00
|
|
|
newMockNormalStep(),
|
|
|
|
newMockNormalStep(),
|
|
|
|
newMockNormalStep(),
|
|
|
|
}
|
|
|
|
l := len(todoSteps)
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
undoTask.AddStep(todoSteps[i], undoSteps[i])
|
|
|
|
}
|
|
|
|
err := undoTask.Execute(context.Background())
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, todoSteps[0].(*mockNormalStep).called)
|
|
|
|
assert.True(t, todoSteps[1].(*mockFailStep).called)
|
|
|
|
assert.False(t, todoSteps[2].(*mockNormalStep).called)
|
|
|
|
|
|
|
|
<-undoSteps[0].(*mockNormalStep).calledChan
|
|
|
|
assert.True(t, undoSteps[0].(*mockNormalStep).called)
|
|
|
|
assert.False(t, undoSteps[1].(*mockNormalStep).called)
|
|
|
|
assert.False(t, undoSteps[2].(*mockNormalStep).called)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("partial error, undo meet error also", func(t *testing.T) {
|
2022-09-21 15:46:51 +08:00
|
|
|
undoTask := newTestUndoTask()
|
|
|
|
todoSteps := []nestedStep{
|
2022-09-05 13:29:11 +08:00
|
|
|
newMockNormalStep(),
|
|
|
|
newMockNormalStep(),
|
|
|
|
newMockFailStep(),
|
|
|
|
}
|
2022-09-21 15:46:51 +08:00
|
|
|
undoSteps := []nestedStep{
|
2022-09-05 13:29:11 +08:00
|
|
|
newMockNormalStep(),
|
|
|
|
newMockFailStep(),
|
|
|
|
newMockNormalStep(),
|
|
|
|
}
|
|
|
|
l := len(todoSteps)
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
undoTask.AddStep(todoSteps[i], undoSteps[i])
|
|
|
|
}
|
|
|
|
err := undoTask.Execute(context.Background())
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, todoSteps[0].(*mockNormalStep).called)
|
|
|
|
assert.True(t, todoSteps[1].(*mockNormalStep).called)
|
|
|
|
assert.True(t, todoSteps[2].(*mockFailStep).called)
|
|
|
|
assert.False(t, undoSteps[0].(*mockNormalStep).called)
|
|
|
|
<-undoSteps[1].(*mockFailStep).calledChan
|
|
|
|
assert.True(t, undoSteps[1].(*mockFailStep).called)
|
|
|
|
assert.False(t, undoSteps[2].(*mockNormalStep).called)
|
|
|
|
})
|
|
|
|
}
|