apptest

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package apptest 把「单体与微服务语义一致」从设计承诺变成可运行的断言。

框架保证契约边界的四件套(事务守卫 / ctx 防火墙 / 独立超时 / 错误规范化) 在进程内绑定与远程 client 上表现一致,但**保证是保证,实现是实现**: 手写的 client 忘了走 contract.Call、两侧 DTO 的 json key 对不上、领域错误 在 problem+json 往返后换了码、client 没把 callctx 白名单带上——这些都会让 "部署形态是启动参数"这句话失效,而且只在真正拆分部署的那天才暴露。

Conform 让同一批用例分别跑过每个绑定,逐项比对可观测行为:

apptest.Conform(t,
    []apptest.Binding[greetv1.Service]{
        {Name: "local", Service: greetv1.WrapService(greet.NewService(), 0)},
        {Name: "remote", Service: greetv1.NewClient(srv.URL)},
    },
    []apptest.Case[greetv1.Service]{
        {Name: "正常问候", Do: greetCall("Ada", "zh"),
            Want: greetv1.GreetReply{Message: "你好,Ada!"}, Idempotent: true},
        {Name: "不支持的语言", Do: greetCall("Ada", "fr"),
            WantCode: greetv1.CodeUnsupportedLang},
    })

测试专用包:只在 _test.go 里 import。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Conform

func Conform[S any](t *testing.T, bindings []Binding[S], cases []Case[S])

Conform 用 cases 逐条比对 bindings 的可观测行为。每条用例断言:

  1. 成功/失败与错误码符合 Case 的期望,且失败时错误是 *apperr.Error;
  2. 返回值在各绑定之间 DeepEqual——JSON 往返丢字段在这里现形;
  3. 事务内发起调用被拒(CodeTxBoundary),每个绑定都要拒;
  4. ctx 已取消时调用直接失败(CodeUnavailable),不落到实现上;
  5. Idempotent 用例连调两次结果一致;
  6. 填了 Binding.SeenMeta 时:callctx 白名单真的穿过了每一种绑定——这条走的 是请求头,从返回值里看不见,也是最安静的一种失效。同样只在 Idempotent 用例上做(要额外发一次真实调用)。

少于两个绑定会直接失败:只跑一个形态的不叫一致性测试。

func ConformWithMeta added in v0.9.1

func ConformWithMeta[S any](t *testing.T, bindings []Binding[S], cases []Case[S], meta callctx.Meta)

ConformWithMeta runs the same boundary checks with explicit, domain-valid metadata on every call. Use it for authenticated bindings whose tenant or partition identifiers must match signed claims and request DTOs. RequestID must be nonempty so loss of metadata cannot pass unnoticed. TenantID and Partition may be empty for domains without those dimensions.

The metadata is not proof of authorization: the remote credential provider and recipient must authorize it independently. Caller may change per hop and is not compared. Conform's existing signature and default probes are unchanged.

Types

type Binding

type Binding[S any] struct {
	Name    string
	Service S

	// SeenMeta 可选:返回服务端在最近一次调用里实际看到的 callctx.Meta。
	//
	// 填了它,Conform 才验得到跨边界元数据传播——那是唯一一条从返回值里看不见
	// 的边界语义,request id 与租户走的是请求头。不填则跳过该项。
	//
	// 检查跑在「期望成功且声明了 Idempotent」的用例上(它要额外发一次真实调用),
	// 所以至少得有一条那样的用例,否则 Conform 会报「配了个摆设」。
	//
	// 接法是让所有绑定共用同一个记录用的实现,SeenMeta 都读同一处——这正是
	// 要比对的「同一个实现,两种到达方式」:
	//
	//	spy := &metaSpy{inner: echo.NewService()}
	//	seen := func() callctx.Meta { return spy.Last() }
	//	[]apptest.Binding[echov1.Service]{
	//	    {Name: "local", Service: echov1.WrapService(spy, 0), SeenMeta: seen},
	//	    {Name: "remote", Service: echov1.NewClient(srv.URL), SeenMeta: seen},
	//	}
	//
	// 要么每个绑定都填,要么都不填:只填一部分时,漏掉的恰好可能是没接白名单
	// 的那个形态,检查会安静地放过它。
	SeenMeta func() callctx.Meta
}

Binding 是被测契约接口的一种绑定形态。Name 只用于测试输出, 习惯上是 "local" 与 "remote"。

type Case

type Case[S any] struct {
	Name string
	Do   func(ctx context.Context, svc S) (any, error)

	// WantCode 非空表示期望失败,且 apperr 错误码等于它;空则期望成功。
	WantCode string

	// Want 非 nil 时断言每个绑定的返回值与它 reflect.DeepEqual。
	// 留空则只做绑定之间的相互比对(跨形态一致即可)。
	Want any

	// Idempotent 声明这个调用重复执行应当得到相同结果。置 true 后
	// Conform 会连调两次并比对——重试与 at-least-once 投递下这是前提。
	//
	// 它同时是 Conform 额外发真实调用的**唯一授权**:元数据传播检查也只在
	// Idempotent 用例上做。留 false 的用例,Conform 保证只让它到达实现一次。
	Idempotent bool
}

Case 是一条契约用例:一次调用加上对结果的期望。

Do 必须只做一次契约调用并原样返回其结果——Conform 会用不同的 ctx 反复 调用它来检验边界语义,Do 里夹带别的逻辑会让失败信息指错地方。

Jump to

Keyboard shortcuts

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