chore: add TestDialer to retry when dial fails for local testing

This commit is contained in:
wwqgtxx
2026-04-18 20:36:04 +08:00
parent c39b680c52
commit df6a5d3fe3
11 changed files with 34 additions and 0 deletions
+1
View File
@@ -41,6 +41,7 @@ func testInboundAnyTLS(t *testing.T, inboundOptions inbound.AnyTLSOption, outbou
outboundOptions.Server = addrPort.Addr().String()
outboundOptions.Port = int(addrPort.Port())
outboundOptions.Password = userUUID
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewAnyTLS(outboundOptions)
if !assert.NoError(t, err) {
+23
View File
@@ -58,6 +58,29 @@ func init() {
realityPublickey = base64.RawURLEncoding.EncodeToString(privateKey.PublicKey().Bytes())
}
type TestDialer struct{ dialer C.Dialer }
func (t *TestDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
start:
conn, err := t.dialer.DialContext(ctx, network, address)
if err != nil && ctx.Err() == nil {
// we are testing in local, should never fail, so just retry when ctx not canceled
// it usually happens when the test is running in parallel
goto start
}
return conn, err
}
func (t *TestDialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
return t.dialer.ListenPacket(ctx, network, address, rAddrPort)
}
func NewTestDialer() *TestDialer {
return &TestDialer{dialer: dialer.NewDialer()}
}
var _ C.Dialer = (*TestDialer)(nil)
type TestTunnel struct {
HandleTCPConnFn func(conn net.Conn, metadata *C.Metadata)
HandleUDPPacketFn func(packet C.UDPPacket, metadata *C.Metadata)
+1
View File
@@ -41,6 +41,7 @@ func testInboundHysteria2(t *testing.T, inboundOptions inbound.Hysteria2Option,
outboundOptions.Server = addrPort.Addr().String()
outboundOptions.Port = int(addrPort.Port())
outboundOptions.Password = userUUID
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewHysteria2(outboundOptions)
if !assert.NoError(t, err) {
+2
View File
@@ -237,6 +237,7 @@ func testInboundMieruTCP(t *testing.T, handshakeMode string) {
Password: "password",
HandshakeMode: handshakeMode,
}
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewMieru(outboundOptions)
if !assert.NoError(t, err) {
return
@@ -292,6 +293,7 @@ func testInboundMieruUDP(t *testing.T, handshakeMode string) {
Password: "password",
HandshakeMode: handshakeMode,
}
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewMieru(outboundOptions)
if !assert.NoError(t, err) {
return
+1
View File
@@ -85,6 +85,7 @@ func testInboundShadowSocks0(t *testing.T, inboundOptions inbound.ShadowSocksOpt
outboundOptions.Server = addrPort.Addr().String()
outboundOptions.Port = int(addrPort.Port())
outboundOptions.Password = password
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewShadowSocks(outboundOptions)
if !assert.NoError(t, err) {
+1
View File
@@ -43,6 +43,7 @@ func testInboundSudoku(t *testing.T, inboundOptions inbound.SudokuOption, outbou
outboundOptions.Name = "sudoku_outbound"
outboundOptions.Server = addrPort.Addr().String()
outboundOptions.Port = int(addrPort.Port())
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewSudoku(outboundOptions)
if !assert.NoError(t, err) {
+1
View File
@@ -43,6 +43,7 @@ func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outbou
outboundOptions.Server = addrPort.Addr().String()
outboundOptions.Port = int(addrPort.Port())
outboundOptions.Password = userUUID
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewTrojan(outboundOptions)
if !assert.NoError(t, err) {
+1
View File
@@ -42,6 +42,7 @@ func testInboundTrustTunnel(t *testing.T, inboundOptions inbound.TrustTunnelOpti
outboundOptions.Port = int(addrPort.Port())
outboundOptions.UserName = "test"
outboundOptions.Password = userUUID
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewTrustTunnel(outboundOptions)
if !assert.NoError(t, err) {
+1
View File
@@ -69,6 +69,7 @@ func testInboundTuic0(t *testing.T, inboundOptions inbound.TuicOption, outboundO
outboundOptions.Name = "tuic_outbound"
outboundOptions.Server = addrPort.Addr().String()
outboundOptions.Port = int(addrPort.Port())
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewTuic(outboundOptions)
if !assert.NoError(t, err) {
+1
View File
@@ -44,6 +44,7 @@ func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outbound
outboundOptions.Server = addrPort.Addr().String()
outboundOptions.Port = int(addrPort.Port())
outboundOptions.UUID = userUUID
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewVless(outboundOptions)
if !assert.NoError(t, err) {
+1
View File
@@ -45,6 +45,7 @@ func testInboundVMess(t *testing.T, inboundOptions inbound.VmessOption, outbound
outboundOptions.UUID = userUUID
outboundOptions.AlterID = 0
outboundOptions.Cipher = "auto"
outboundOptions.DialerForAPI = NewTestDialer()
out, err := outbound.NewVmess(outboundOptions)
if !assert.NoError(t, err) {