修复全量测试竞态并更新审查夹具,保存证书覆盖调整前基线
This commit is contained in:
1 parent
78dfcde5e1
commit
2aac6dd9be
15 files changed
+146
-158
No files matched your search
@@ -2,62 +2,23 @@
|
||||
package passwordtest
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
"git.pcux.cn/xuans/Smail/internal/passwordcapacity"
|
||||
)
|
||||
|
||||
type heldReader struct {
|
||||
original io.Reader
|
||||
entered chan struct{}
|
||||
resume chan struct{}
|
||||
enteredOnce atomic.Bool
|
||||
}
|
||||
|
||||
func (r *heldReader) Read(p []byte) (int, error) {
|
||||
if r.enteredOnce.CompareAndSwap(false, true) {
|
||||
close(r.entered)
|
||||
<-r.resume
|
||||
panic(r)
|
||||
}
|
||||
return r.original.Read(p)
|
||||
}
|
||||
|
||||
// Hold 仅供非并行测试使用,释放时同时验证异常路径没有泄漏计算名额。
|
||||
// Hold 仅供非并行测试使用,直接占用计算名额,不修改 TLS 等服务使用的随机源。
|
||||
func Hold(t *testing.T) func() {
|
||||
t.Helper()
|
||||
r := &heldReader{original: rand.Reader, entered: make(chan struct{}), resume: make(chan struct{})}
|
||||
rand.Reader = r
|
||||
done := make(chan any, 1)
|
||||
go func() {
|
||||
defer func() { done <- recover() }()
|
||||
_, _ = password.Hash("HeldPass123")
|
||||
}()
|
||||
unlock, err := passwordcapacity.Acquire()
|
||||
if err != nil {
|
||||
t.Fatalf("密码计算名额未被测试请求占用:%v", err)
|
||||
}
|
||||
var once sync.Once
|
||||
release := func() {
|
||||
once.Do(func() {
|
||||
close(r.resume)
|
||||
defer func() { rand.Reader = r.original }()
|
||||
select {
|
||||
case result := <-done:
|
||||
if result != r {
|
||||
t.Errorf("密码测试占位未经过预期的异常释放路径")
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Error("密码测试占位释放超时")
|
||||
}
|
||||
})
|
||||
once.Do(unlock)
|
||||
}
|
||||
t.Cleanup(release)
|
||||
select {
|
||||
case <-r.entered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("密码计算名额未被测试请求占用")
|
||||
}
|
||||
return release
|
||||
}
|
||||
Reference in new issue
Block a user