trpc.group/trpc-go/trpc-go@v1.0.3/server/server_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 server_test
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	trpc "trpc.group/trpc-go/trpc-go"
    30  	"trpc.group/trpc-go/trpc-go/admin"
    31  	"trpc.group/trpc-go/trpc-go/log"
    32  	"trpc.group/trpc-go/trpc-go/server"
    33  	"trpc.group/trpc-go/trpc-go/transport"
    34  )
    35  
    36  func TestStartNewProcess(t *testing.T) {
    37  	// If the process is started by graceful restart,
    38  	// exit here in case of infinite loop.
    39  	if len(os.Getenv(transport.EnvGraceRestart)) > 0 {
    40  		t.SkipNow()
    41  	}
    42  	s := &server.Server{}
    43  	cfg := &trpc.Config{}
    44  	cfg.Server.Admin.IP = "127.0.0.1"
    45  	cfg.Server.Admin.Port = 9028
    46  	opts := []admin.Option{
    47  		admin.WithVersion(trpc.Version()),
    48  		admin.WithAddr(fmt.Sprintf("%s:%d", cfg.Server.Admin.IP, cfg.Server.Admin.Port)),
    49  		admin.WithTLS(cfg.Server.Admin.EnableTLS),
    50  	}
    51  
    52  	adminService := admin.NewServer(opts...)
    53  	s.AddService(admin.ServiceName, adminService)
    54  
    55  	service := server.New(server.WithAddress("127.0.0.1:9080"),
    56  		server.WithNetwork("tcp"),
    57  		server.WithProtocol("trpc"),
    58  		server.WithServiceName("trpc.test.helloworld.Greeter1"))
    59  
    60  	s.AddService("trpc.test.helloworld.Greeter1", service)
    61  	err := s.Register(nil, nil)
    62  
    63  	impl := &GreeterServerImpl{}
    64  	err = s.Register(&GreeterServerServiceDesc, impl)
    65  	go s.Serve()
    66  	time.Sleep(time.Second * 1)
    67  
    68  	log.Info(os.Environ())
    69  	// The environment variable is not set for parent process.
    70  	// It will be set by the child process started by graceful restart.
    71  	if os.Getenv(transport.EnvGraceRestart) == "" {
    72  		fpid := os.Getpid()
    73  		// graceful restart
    74  		cpid, err := s.StartNewProcess("-test.run=Test[^StartNewProcess$]")
    75  		assert.Nil(t, err)
    76  		assert.NotEqual(t, fpid, cpid)
    77  		t.Logf("fpid:%v, cpid:%v", fpid, cpid)
    78  	}
    79  	// Sleep 10s, let the parent process rewrite test coverage. The child process will exit quickly.
    80  	time.Sleep(time.Second * 10)
    81  	err = s.Close(nil)
    82  	assert.Nil(t, err)
    83  }
    84  
    85  func TestCloseOldListenerDuringHotRestart(t *testing.T) {
    86  	// If the process is started by graceful restart,
    87  	// exit here in case of infinite loop.
    88  	if len(os.Getenv(transport.EnvGraceRestart)) > 0 {
    89  		t.SkipNow()
    90  	}
    91  	s := &server.Server{}
    92  	ln, err := net.Listen("tcp", "127.0.0.1:0")
    93  	require.Nil(t, err)
    94  	service := server.New(
    95  		server.WithNetwork("tcp"),
    96  		server.WithProtocol("trpc"),
    97  		server.WithServiceName("trpc.test.helloworld.Greeter1"),
    98  		server.WithListener(ln),
    99  	)
   100  
   101  	s.AddService("trpc.test.helloworld.Greeter1", service)
   102  	err = s.Register(&GreeterServerServiceDesc, &GreeterServerImpl{})
   103  	go func() {
   104  		err = s.Serve()
   105  		assert.Nil(t, err)
   106  	}()
   107  	time.Sleep(time.Second)
   108  
   109  	log.Info(os.Environ())
   110  	// The environment variable is not set for parent process.
   111  	// It will be set by the child process started by graceful restart.
   112  	if os.Getenv(transport.EnvGraceRestart) == "" {
   113  		fpid := os.Getpid()
   114  		// Graceful restart
   115  		cpid, err := s.StartNewProcess("-test.run=^TestCloseOldListenerDuringHotRestart$")
   116  		require.Nil(t, err)
   117  		require.NotEqual(t, fpid, cpid)
   118  		t.Logf("fpid:%v, cpid:%v", fpid, cpid)
   119  		time.Sleep(time.Second)
   120  		// Child will not be up in this test case, so trying to connect won't work.
   121  		_, err = net.Dial("tcp", ln.Addr().String())
   122  		t.Logf("dial err: %+v", err)
   123  		require.NotNil(t, err)
   124  	}
   125  	// Sleep 1s, let the parent process rewrite test coverage. The child process will exit quickly.
   126  	time.Sleep(time.Second)
   127  	err = s.Close(nil)
   128  	require.Nil(t, err)
   129  }