mockgw

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package mockgw 是一个可注入故障的 mock LLM/OCR 网关,只给 e2e 测试用,不属于库的公开 API。 零新依赖:只用标准库(net/http/httptest + math/rand 固定种子)。

把生产环境踩过的坑做成开关:

  • Latency:每个正常请求固定延迟(模拟网关真实处理耗时,也用来撑起可观测的并发);
  • BusyAfterConcurrency:并发超过 n 时返回 HTTP 200、但 body 里藏 busy 错误事件 (复刻"错误藏在 200 的 SSE 体里"的真实网关行为——HTTP 状态码骗人);
  • FailRate:固定种子的随机 HTTP 500(同种子同序列,CI 可复现);
  • CrashAfterConcurrency:并发超过 n 时直接掐断 TCP 连接(复刻并发打爆后断连的网关);
  • BusyFirstN:前 n 个请求定向返回 busy 体,与并发无关(单独复刻"SSE 藏错误"场景)。

响应体约定(见 specs/003-m3-polish/data-model.md 第 5 节):

  • 正常:HTTP 200,body `data: {"ok":true}`(请求带 body 时回显成 `data: {"ok":true,"echo":<请求体>}`,要求请求体是合法 JSON);
  • busy:HTTP 200,body `data: {"error":"busy"}`;
  • 随机失败:HTTP 500;
  • 断连:不写任何响应,直接关连接,客户端看到 EOF/连接重置。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gateway

type Gateway struct {
	// contains filtered or unexported fields
}

Gateway 一个跑在 httptest.Server 上的 mock 网关,带原子的并发观测口。

func New

func New(opts ...Option) *Gateway

New 起一个 mock 网关(内部是 httptest.Server),用完必须 Close。

func (*Gateway) BusyCount

func (g *Gateway) BusyCount() int

BusyCount busy 响应的总次数(含并发触发与 BusyFirstN 定向触发)。

func (*Gateway) Close

func (g *Gateway) Close()

Close 关掉网关,等在处理的请求收尾。

func (*Gateway) CrashCount

func (g *Gateway) CrashCount() int

CrashCount 断连的总次数。

func (*Gateway) MaxConcurrency

func (g *Gateway) MaxConcurrency() int

MaxConcurrency 观测到的最大并发(原子峰值,竞态下不少算)。

func (*Gateway) Requests

func (g *Gateway) Requests() int

Requests 收到的总请求数(含 busy/500/断连)。

func (*Gateway) URL

func (g *Gateway) URL() string

URL 网关的基地址(http://127.0.0.1:端口)。

type Option

type Option func(*Gateway)

Option 网关的故障开关。

func BusyAfterConcurrency

func BusyAfterConcurrency(n int) Option

BusyAfterConcurrency 并发超过 n 时返回 HTTP 200 + body 里的 busy 错误事件。

func BusyFirstN

func BusyFirstN(n int) Option

BusyFirstN 前 n 个请求(按到达顺序)定向返回 busy 体,与并发无关。 专门给"SSE 藏错误 → handler 判定 ErrThrottled → 重排后成功"这条路径用。

func CrashAfterConcurrency

func CrashAfterConcurrency(n int) Option

CrashAfterConcurrency 并发超过 n 时不写任何响应、直接关掉 TCP 连接。

func FailRate

func FailRate(p float64, seed int64) Option

FailRate 以概率 p 返回 HTTP 500,随机源用固定种子 seed(同种子同序列,CI 确定)。

func Latency

func Latency(d time.Duration) Option

Latency 每个正常请求固定延迟 d(busy/断连是快速拒绝,不吃延迟)。

Jump to

Keyboard shortcuts

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