Files
mc-gateway/cmd/kcp/main_test.go
tursom f508ecc1b9
Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
docs: 补充中文代码注释
2026-06-27 20:15:29 +08:00

97 lines
1.9 KiB
Go

// cmd/kcp/main_test.go 包含用于约束 kcp 行为的测试。
package main
import (
"bytes"
"errors"
"io"
"net"
"strings"
"sync"
"testing"
"time"
)
func TestCopyData(t *testing.T) {
var dst bytes.Buffer
var wg sync.WaitGroup
wg.Add(1)
copyData(strings.NewReader("payload"), &dst, &wg)
wg.Wait()
if got := dst.String(); got != "payload" {
t.Fatalf("copyData() wrote %q, want payload", got)
}
}
func TestCopyDataIgnoresEOFAndLogsOtherErrors(t *testing.T) {
copyData(strings.NewReader(""), io.Discard, nil)
copyData(errorReader{err: errors.New("read failed")}, io.Discard, nil)
}
func TestSetSocketOptions(t *testing.T) {
client, server := net.Pipe()
defer client.Close()
defer server.Close()
setSocketOptions(client)
}
func TestSetSocketOptionsTCPConn(t *testing.T) {
client, server := newKcpTestTCPConnPair(t)
defer client.Close()
defer server.Close()
setSocketOptions(client)
setSocketOptions(server)
}
type errorReader struct {
err error
}
func (r errorReader) Read([]byte) (int, error) {
return 0, r.err
}
func newKcpTestTCPConnPair(t *testing.T) (*net.TCPConn, *net.TCPConn) {
t.Helper()
listener, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")})
if err != nil {
t.Fatalf("ListenTCP() error = %v", err)
}
defer listener.Close()
accepted := make(chan *net.TCPConn, 1)
errCh := make(chan error, 1)
go func() {
conn, err := listener.AcceptTCP()
if err != nil {
errCh <- err
return
}
accepted <- conn
}()
client, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr))
if err != nil {
t.Fatalf("DialTCP() error = %v", err)
}
select {
case server := <-accepted:
return client, server
case err := <-errCh:
client.Close()
t.Fatalf("AcceptTCP() error = %v", err)
case <-time.After(2 * time.Second):
client.Close()
t.Fatal("timed out waiting for TCP accept")
}
return nil, nil
}