返回 DeepSeek-Reasonix
command_transport_test.go
根目录 / internal / persistentshell / command_transport_test.go
1 package persistentshell
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "io"
8 "strings"
9 "testing"
10 )
11
12 type stagingConn struct{ write func([]byte) (int, error) }
13
14 func (c stagingConn) Read([]byte) (int, error) { return 0, io.EOF }
15 func (c stagingConn) Write(p []byte) (int, error) { return c.write(p) }
16 func (c stagingConn) Close() error { return nil }
17
18 func TestCommandStagingConsumesSplitAcknowledgementBeforeNextWrite(t *testing.T) {
19 s := &session{pendingRead: make(chan readChunkResult, 4)}
20 writes, executed := 0, false
21 s.conn = stagingConn{write: func(p []byte) (int, error) {
22 if len(s.pendingRead) != 0 {
23 t.Fatal("advanced on echoed source or an earlier acknowledgement")
24 }
25 if strings.Contains(string(p), "; eval -- ") {
26 executed = true
27 return len(p), nil
28 }
29 ack := fmt.Sprintf("S_INPUT_%d", writes*commandWordLimit/4)
30 writes++
31 s.pendingRead <- readChunkResult{data: p}
32 s.pendingRead <- readChunkResult{data: []byte("S_INPUT_OLD\n")}
33 s.pendingRead <- readChunkResult{data: []byte(ack[:3])}
34 s.pendingRead <- readChunkResult{data: []byte(ack[3:] + "\n")}
35 return len(p), nil
36 }}
37 if err := s.writeCommand(t.Context(), strings.Repeat("x", 4096), "S", "E:"); err != nil {
38 t.Fatal(err)
39 }
40 if writes != 32 || !executed {
41 t.Fatalf("stages=%d executed=%v", writes, executed)
42 }
43 }
44
45 func TestCommandStagingCancellationDoesNotExecutePartialCommand(t *testing.T) {
46 ctx, cancel := context.WithCancel(t.Context())
47 defer cancel()
48 s := &session{pendingRead: make(chan readChunkResult, 1)}
49 writes := 0
50 s.conn = stagingConn{write: func(p []byte) (int, error) {
51 writes++
52 if strings.Contains(string(p), "; eval -- ") {
53 t.Fatal("executed an unacknowledged command")
54 }
55 s.pendingRead <- readChunkResult{data: p}
56 cancel()
57 return len(p), nil
58 }}
59 if err := s.writeCommand(ctx, strings.Repeat("x", 4096), "S", "E:"); !errors.Is(err, context.Canceled) {
60 t.Fatalf("cancellation=%v", err)
61 }
62 if writes != 1 {
63 t.Fatalf("wrote %d stages after cancellation", writes)
64 }
65 }
66
66 lines GO