mockprovider

package
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package mockprovider 提供可控的异步任务模拟 HTTP 服务,供 B-M1 验证用。

模拟真实外部 Provider 的 submit→poll/callback 语义,但所有行为可控:

  • 延迟(模拟网络/处理耗时)
  • 失败(模拟外部错误)
  • 重复回调(模拟幂等测试)

三个端点:

POST /submit  → {"task_id": "xxx"}
GET  /poll    → {"status": "pending|done", "result": {...}}
POST /callback → 触发回调(预留)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Provider

type Provider struct {

	// SubmitDelay 模拟提交耗时
	SubmitDelay time.Duration
	// ProcessTime 模拟处理耗时(submit 后多久 poll 返回 done)
	ProcessTime time.Duration
	// ShouldFail 控制 /poll 是否返回错误
	ShouldFail bool
	// contains filtered or unexported fields
}

Provider 是一个可控的异步任务模拟器。

func New

func New(processTime time.Duration) *Provider

New 创建 Provider。processTime 是 submit 后任务变为 done 的默认耗时。

func (*Provider) ForceComplete

func (p *Provider) ForceComplete(taskID string)

ForceComplete 强制将任务标记为 done(模拟跳过等待直接完成)。

func (*Provider) GetTask

func (p *Provider) GetTask(taskID string) *Task

GetTask 返回任务信息。

func (*Provider) ServeHTTP

func (p *Provider) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 实现 http.Handler。

func (*Provider) SetTaskBehavior

func (p *Provider) SetTaskBehavior(taskID string, b TaskBehavior)

SetTaskBehavior 为指定 task 配置行为(submit 后调用)。

type Task

type Task struct {
	ID        string         `json:"id"`
	Status    string         `json:"status"` // "pending" | "done" | "failed"
	Result    map[string]any `json:"result,omitempty"`
	Input     map[string]any `json:"input"`
	CreatedAt time.Time      `json:"created_at"`
	DoneAt    time.Time      `json:"done_at,omitempty"`
}

Task 表示一个已提交的异步任务。

type TaskBehavior

type TaskBehavior struct {
	ResultStatus string         // "done" 或 "failed"
	Result       map[string]any // 完成时的结果(仅 done)
	Duration     time.Duration  // 覆盖全局 ProcessTime
}

TaskBehavior 控制单个 task 的行为(B-M2:不同分支不同命运)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL