trpc.group/trpc-go/trpc-go@v1.0.3/http/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 http_test
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	thttp "trpc.group/trpc-go/trpc-go/http"
    28  	"trpc.group/trpc-go/trpc-go/server"
    29  	"trpc.group/trpc-go/trpc-go/transport"
    30  )
    31  
    32  // TestPassedListener tests passing listener.
    33  func TestPassedListener(t *testing.T) {
    34  	ctx := context.Background()
    35  	addr := "127.0.0.1:28084"
    36  	key := "TRPC_TEST_HTTP_PASSED_LISTENER"
    37  	if value := os.Getenv(key); value == "1" {
    38  		time.Sleep(1 * time.Second)
    39  		os.Unsetenv(key)
    40  		// child process, tests whether the listener fd can be got.
    41  		ln, err := transport.GetPassedListener("tcp", addr)
    42  		require.Nil(t, err)
    43  		require.NotNil(t, ln)
    44  		require.Nil(t, ln.(net.Listener).Close())
    45  		return
    46  	}
    47  
    48  	tp := thttp.NewServerTransport(newNoopStdHTTPServer)
    49  	option := transport.WithListenAddress(addr)
    50  	handler := transport.WithHandler(transport.Handler(&h{}))
    51  	err := tp.ListenAndServe(ctx, option, handler)
    52  	require.Nil(t, err)
    53  	os.Setenv(key, "1")
    54  	s := server.Server{}
    55  	os.Args = os.Args[0:1]
    56  	os.Args = append(os.Args, "-test.run", "^TestPassedListener$")
    57  	time.Sleep(time.Millisecond)
    58  	cpid, err := s.StartNewProcess()
    59  	require.Nil(t, err)
    60  
    61  	process, err := os.FindProcess(int(cpid))
    62  	require.Nil(t, err)
    63  	ps, err := process.Wait()
    64  	require.Nil(t, err)
    65  	require.Equal(t, 0, ps.ExitCode())
    66  }