把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到 third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或 改写 tag 都不再影响本仓库构建。 - 已验证 GOPROXY=off 下可完整编译并打出发行包 - go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希) - 升级方法与授权注意事项见 third_party/README.md
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package tool
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSleepReturns(t *testing.T) {
|
|
tl := NewSleep()
|
|
if tl.Name() != "sleep" || !tl.IsReadOnly(nil) {
|
|
t.Fatalf("unexpected identity: name=%q readonly=%v", tl.Name(), tl.IsReadOnly(nil))
|
|
}
|
|
res, err := tl.Call(context.Background(), json.RawMessage(`{"seconds":1}`), nil)
|
|
if err != nil || res.IsError {
|
|
t.Fatalf("call: err=%v res=%v", err, res)
|
|
}
|
|
if got := res.Flatten(); got != "slept 1s" {
|
|
t.Fatalf("got %q, want %q", got, "slept 1s")
|
|
}
|
|
}
|
|
|
|
func TestSleepInterrupted(t *testing.T) {
|
|
// A large requested duration must return promptly once ctx is cancelled, not
|
|
// block out the (clamped) wait.
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
go func() { time.Sleep(20 * time.Millisecond); cancel() }()
|
|
start := time.Now()
|
|
res, err := NewSleep().Call(ctx, json.RawMessage(`{"seconds":3600}`), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if elapsed := time.Since(start); elapsed > time.Second {
|
|
t.Fatalf("interrupt too slow: %v", elapsed)
|
|
}
|
|
if got := res.Flatten(); got != "sleep interrupted" {
|
|
t.Fatalf("got %q, want interrupted", got)
|
|
}
|
|
}
|