跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

lifecycle_test.go

127 行 · 3.4 KB历史下载
1package workbench
2
3import (
4 "context"
5 "errors"
6 "sync"
7 "testing"
8 "time"
9)
10
11func TestCloseBackgroundCancelsControlAndSafetyChecks(t *testing.T) {
12 s := &Server{}
13 control, cancelControl := s.controlContext()
14 defer cancelControl()
15 check, cancelCheck := context.WithCancel(context.Background())
16 defer cancelCheck()
17 monitor, cancelMonitor := context.WithCancel(context.Background())
18 defer cancelMonitor()
19 s.checkCancel, s.monitorCancel = cancelCheck, cancelMonitor
20 if deadline, ok := control.Deadline(); !ok || time.Until(deadline) > 5*time.Second {
21 t.Fatal("guest control has no bounded deadline")
22 }
23 s.closeBackground()
24 for name, ctx := range map[string]context.Context{"control": control, "check": check, "monitor": monitor} {
25 if ctx.Err() != context.Canceled {
26 t.Fatalf("%s survived controller shutdown: %v", name, ctx.Err())
27 }
28 }
29 late, cancelLate := s.controlContext()
30 defer cancelLate()
31 if late.Err() != context.Canceled {
32 t.Fatal("new control request survived controller shutdown")
33 }
34}
35
36func TestClosingControllerCannotStartMonitors(t *testing.T) {
37 // A service can enter closing before anything initializes its lifetime.
38 // Concurrent late callers must observe cancellation and cannot add work
39 // after closeBackground has begun waiting for the monitors.
40 s := &Server{closing: true}
41 var callers sync.WaitGroup
42 for range 8 {
43 callers.Add(1)
44 go func() {
45 defer callers.Done()
46 s.startMonitors()
47 if s.lifetimeContext().Err() != context.Canceled {
48 t.Error("closing service created a live context")
49 }
50 }()
51 }
52 s.closeBackground()
53 callers.Wait()
54 s.closeBackground()
55}
56
57func TestCloseBackgroundDrainsStartedMonitorLoops(t *testing.T) {
58 s := &Server{}
59 // Keep monitor iterations away from external I/O while exercising the real
60 // loops. Cancellation must wake both loops without waiting for the next tick.
61 s.op.Lock()
62 defer s.op.Unlock()
63 s.startMonitors()
64 done := make(chan struct{})
65 go func() { s.closeBackground(); close(done) }()
66 select {
67 case <-done:
68 case <-time.After(time.Second):
69 t.Fatal("started monitor loops survived shutdown")
70 }
71}
72
73func TestCloseBackgroundWaitsForJobAndRejectsNewWork(t *testing.T) {
74 s := &Server{}
75 started := make(chan struct{})
76 cancelled := make(chan struct{})
77 release := make(chan struct{})
78 done := make(chan struct{})
79 var releaseOnce sync.Once
80 finishJob := func() { releaseOnce.Do(func() { close(release) }) }
81 defer finishJob()
82 _, err := s.jobs.submit(&s.op, "start", 0, nil, nil, func() error {
83 ctx := s.jobs.execution().ctx
84 close(started)
85 <-ctx.Done()
86 close(cancelled)
87 <-release
88 return ctx.Err()
89 })
90 if err != nil {
91 t.Fatal(err)
92 }
93 select {
94 case <-started:
95 case <-time.After(time.Second):
96 t.Fatal("job did not start")
97 }
98 go func() {
99 s.closeBackground()
100 close(done)
101 }()
102 select {
103 case <-cancelled:
104 case <-time.After(time.Second):
105 t.Fatal("shutdown did not cancel the job")
106 }
107 select {
108 case <-done:
109 t.Fatal("shutdown returned before the job released its resources")
110 default:
111 }
112 if _, err := s.jobs.submit(&s.op, "stop", 0, nil, nil, func() error {
113 t.Error("shutdown admitted a new job")
114 return nil
115 }); !errors.Is(err, errJobsClosed) {
116 t.Fatalf("new submission after shutdown: %v", err)
117 }
118 finishJob()
119 select {
120 case <-done:
121 case <-time.After(time.Second):
122 t.Fatal("shutdown did not finish after the job returned")
123 }
124 if job := s.jobs.snapshot(); job.Running || job.Message != "已取消" {
125 t.Fatalf("shutdown left a running job: %+v", job)
126 }
127}