Files
lijiaoqiao/supply-api/internal/domain/compensation_context_test.go
Your Name 6a5730a261 feat(supply-api): restore domain outbox support and worker tests
Add the domain-level outbox processor implementation required by existing domain tests, and add regression coverage for compensation worker cancellation behavior. Verified with a fresh go test run for ./internal/domain before commit.
2026-04-11 10:45:17 +08:00

66 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package domain
import (
"context"
"testing"
"time"
)
// TestCompensationProcessor_StartBackgroundWorker_Cancel
// TDD: 验证 StopBackgroundWorker 能够正确取消后台 worker
func TestCompensationProcessor_StartBackgroundWorker_Cancel(t *testing.T) {
store := &mockCompensationStore{}
executor := &mockOperationExecutor{}
stats := &mockCompensationStats{}
processor := NewCompensationProcessor(store, executor, stats)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 启动 worker
workerCtx := processor.StartBackgroundWorker(ctx, 100*time.Millisecond)
// 验证 worker context 不是已经取消的
select {
case <-workerCtx.Done():
t.Fatal("workerCtx should not be cancelled immediately")
default:
}
// 停止 worker
processor.StopBackgroundWorker()
// 验证 worker context 已被取消
select {
case <-workerCtx.Done():
// 预期context 应该在 Stop 后被取消
default:
t.Fatal("workerCtx should be cancelled after StopBackgroundWorker")
}
}
// TestCompensationProcessor_WorkerStopsOnParentContext
// TDD: 验证当父 context 取消时worker 应该停止
func TestCompensationProcessor_WorkerStopsOnParentContext(t *testing.T) {
store := &mockCompensationStore{}
executor := &mockOperationExecutor{}
stats := &mockCompensationStats{}
processor := NewCompensationProcessor(store, executor, stats)
ctx, cancel := context.WithCancel(context.Background())
// 启动 worker
workerCtx := processor.StartBackgroundWorker(ctx, 100*time.Millisecond)
// 取消父 context
cancel()
// 验证 worker context 已被取消
select {
case <-workerCtx.Done():
// 预期:父 context 取消后 worker 应该停止
default:
t.Fatal("workerCtx should be cancelled after parent context is cancelled")
}
}