session: fix the racy snapshot, hop and idle ttl tests
Signed-off-by: dawn <90008@klbr.net>
15 unmodified lines1617181920212223246 unmodified lines3132333434353637383940414243444546391 unmodified linesExpand all438439440441436442443444445446293 unmodified linesExpand all7407417427437387447457467477488 unmodified lines15 unmodified lines "os/signal" "path/filepath" "strings" "sync" "sync/atomic" "syscall" "time"
"github.com/creack/pty"6 unmodified lines
var ( ErrDetached = errors.New("session detached") b32 = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding) detachedTTL = 24 * time.Hour)
// how long an owner outlives its last viewer; atomic because a test shortens// it while owners from earlier tests are still runningvar detachedTTL atomic.Int64
func init() { detachedTTL.Store(int64(24 * time.Hour)) }
type Session struct { ID string `json:"id"` Host string `json:"host"` Command string `json:"command"`391 unmodified linesExpand all o.colorMu.Lock() delete(o.colorSets, vc) o.colorMu.Unlock() if len(o.viewers) == 0 && !o.closed { o.idleTimer = time.AfterFunc(detachedTTL, o.Close) o.idleTimer = time.AfterFunc(time.Duration(detachedTTL.Load()), o.Close) } o.applySizeLocked()}
293 unmodified linesExpand all fmt.Fprint(owner.ptyFile, termcolor.Reply(i, c)) } } }) owner.idleTimer = time.AfterFunc(detachedTTL, owner.Close) owner.idleTimer = time.AfterFunc(time.Duration(detachedTTL.Load()), owner.Close)
go owner.readPTYLoop() go func() { _ = cmd.Wait() }()
8 unmodified lines82 unmodified lines83848586878887899091929369 unmodified lines16316416516616516716816917017127 unmodified lines19920020120220120220320320420520620787 unmodified lines82 unmodified lines t.Setenv("XDG_RUNTIME_DIR", shortTempDir(t)) sp := startOwner(t, "sh") v := attach(t, sp) defer v.Close() // the tty echoes the typed line, so the marker is split by quotes to // match only once the command has run _ = protocol.WriteFrame(v, protocol.TypeInput, []byte("printf '\\033[?1002h\\033[?1006h'; echo done\n")) _ = protocol.WriteFrame(v, protocol.TypeInput, []byte("printf '\\033[?1002h\\033[?1006h'; echo do''ne\n")) expectFrame(t, v, protocol.TypeOutput, "done") c, err := net.Dial("unix", sp) if err != nil { t.Fatal(err)69 unmodified lines v1, v2 := attach(t, sp), attach(t, sp) defer v1.Close() defer v2.Close()
cmd := "printf '\\033]2;hx main.go\\a\\033]7;file://localhost/custom/dir\\a'; echo shared\n" cmd := "printf '\\033]2;hx main.go\\a\\033]7;file://localhost/custom/dir\\a'; echo sha''red\n" // quoted apart from the tty's echo _ = protocol.WriteFrame(v1, protocol.TypeInput, []byte(cmd)) expectFrame(t, v1, protocol.TypeOutput, "shared") expectFrame(t, v2, protocol.TypeOutput, "shared") if err := SendHop(sp, "remote.123456"); err != nil {27 unmodified lines}
func TestDetachedIdleTTL(t *testing.T) { t.Setenv("XDG_RUNTIME_DIR", shortTempDir(t)) orig := detachedTTL detachedTTL = 80 * time.Millisecond defer func() { detachedTTL = orig }() defer detachedTTL.Store(detachedTTL.Swap(int64(80 * time.Millisecond))) // the swap runs now, the restore on return sp := startOwner(t, "sh", "-c", "sleep 10") v := attach(t, sp) _ = v.Close() time.Sleep(150 * time.Millisecond)87 unmodified lines