跳到主要内容

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

浏览文件

switch_test.go

48 行 · 1.0 KB历史下载
1package tap
2
3import (
4 "context"
5 "net"
6 "sync"
7 "testing"
8
9 "github.com/containers/gvisor-tap-vsock/pkg/types"
10 "github.com/stretchr/testify/require"
11)
12
13// readingConn says when it is first read.
14type readingConn struct {
15 net.Conn
16 reading chan struct{}
17 once sync.Once
18}
19
20func (c *readingConn) Read(b []byte) (int, error) {
21 c.once.Do(func() { close(c.reading) })
22 return c.Conn.Read(b)
23}
24
25// A connection that ends because the program cancelled the context is not an
26// error; one that ends by itself is.
27func TestAcceptEndedByContext(t *testing.T) {
28 for _, cancelled := range []bool{true, false} {
29 s := NewSwitch(false)
30 ours, theirs := net.Pipe()
31 conn := &readingConn{Conn: ours, reading: make(chan struct{})}
32 ctx, cancel := context.WithCancel(context.Background())
33 done := make(chan error, 1)
34 go func() { done <- s.Accept(ctx, conn, types.VfkitProtocol) }()
35 <-conn.reading
36 if cancelled {
37 cancel()
38 }
39 theirs.Close()
40 err := <-done
41 cancel()
42 if cancelled {
43 require.NoError(t, err)
44 } else {
45 require.Error(t, err)
46 }
47 }
48}