52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
// R2-B4:注册→取消→CancelFunc 被调用;注销后 CancelInflight 返回 false。
|
|
func TestInflightCancel(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
const id = "req-cancel-1"
|
|
RegisterInflight(id, cancel)
|
|
if !CancelInflight(id) {
|
|
t.Fatal("已注册应能取消")
|
|
}
|
|
if ctx.Err() != context.Canceled {
|
|
t.Fatal("CancelFunc 应已被调用")
|
|
}
|
|
// 取消后即注销,重复取消返回 false
|
|
if CancelInflight(id) {
|
|
t.Fatal("注销后 CancelInflight 应返回 false")
|
|
}
|
|
}
|
|
|
|
func TestInflightUnregister(t *testing.T) {
|
|
_, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
const id = "req-unregister-1"
|
|
RegisterInflight(id, cancel)
|
|
UnregisterInflight(id)
|
|
if CancelInflight(id) {
|
|
t.Fatal("注销后不应再取消到")
|
|
}
|
|
// 幂等:重复注销不 panic
|
|
UnregisterInflight(id)
|
|
}
|
|
|
|
// 空 id / nil cancel 注册应被忽略。
|
|
func TestInflightRegisterGuards(t *testing.T) {
|
|
RegisterInflight("", func() {})
|
|
if CancelInflight("") {
|
|
t.Fatal("空 requestId 不应注册成功")
|
|
}
|
|
RegisterInflight("req-nil-cancel", nil)
|
|
if CancelInflight("req-nil-cancel") {
|
|
t.Fatal("nil cancel 不应注册成功")
|
|
}
|
|
}
|