starnet/errors_kind_test.go
starainrt b5bd7595a1
1. 优化ping功能
2. 新增重试机制
3. 优化错误处理逻辑
2026-03-19 16:42:45 +08:00

50 lines
1.3 KiB
Go

package starnet
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"testing"
)
func TestIsCanceled(t *testing.T) {
if !IsCanceled(context.Canceled) {
t.Fatal("context canceled should be canceled")
}
if !IsCanceled(fmt.Errorf("wrapped: %w", context.Canceled)) {
t.Fatal("wrapped context canceled should be canceled")
}
if IsCanceled(context.DeadlineExceeded) {
t.Fatal("deadline exceeded must not be canceled")
}
}
func TestClassifyError(t *testing.T) {
dnsErr := &net.DNSError{Err: "no such host", Name: "example.invalid", IsNotFound: true}
tlsErr := tls.RecordHeaderError{Msg: "bad tls record"}
tests := []struct {
name string
err error
want ErrorKind
}{
{name: "nil", err: nil, want: ErrorKindNone},
{name: "canceled", err: context.Canceled, want: ErrorKindCanceled},
{name: "proxy", err: errors.New("proxyconnect tcp: dial tcp 127.0.0.1:8080: i/o timeout"), want: ErrorKindProxy},
{name: "dns", err: dnsErr, want: ErrorKindDNS},
{name: "tls", err: tlsErr, want: ErrorKindTLS},
{name: "timeout", err: context.DeadlineExceeded, want: ErrorKindTimeout},
{name: "other", err: errors.New("boom"), want: ErrorKindOther},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ClassifyError(tt.err); got != tt.want {
t.Fatalf("ClassifyError()=%s want=%s err=%v", got, tt.want, tt.err)
}
})
}
}