Files
NixMsg/test/accept/directory_scale_test.go
T

85 lines
1.9 KiB
Go

package accept
import (
"fmt"
"os"
"strconv"
"testing"
"time"
"git.asio.asia/nixevol/NixMsg/internal/protocol"
"git.asio.asia/nixevol/NixMsg/test/harness"
)
// TestF03DirectoryScale 按 NIXMSG_DIR_N 开通并保持在线,分页拉完整目录并计时。
// 默认跳过,不进入 task check。
func TestF03DirectoryScale(t *testing.T) {
if os.Getenv("NIXMSG_DIR_SCALE") != "1" {
t.Skip("设置 NIXMSG_DIR_SCALE=1 才跑目录规模测试")
}
n := 50
if v := os.Getenv("NIXMSG_DIR_N"); v != "" {
parsed, err := strconv.Atoi(v)
if err != nil || parsed < 1 {
t.Fatalf("NIXMSG_DIR_N=%q", v)
}
n = parsed
}
srv, err := harness.Start(harness.Options{})
if err != nil {
t.Fatal(err)
}
defer func() { _ = srv.Stop() }()
ac := AdminLogin(t, srv)
const password = "password1234"
ids := make([]string, n)
for i := 0; i < n; i++ {
ids[i] = fmt.Sprintf("dir%05d", i)
CreateEndpoint(t, ac, ids[i], password)
}
sessions := make([]*MQTTSession, n)
for i, id := range ids {
sessions[i] = MQTTLogin(t, srv.HTTPBase, id, password)
}
t.Cleanup(func() {
for _, s := range sessions {
if s != nil {
s.Close()
}
}
})
start := time.Now()
got := 0
cursor := ""
for page := 0; page < n+2; page++ {
frame := map[string]any{
"v": 1, "type": protocol.TypeDirectoryList, "rid": fmt.Sprintf("d%d", page),
"limit": protocol.MaxPageLimit,
}
if cursor != "" {
frame["cursor"] = cursor
}
resp := sessions[0].Request(t, frame)
if !resp.OK {
t.Fatalf("directory page %d: %+v", page, resp.Raw)
}
data, _ := resp.Data.(map[string]any)
items, _ := data["items"].([]any)
got += len(items)
next, _ := data["next_cursor"].(string)
if next == "" {
break
}
cursor = next
}
elapsed := time.Since(start)
if got < n {
t.Fatalf("listed %d want at least %d in %s", got, n, elapsed)
}
t.Logf("F03c N=%d listed=%d elapsed=%s", n, got, elapsed)
}