trpc.group/trpc-go/trpc-go@v1.0.2/transport/tnet/client_transport_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 //go:build linux || freebsd || dragonfly || darwin 15 // +build linux freebsd dragonfly darwin 16 17 package tnet_test 18 19 import ( 20 "fmt" 21 "net" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 26 "trpc.group/trpc-go/trpc-go/errs" 27 "trpc.group/trpc-go/trpc-go/pool/connpool" 28 tnettrans "trpc.group/trpc-go/trpc-go/transport/tnet" 29 ) 30 31 func TestDial(t *testing.T) { 32 l, err := net.Listen("tcp", "localhost:0") 33 assert.Nil(t, err) 34 defer l.Close() 35 36 tests := []struct { 37 name string 38 opts *connpool.DialOptions 39 want net.Conn 40 wantErr assert.ErrorAssertionFunc 41 }{ 42 { 43 name: "empty CACertFile and Network", 44 opts: &connpool.DialOptions{ 45 CACertFile: "", 46 Network: "", 47 }, 48 want: nil, 49 wantErr: func(t assert.TestingT, err error, msg ...interface{}) bool { 50 return assert.Contains(t, err.Error(), "unknown network") 51 }, 52 }, 53 { 54 name: "invalid idle timeout", 55 opts: &connpool.DialOptions{ 56 CACertFile: "", 57 Network: "tcp", 58 Address: l.Addr().String(), 59 IdleTimeout: -1, 60 }, 61 want: nil, 62 wantErr: func(t assert.TestingT, err error, msg ...interface{}) bool { 63 return assert.Contains(t, err.Error(), "delay time is too short") 64 }, 65 }, 66 { 67 name: "wrong CACertFile and TLSServerName ", 68 opts: &connpool.DialOptions{ 69 CACertFile: "xxx", 70 TLSServerName: "xxx", 71 }, 72 want: nil, 73 wantErr: func(t assert.TestingT, err error, msg ...interface{}) bool { 74 return assert.Equal(t, errs.RetClientDecodeFail, errs.Code(err)) && 75 assert.Contains(t, err.Error(), "client dial tnet tls fail") 76 }, 77 }, 78 } 79 for _, tt := range tests { 80 t.Run(tt.name, func(t *testing.T) { 81 got, err := tnettrans.Dial(tt.opts) 82 assert.True(t, tt.wantErr(t, err, fmt.Sprintf("Dial(%v)", tt.opts))) 83 assert.Equalf(t, tt.want, got, "Dial(%v)", tt.opts) 84 }) 85 } 86 }