gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/internal/channelz/util_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5   *
     6   * Copyright 2018 gRPC authors.
     7   *
     8   * Licensed under the Apache License, Version 2.0 (the "License");
     9   * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   *     http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   *
    20   */
    21  
    22  // The test in this file should be run in an environment that has go1.10 or later,
    23  // as the function SyscallConn() (required to get socket option) was introduced
    24  // to net.TCPListener in go1.10.
    25  
    26  package channelz_test
    27  
    28  import (
    29  	"net"
    30  	"reflect"
    31  	"syscall"
    32  	"testing"
    33  
    34  	"gitee.com/ks-custle/core-gm/grpc/internal/channelz"
    35  	"gitee.com/ks-custle/core-gm/grpc/internal/grpctest"
    36  	"golang.org/x/sys/unix"
    37  )
    38  
    39  type s struct {
    40  	grpctest.Tester
    41  }
    42  
    43  func Test(t *testing.T) {
    44  	grpctest.RunSubTests(t, s{})
    45  }
    46  
    47  func (s) TestGetSocketOpt(t *testing.T) {
    48  	network, addr := "tcp", ":0"
    49  	ln, err := net.Listen(network, addr)
    50  	if err != nil {
    51  		t.Fatalf("net.Listen(%s,%s) failed with err: %v", network, addr, err)
    52  	}
    53  	defer func(ln net.Listener) {
    54  		_ = ln.Close()
    55  	}(ln)
    56  	go func() {
    57  		_, _ = ln.Accept()
    58  	}()
    59  	conn, _ := net.Dial(network, ln.Addr().String())
    60  	defer func(conn net.Conn) {
    61  		_ = conn.Close()
    62  	}(conn)
    63  	tcpc := conn.(*net.TCPConn)
    64  	raw, err := tcpc.SyscallConn()
    65  	if err != nil {
    66  		t.Fatalf("SyscallConn() failed due to %v", err)
    67  	}
    68  
    69  	l := &unix.Linger{Onoff: 1, Linger: 5}
    70  	recvTimout := &unix.Timeval{Sec: 100}
    71  	sendTimeout := &unix.Timeval{Sec: 8888}
    72  	_ = raw.Control(func(fd uintptr) {
    73  		err := unix.SetsockoptLinger(int(fd), syscall.SOL_SOCKET, syscall.SO_LINGER, l)
    74  		if err != nil {
    75  			t.Fatalf("failed to SetsockoptLinger(%v,%v,%v,%v) due to %v", int(fd), syscall.SOL_SOCKET, syscall.SO_LINGER, l, err)
    76  		}
    77  		err = unix.SetsockoptTimeval(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, recvTimout)
    78  		if err != nil {
    79  			t.Fatalf("failed to SetsockoptTimeval(%v,%v,%v,%v) due to %v", int(fd), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, recvTimout, err)
    80  		}
    81  		err = unix.SetsockoptTimeval(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, sendTimeout)
    82  		if err != nil {
    83  			t.Fatalf("failed to SetsockoptTimeval(%v,%v,%v,%v) due to %v", int(fd), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, sendTimeout, err)
    84  		}
    85  	})
    86  	sktopt := channelz.GetSocketOption(conn)
    87  	if !reflect.DeepEqual(sktopt.Linger, l) {
    88  		t.Fatalf("get socket option linger, want: %v, got %v", l, sktopt.Linger)
    89  	}
    90  	if !reflect.DeepEqual(sktopt.RecvTimeout, recvTimout) {
    91  		t.Logf("get socket option recv timeout, want: %v, got %v, may be caused by system allowing non or partial setting of this value", recvTimout, sktopt.RecvTimeout)
    92  	}
    93  	if !reflect.DeepEqual(sktopt.SendTimeout, sendTimeout) {
    94  		t.Logf("get socket option send timeout, want: %v, got %v, may be caused by system allowing non or partial setting of this value", sendTimeout, sktopt.SendTimeout)
    95  	}
    96  	if sktopt == nil || sktopt.TCPInfo != nil && sktopt.TCPInfo.State != 1 {
    97  		t.Fatalf("TCPInfo.State want 1 (TCP_ESTABLISHED), got %v", sktopt)
    98  	}
    99  
   100  	sktopt = channelz.GetSocketOption(ln)
   101  	if sktopt == nil || sktopt.TCPInfo == nil || sktopt.TCPInfo.State != 10 {
   102  		t.Fatalf("TCPInfo.State want 10 (TCP_LISTEN), got %v", sktopt)
   103  	}
   104  }