Beacon/server/workflow/workflow.go

92 lines
2.9 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 workflow
// 定义 Temporal Workflow
import (
"beacon/server/activity"
"beacon/server/gen/pb"
"fmt"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
"time"
)
// TestRunWorkflow 定义了整个测试执行的工作流
func TestRunWorkflow(ctx workflow.Context, input *pb.TestRunInput) (*pb.TestRunOutput, error) {
logger := workflow.GetLogger(ctx)
logger.Info("TestRunWorkflow started", "runID", input.RunId)
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Minute, // Activity 执行超时时间
HeartbeatTimeout: 30 * time.Second, // Heartbeat 防止 Worker 假死
RetryPolicy: &temporal.RetryPolicy{ // Activity 级别的重试策略
InitialInterval: time.Second,
BackoffCoefficient: 2.0,
MaximumInterval: time.Minute,
MaximumAttempts: 3,
NonRetryableErrorTypes: []string{"NonRetryableErrorType"}, // 自定义不可重试的错误
},
}
ctx = workflow.WithActivityOptions(ctx, ao)
var (
apiResults []*pb.ApiTestResult
uiResults []*pb.UiTestResult
overallSuccess = true
completionMessage = "Test run completed successfully."
)
// 执行 API 测试 Activity
if input.RunApiTests {
apiTestInput := &pb.ApiTestRequest{
TestCaseId: "api-example-1",
Endpoint: "/api/v1/data",
HttpMethod: "GET",
Headers: map[string]string{"Authorization": "Bearer token123"},
ExpectedStatusCode: 200,
}
var apiRes pb.ApiTestResult
err := workflow.ExecuteActivity(ctx, activity.RunApiTest, apiTestInput).Get(ctx, &apiRes)
if err != nil {
logger.Error("API test activity failed", "error", err)
// 可以选择标记为失败或者继续执行UI测试
overallSuccess = false
apiRes.BaseResult.Success = false
apiRes.BaseResult.Message = fmt.Sprintf("API Test Failed: %v", err)
}
apiResults = append(apiResults, &apiRes)
}
// 执行 UI 测试 Activity
if input.RunUiTests {
uiTestInput := &pb.UiTestRequest{
TestCaseId: "ui-example-1",
UrlPath: "/dashboard",
BrowserType: "chromium",
Headless: true,
UserData: map[string]string{"user": "test", "pass": "password"},
}
var uiRes pb.UiTestResult
err := workflow.ExecuteActivity(ctx, activity.RunUiTest, uiTestInput).Get(ctx, &uiRes)
if err != nil {
logger.Error("UI test activity failed", "error", err)
overallSuccess = false
uiRes.BaseResult.Success = false
uiRes.BaseResult.Message = fmt.Sprintf("UI Test Failed: %v", err)
}
uiResults = append(uiResults, &uiRes)
}
if !overallSuccess {
completionMessage = "Test run completed with failures."
}
logger.Info("TestRunWorkflow completed", "overallSuccess", overallSuccess)
return &pb.TestRunOutput{
RunId: input.RunId,
OverallSuccess: overallSuccess,
CompletionMessage: completionMessage,
ApiResults: apiResults,
UiResults: uiResults,
}, nil
}