trpc.group/trpc-go/trpc-go@v1.0.3/admin/admin_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 admin 18 19 import ( 20 "fmt" 21 "os" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 26 "trpc.group/trpc-go/trpc-go/transport" 27 ) 28 29 func TestReuseListener(t *testing.T) { 30 value, ok := os.LookupEnv(transport.EnvGraceRestart) 31 t.Cleanup(func() { 32 if ok { 33 if err := os.Setenv(transport.EnvGraceRestart, value); err != nil { 34 t.Log(err) 35 } 36 } else { 37 if err := os.Unsetenv(transport.EnvGraceRestart); err != nil { 38 t.Log(err) 39 } 40 } 41 }) 42 43 // listen success and save listener 44 tln := mustListenTCP(t) 45 t.Cleanup(func() { 46 if err := tln.Close(); err != nil { 47 t.Log(err) 48 } 49 }) 50 file, err := tln.File() 51 assert.Nil(t, err) 52 assert.NotNil(t, file) 53 54 // reuse saved listener 55 err = os.Setenv(transport.EnvGraceRestart, "1") 56 assert.Nil(t, err) 57 err = os.Setenv(transport.EnvGraceFirstFd, fmt.Sprint(file.Fd())) 58 assert.Nil(t, err) 59 err = os.Setenv(transport.EnvGraceRestartFdNum, "1") 60 assert.Nil(t, err) 61 62 s := NewServer() 63 ln1, err := s.listen("tcp", tln.Addr().String()) 64 assert.Nil(t, err) 65 assert.Equal(t, tln.Addr(), ln1.Addr()) 66 t.Cleanup(func() { 67 if err := ln1.Close(); err != nil { 68 t.Log(err) 69 } 70 }) 71 72 // listen fail on other addr if enable grace restart 73 ln2, err := s.listen("tcp", testAddress) 74 assert.NotNil(t, err) 75 assert.Nil(t, ln2) 76 }