trpc.group/trpc-go/trpc-go@v1.0.3/transport/server_transport_unix_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 !windows 15 // +build !windows 16 17 package transport_test 18 19 import ( 20 "context" 21 "fmt" 22 "net" 23 "os" 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/require" 29 "trpc.group/trpc-go/trpc-go/transport" 30 ) 31 32 func TestST_UnixDomain(t *testing.T) { 33 // Disable reuse port 34 ctx, cancel := context.WithCancel(context.Background()) 35 t.Cleanup(func() { 36 cancel() 37 time.Sleep(time.Millisecond * 100) // Ensure the unix listener is closed. 38 }) 39 require.Nil(t, transport.NewServerTransport( 40 transport.WithReusePort(false), 41 ).ListenAndServe( 42 ctx, 43 transport.WithListenNetwork("unix"), 44 transport.WithListenAddress(fmt.Sprintf("%s/test.sock", t.TempDir())), 45 transport.WithServerFramerBuilder(&framerBuilder{}), 46 )) 47 48 // Enable reuse port 49 require.Nil(t, transport.NewServerTransport( 50 transport.WithReusePort(true), 51 ).ListenAndServe( 52 ctx, 53 transport.WithListenNetwork("unix"), 54 transport.WithListenAddress(fmt.Sprintf("%s/test.sock", t.TempDir())), 55 transport.WithServerFramerBuilder(&framerBuilder{}), 56 )) 57 } 58 59 func TestGetPassedListenerErr(t *testing.T) { 60 listener, err := net.Listen("tcp", "127.0.0.1:0") 61 assert.Nil(t, err) 62 addr := listener.Addr().String() 63 ln := listener.(*net.TCPListener) 64 file, _ := ln.File() 65 66 _ = os.Setenv(transport.EnvGraceFirstFd, fmt.Sprint(file.Fd())) 67 _ = os.Setenv(transport.EnvGraceRestartFdNum, "1") 68 69 _, err = transport.GetPassedListener("tcp", fmt.Sprintf("localhost:%d", savedListenerPort)) 70 assert.NotNil(t, err) 71 72 // Simulate fd derived from environment. 73 _, err = transport.GetPassedListener("tcp", addr) 74 assert.Nil(t, err) 75 76 _ = os.Setenv(transport.EnvGraceRestart, "true") 77 fb := transport.GetFramerBuilder("trpc") 78 79 st := transport.NewServerTransport(transport.WithReusePort(false)) 80 err = st.ListenAndServe(context.Background(), 81 transport.WithListenNetwork("tcp"), 82 transport.WithListenAddress(addr), 83 transport.WithServerFramerBuilder(fb)) 84 assert.NotNil(t, err) 85 86 err = st.ListenAndServe(context.Background(), 87 transport.WithListenNetwork("udp"), 88 transport.WithListenAddress(addr), 89 transport.WithServerFramerBuilder(fb)) 90 assert.NotNil(t, err) 91 92 _ = os.Setenv(transport.EnvGraceRestart, "") 93 }