阶段十:完成非冻结 Browser 注释基线清零
This commit is contained in:
1 parent
a45a82a149
commit
393c45a1e3
22 files changed
+684
-4177
No files matched your search
@@ -12,13 +12,18 @@ import (
|
||||
|
||||
// TestChromiumLaunchArgs 验证启动参数含关键安全/反检测项。
|
||||
func TestChromiumLaunchArgs(t *testing.T) {
|
||||
// args 保存args,供当前处理流程使用
|
||||
args := chromiumLaunchArgs()
|
||||
if len(args) == 0 {
|
||||
t.Fatal("应返回非空参数列表")
|
||||
}
|
||||
// want 保存want,供当前处理流程使用
|
||||
want := []string{"--no-sandbox", "--disable-dev-shm-usage", "--disable-blink-features=AutomationControlled", "--lang=zh-CN"}
|
||||
// w 表示当前遍历过程中的w
|
||||
for _, w := range want {
|
||||
// found 保存found,供当前处理流程使用
|
||||
found := false
|
||||
// a 表示当前遍历过程中的a
|
||||
for _, a := range args {
|
||||
if a == w {
|
||||
found = true
|
||||
@@ -31,25 +36,34 @@ func TestChromiumLaunchArgs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPackagedPlaywrightRuntimeReady 负责TestPackagedPlaywrightRuntimeReady相关处理。
|
||||
func TestPackagedPlaywrightRuntimeReady(t *testing.T) {
|
||||
// runtimeRoot 保存runtimeRoot,供当前处理流程使用
|
||||
runtimeRoot := t.TempDir()
|
||||
// driverDir 保存driverDir,供当前处理流程使用
|
||||
driverDir := filepath.Join(runtimeRoot, "driver")
|
||||
// browserDir 保存浏览器Dir,供当前处理流程使用
|
||||
browserDir := filepath.Join(runtimeRoot, "browsers")
|
||||
if err := os.MkdirAll(filepath.Join(driverDir, "package"), 0o755); err != nil {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := os.MkdirAll(filepath.Join(driverDir, "package"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(browserDir, "chromium-1228"), 0o755); err != nil {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := os.MkdirAll(filepath.Join(browserDir, "chromium-1228"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// nodeName 保存node名称,供当前处理流程使用
|
||||
nodeName := "node"
|
||||
if runtime.GOOS == "windows" {
|
||||
nodeName = "node.exe"
|
||||
}
|
||||
// path 表示当前遍历过程中的路径
|
||||
for _, path := range []string{
|
||||
filepath.Join(driverDir, nodeName),
|
||||
filepath.Join(driverDir, "package", "cli.js"),
|
||||
} {
|
||||
if err := os.WriteFile(path, []byte("runtime"), 0o755); err != nil {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := os.WriteFile(path, []byte("runtime"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -61,21 +75,30 @@ func TestPackagedPlaywrightRuntimeReady(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPackagedPlaywrightRuntimeReadyWithExternalNode 负责TestPackagedPlaywrightRuntimeReadyWithExternalNode相关处理。
|
||||
func TestPackagedPlaywrightRuntimeReadyWithExternalNode(t *testing.T) {
|
||||
// runtimeRoot 保存runtimeRoot,供当前处理流程使用
|
||||
runtimeRoot := t.TempDir()
|
||||
// driverDir 保存driverDir,供当前处理流程使用
|
||||
driverDir := filepath.Join(runtimeRoot, "driver")
|
||||
// browserDir 保存浏览器Dir,供当前处理流程使用
|
||||
browserDir := filepath.Join(runtimeRoot, "browsers")
|
||||
if err := os.MkdirAll(filepath.Join(driverDir, "package"), 0o755); err != nil {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := os.MkdirAll(filepath.Join(driverDir, "package"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(browserDir, "chromium-1228"), 0o755); err != nil {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := os.MkdirAll(filepath.Join(browserDir, "chromium-1228"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(driverDir, "package", "cli.js"), []byte("runtime"), 0o644); err != nil {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := os.WriteFile(filepath.Join(driverDir, "package", "cli.js"), []byte("runtime"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// nodePath 保存node路径,供当前处理流程使用
|
||||
nodePath := filepath.Join(runtimeRoot, "node")
|
||||
if err := os.WriteFile(nodePath, []byte("runtime"), 0o755); err != nil {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := os.WriteFile(nodePath, []byte("runtime"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PLAYWRIGHT_DRIVER_PATH", driverDir)
|
||||
@@ -99,7 +122,9 @@ func newTestManager(maxSize int) *Manager {
|
||||
|
||||
// TestTouchUpdatesLastUsed touch 命中池中条目时更新 lastUsed。
|
||||
func TestTouchUpdatesLastUsed(t *testing.T) {
|
||||
// m 保存m,供当前处理流程使用
|
||||
m := newTestManager(3)
|
||||
// old 保存old,供当前处理流程使用
|
||||
old := time.Now().Add(-time.Hour)
|
||||
m.pool["c1"] = &poolEntry{cookieID: "c1", lastUsed: old}
|
||||
m.touch("c1")
|
||||
@@ -112,14 +137,17 @@ func TestTouchUpdatesLastUsed(t *testing.T) {
|
||||
|
||||
// TestEvictRemovesEntry evict 删除指定条目(nil browser 时 closeEntry 为 no-op)。
|
||||
func TestEvictRemovesEntry(t *testing.T) {
|
||||
// m 保存m,供当前处理流程使用
|
||||
m := newTestManager(3)
|
||||
m.pool["c1"] = &poolEntry{cookieID: "c1"}
|
||||
m.pool["c2"] = &poolEntry{cookieID: "c2"}
|
||||
m.evict("c1")
|
||||
if _, ok := m.pool["c1"]; ok {
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
_, ok := m.pool["c1"]; ok {
|
||||
t.Fatal("evict 应删除 c1")
|
||||
}
|
||||
if _, ok := m.pool["c2"]; !ok {
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
_, ok := m.pool["c2"]; !ok {
|
||||
t.Fatal("c2 应保留")
|
||||
}
|
||||
// evict 不存在的条目不 panic。
|
||||
@@ -128,6 +156,7 @@ func TestEvictRemovesEntry(t *testing.T) {
|
||||
|
||||
// TestEvictIfNeededEvictsOldest 池满时驱逐最久未用的条目。
|
||||
func TestEvictIfNeededEvictsOldest(t *testing.T) {
|
||||
// m 保存m,供当前处理流程使用
|
||||
m := newTestManager(2)
|
||||
m.pool["c1"] = &poolEntry{cookieID: "c1", lastUsed: time.Now().Add(-2 * time.Hour)}
|
||||
m.pool["c2"] = &poolEntry{cookieID: "c2", lastUsed: time.Now()}
|
||||
@@ -135,35 +164,44 @@ func TestEvictIfNeededEvictsOldest(t *testing.T) {
|
||||
if _, ok := m.pool["c1"]; ok {
|
||||
t.Fatal("应驱逐最旧的 c1")
|
||||
}
|
||||
if _, ok := m.pool["c2"]; !ok {
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
_, ok := m.pool["c2"]; !ok {
|
||||
t.Fatal("c2 应保留")
|
||||
}
|
||||
}
|
||||
|
||||
// TestEvictIfNeededNoopWhenUnderLimit 池未满时不驱逐。
|
||||
func TestEvictIfNeededNoopWhenUnderLimit(t *testing.T) {
|
||||
// m 保存m,供当前处理流程使用
|
||||
m := newTestManager(5)
|
||||
m.pool["c1"] = &poolEntry{cookieID: "c1", lastUsed: time.Now()}
|
||||
m.evictIfNeeded()
|
||||
if _, ok := m.pool["c1"]; !ok {
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
_, ok := m.pool["c1"]; !ok {
|
||||
t.Fatal("未满不应驱逐")
|
||||
}
|
||||
}
|
||||
|
||||
// TestEvictIfNeededSkipsActiveEntries 负责TestEvictIfNeededSkipsActiveEntries相关处理。
|
||||
func TestEvictIfNeededSkipsActiveEntries(t *testing.T) {
|
||||
// m 保存m,供当前处理流程使用
|
||||
m := newTestManager(2)
|
||||
m.pool["active-old"] = &poolEntry{cookieID: "active-old", lastUsed: time.Now().Add(-2 * time.Hour), active: 1}
|
||||
m.pool["idle-new"] = &poolEntry{cookieID: "idle-new", lastUsed: time.Now()}
|
||||
m.evictIfNeeded()
|
||||
if _, ok := m.pool["active-old"]; !ok {
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
_, ok := m.pool["active-old"]; !ok {
|
||||
t.Fatal("正在执行 token 请求的条目不得被淘汰")
|
||||
}
|
||||
if _, ok := m.pool["idle-new"]; ok {
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
_, ok := m.pool["idle-new"]; ok {
|
||||
t.Fatal("池满时应优先淘汰空闲条目")
|
||||
}
|
||||
}
|
||||
|
||||
// TestEvictIfNeededAllowsTemporaryOverflowWhenAllActive 负责TestEvictIfNeededAllowsTemporaryOverflowWhenAllActive相关处理。
|
||||
func TestEvictIfNeededAllowsTemporaryOverflowWhenAllActive(t *testing.T) {
|
||||
// m 保存m,供当前处理流程使用
|
||||
m := newTestManager(2)
|
||||
m.pool["active-1"] = &poolEntry{cookieID: "active-1", lastUsed: time.Now().Add(-2 * time.Hour), active: 1}
|
||||
m.pool["active-2"] = &poolEntry{cookieID: "active-2", lastUsed: time.Now().Add(-time.Hour), active: 1}
|
||||
@@ -173,23 +211,29 @@ func TestEvictIfNeededAllowsTemporaryOverflowWhenAllActive(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestCleanupIdleSkipsActiveEntries 负责TestCleanupIdleSkipsActiveEntries相关处理。
|
||||
func TestCleanupIdleSkipsActiveEntries(t *testing.T) {
|
||||
// m 保存m,供当前处理流程使用
|
||||
m := newTestManager(3)
|
||||
m.idleTTL = time.Minute
|
||||
// old 保存old,供当前处理流程使用
|
||||
old := time.Now().Add(-time.Hour)
|
||||
m.pool["active"] = &poolEntry{cookieID: "active", lastUsed: old, active: 1}
|
||||
m.pool["idle"] = &poolEntry{cookieID: "idle", lastUsed: old}
|
||||
m.CleanupIdle()
|
||||
if _, ok := m.pool["active"]; !ok {
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
_, ok := m.pool["active"]; !ok {
|
||||
t.Fatal("CleanupIdle 不得关闭仍有租约的条目")
|
||||
}
|
||||
if _, ok := m.pool["idle"]; ok {
|
||||
if // ok 保存ok,供当前处理流程使用
|
||||
_, ok := m.pool["idle"]; ok {
|
||||
t.Fatal("CleanupIdle 应清理过期空闲条目")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMarshalCookies 导出包装器等价 cookieMarshal。
|
||||
func TestMarshalCookies(t *testing.T) {
|
||||
// got 保存got,供当前处理流程使用
|
||||
got := MarshalCookies(map[string]string{"unb": "1", "cna": "xx"})
|
||||
// map 顺序不保证,逐项检查。
|
||||
if !contains(got, "unb=1") || !contains(got, "cna=xx") {
|
||||
@@ -199,10 +243,12 @@ func TestMarshalCookies(t *testing.T) {
|
||||
|
||||
// TestCookiesToMap playwright.Cookie 切片转 map。
|
||||
func TestCookiesToMap(t *testing.T) {
|
||||
// cs 保存cs,供当前处理流程使用
|
||||
cs := []playwright.Cookie{
|
||||
{Name: "unb", Value: "123"},
|
||||
{Name: "_m_h5_tk", Value: "tok"},
|
||||
}
|
||||
// m 保存m,供当前处理流程使用
|
||||
m := cookiesToMap(cs)
|
||||
if m["unb"] != "123" || m["_m_h5_tk"] != "tok" || len(m) != 2 {
|
||||
t.Fatalf("cookiesToMap=%+v", m)
|
||||
@@ -213,8 +259,10 @@ func TestCookiesToMap(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// contains 负责contains相关处理。
|
||||
func contains(s, sub string) bool {
|
||||
for i := 0; i+len(sub) <= len(s); i++ {
|
||||
for // i 保存i,供当前处理流程使用
|
||||
i := 0; i+len(sub) <= len(s); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user