优化结构
This commit is contained in:
@@ -4,11 +4,12 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"kra/internal/biz/system"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
taskbiz "kra/internal/biz/task"
|
||||
)
|
||||
|
||||
func TestPrivateIP(t *testing.T) {
|
||||
@@ -24,25 +25,25 @@ func TestPrivateIP(t *testing.T) {
|
||||
|
||||
func TestRunMethodReportsParentDeadlineAsTimeout(t *testing.T) {
|
||||
const methodName = "worker-test-parent-deadline"
|
||||
system.RegisterTaskMethod(methodName, "test", func(ctx context.Context, _ json.RawMessage) error {
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(ctx context.Context, _ json.RawMessage) error {
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
})
|
||||
executor := &TaskExecutor{}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := executor.runMethod(ctx, &system.TimedTask{MethodName: methodName}); !errors.Is(err, errTaskTimeout) {
|
||||
if err := executor.runMethod(ctx, &taskbiz.TimedTask{MethodName: methodName}); !errors.Is(err, errTaskTimeout) {
|
||||
t.Fatalf("runMethod() error = %v, want errTaskTimeout", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMethodConvertsPanicToError(t *testing.T) {
|
||||
const methodName = "worker-test-panic"
|
||||
system.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
panic("boom")
|
||||
})
|
||||
executor := &TaskExecutor{}
|
||||
err := executor.runMethod(context.Background(), &system.TimedTask{MethodName: methodName})
|
||||
err := executor.runMethod(context.Background(), &taskbiz.TimedTask{MethodName: methodName})
|
||||
if err == nil || !strings.Contains(err.Error(), "panic: boom") {
|
||||
t.Fatalf("runMethod() error = %v, want recovered panic", err)
|
||||
}
|
||||
@@ -51,7 +52,7 @@ func TestRunMethodConvertsPanicToError(t *testing.T) {
|
||||
func TestRunMethodUsesParentContext(t *testing.T) {
|
||||
const methodName = "worker-test-parent-context"
|
||||
started := make(chan struct{})
|
||||
system.RegisterTaskMethod(methodName, "test", func(ctx context.Context, _ json.RawMessage) error {
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(ctx context.Context, _ json.RawMessage) error {
|
||||
close(started)
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
@@ -60,7 +61,7 @@ func TestRunMethodUsesParentContext(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- executor.runMethod(ctx, &system.TimedTask{MethodName: methodName})
|
||||
done <- executor.runMethod(ctx, &taskbiz.TimedTask{MethodName: methodName})
|
||||
}()
|
||||
select {
|
||||
case <-started:
|
||||
@@ -81,14 +82,14 @@ func TestRunMethodUsesParentContext(t *testing.T) {
|
||||
func TestRunMethodHonorsDeadlineWhenMethodIgnoresContext(t *testing.T) {
|
||||
const methodName = "worker-test-ignores-context"
|
||||
release := make(chan struct{})
|
||||
system.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
taskbiz.RegisterTaskMethod(methodName, "test", func(context.Context, json.RawMessage) error {
|
||||
<-release
|
||||
return nil
|
||||
})
|
||||
executor := &TaskExecutor{}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := executor.runMethod(ctx, &system.TimedTask{MethodName: methodName}); !errors.Is(err, errTaskTimeout) {
|
||||
if err := executor.runMethod(ctx, &taskbiz.TimedTask{MethodName: methodName}); !errors.Is(err, errTaskTimeout) {
|
||||
close(release)
|
||||
t.Fatalf("runMethod() error = %v, want errTaskTimeout", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user