sync: 同步 v1.0.4 开源代码并脱敏
This commit is contained in:
@@ -136,6 +136,64 @@ func (s *repositoryStub) ReclaimProcessing(_ context.Context, batchNo string, ba
|
||||
return s.reclaimed, s.reclaimErr
|
||||
}
|
||||
|
||||
type durableExecutorStub struct {
|
||||
execute func(context.Context) error
|
||||
protectErr error
|
||||
protectCalls int
|
||||
}
|
||||
|
||||
func (s *durableExecutorStub) Execute(ctx context.Context, _ string) error {
|
||||
if s.execute != nil {
|
||||
return s.execute(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *durableExecutorStub) ProtectExecutionFailure(ctx context.Context, _ string, _ int, _ string) (bool, error) {
|
||||
s.protectCalls++
|
||||
if ctx.Err() != nil {
|
||||
return true, ctx.Err()
|
||||
}
|
||||
return true, s.protectErr
|
||||
}
|
||||
|
||||
func TestRuntimeDurableOwnsEveryFailurePath(t *testing.T) {
|
||||
for _, kind := range []string{"error", "deadline-error", "deadline-nil", "failure-reason", "retry-exhausted", "protect-db-down"} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
repo := &repositoryStub{mains: []*entity.BatchMain{{BatchNo: "DURABLE", BatchType: Type109, BatchAsyncStatus: AsyncStatusPending}}, claimed: true}
|
||||
exe := &durableExecutorStub{}
|
||||
msg := Message{BatchNo: "DURABLE", BatchType: Type109}
|
||||
switch kind {
|
||||
case "error":
|
||||
exe.execute = func(context.Context) error { return errors.New("执行中断") }
|
||||
case "deadline-error":
|
||||
exe.execute = func(ctx context.Context) error { <-ctx.Done(); return ctx.Err() }
|
||||
case "deadline-nil":
|
||||
exe.execute = func(ctx context.Context) error { <-ctx.Done(); return nil }
|
||||
case "failure-reason", "retry-exhausted":
|
||||
msg.FailureReason = "重试耗尽或失败重投"
|
||||
repo.mains[0].BatchAsyncStatus = AsyncStatusProcessing
|
||||
case "protect-db-down":
|
||||
exe.execute = func(context.Context) error { return errors.New("执行中断") }
|
||||
exe.protectErr = errors.New("数据库不可用")
|
||||
}
|
||||
dispatcher := NewDispatcher(&executorStub{})
|
||||
dispatcher.Register(Type109, exe)
|
||||
err := NewRuntime(repo, dispatcher, WithExecutionTimeout(time.Millisecond)).Handle(context.Background(), msg)
|
||||
if kind == "protect-db-down" {
|
||||
var failure *FailureMarkError
|
||||
if !errors.As(err, &failure) {
|
||||
t.Fatalf("保护失败应可重投: %v", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if exe.protectCalls != 1 || repo.failCalls != 0 {
|
||||
t.Fatalf("通用MarkFailed越过durable保护: protect=%d failed=%d", exe.protectCalls, repo.failCalls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageJSONContract(t *testing.T) {
|
||||
body, err := json.Marshal(Message{BatchNo: "BATCH-1", BatchType: Type103})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user