fix: align business reports and record task migration progress
This commit is contained in:
@@ -24,6 +24,52 @@ func TestPrivateIP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMethodExecutionPersistsProgressAndRetryOutcome(t *testing.T) {
|
||||
registry := platformtask.NewRegistry()
|
||||
attempts := 0
|
||||
registry.Register(platformtask.Method{Name: "report", Run: func(ctx context.Context, _ json.RawMessage) error {
|
||||
attempts++
|
||||
if attempts == 1 {
|
||||
return errors.New("temporary storage failure")
|
||||
}
|
||||
platformtask.Logf(ctx, "日期=2026-11-05;已提交 12 行")
|
||||
return nil
|
||||
}})
|
||||
repo := &workerTaskRepo{logs: make(chan *taskbiz.TimedTaskLog, 1)}
|
||||
executor := NewTaskExecutorWithRegistry(taskbiz.NewTaskUsecaseWithRegistry(repo, registry), registry)
|
||||
log := executor.Run(context.Background(), &taskbiz.TimedTask{ID: 1, Name: "日报", ExecutorType: taskbiz.TaskExecutorMethod, MethodName: "report", Params: []byte(`{"_execution":{"maxRetryTimes":1,"retryIntervalSeconds":0}}`)}, "manual")
|
||||
stored := <-repo.logs
|
||||
if log.Status != "success" || stored.Output != log.Output {
|
||||
t.Fatalf("result was not persisted: %+v", log)
|
||||
}
|
||||
for _, detail := range []string{"方法:report", "第 1 次执行失败", "第 2 次执行", "已提交 12 行", "执行结束:success"} {
|
||||
if !strings.Contains(log.Output, detail) {
|
||||
t.Fatalf("missing %q in %s", detail, log.Output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMethodTimeoutKeepsPartialProgressWithoutClaimingCompletion(t *testing.T) {
|
||||
registry := platformtask.NewRegistry()
|
||||
release := make(chan struct{})
|
||||
finished := make(chan struct{})
|
||||
registry.Register(platformtask.Method{Name: "slow", Run: func(ctx context.Context, _ json.RawMessage) error {
|
||||
defer close(finished)
|
||||
platformtask.Logf(ctx, "已处理第一批")
|
||||
<-release
|
||||
platformtask.Logf(ctx, "迟到的结果")
|
||||
return nil
|
||||
}})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Millisecond)
|
||||
defer cancel()
|
||||
log := (&TaskExecutor{methods: registry}).Run(ctx, &taskbiz.TimedTask{ID: 1, ExecutorType: taskbiz.TaskExecutorMethod, MethodName: "slow"}, "manual")
|
||||
close(release)
|
||||
<-finished
|
||||
if log.Status != "timeout" || !strings.Contains(log.Output, "已处理第一批") || strings.Contains(log.Output, "迟到的结果") {
|
||||
t.Fatalf("timeout did not preserve the execution snapshot: %+v", log)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMethodReportsParentDeadlineAsTimeout(t *testing.T) {
|
||||
const methodName = "worker-test-parent-deadline"
|
||||
methods := platformtask.NewRegistry()
|
||||
|
||||
Reference in New Issue
Block a user