gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/test/channelz_linux_test.go (about)

     1  /*
     2   *
     3   * Copyright 2018 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // The test in this file should be run in an environment that has go1.10 or later,
    20  // as the function SyscallConn() (required to get socket option) was
    21  // introduced to net.TCPListener in go1.10.
    22  
    23  package test
    24  
    25  import (
    26  	"testing"
    27  	"time"
    28  
    29  	"gitee.com/ks-custle/core-gm/grpc/internal/channelz"
    30  	testpb "gitee.com/ks-custle/core-gm/grpc/test/grpc_testing"
    31  )
    32  
    33  func (s) TestCZSocketMetricsSocketOption(t *testing.T) {
    34  	envs := []env{tcpClearRREnv, tcpTLSRREnv}
    35  	for _, e := range envs {
    36  		testCZSocketMetricsSocketOption(t, e)
    37  	}
    38  }
    39  
    40  func testCZSocketMetricsSocketOption(t *testing.T, e env) {
    41  	czCleanup := channelz.NewChannelzStorage()
    42  	defer czCleanupWrapper(czCleanup, t)
    43  	te := newTest(t, e)
    44  	te.startServer(&testServer{security: e.security})
    45  	defer te.tearDown()
    46  	cc := te.clientConn()
    47  	tc := testpb.NewTestServiceClient(cc)
    48  	doSuccessfulUnaryCall(tc, t)
    49  
    50  	time.Sleep(10 * time.Millisecond)
    51  	ss, _ := channelz.GetServers(0, 0)
    52  	if len(ss) != 1 {
    53  		t.Fatalf("There should be one server, not %d", len(ss))
    54  	}
    55  	if len(ss[0].ListenSockets) != 1 {
    56  		t.Fatalf("There should be one listen socket, not %d", len(ss[0].ListenSockets))
    57  	}
    58  	for id := range ss[0].ListenSockets {
    59  		sm := channelz.GetSocket(id)
    60  		if sm == nil || sm.SocketData == nil || sm.SocketData.SocketOptions == nil {
    61  			t.Fatalf("Unable to get server listen socket options")
    62  		}
    63  	}
    64  	ns, _ := channelz.GetServerSockets(ss[0].ID, 0, 0)
    65  	if len(ns) != 1 {
    66  		t.Fatalf("There should be one server normal socket, not %d", len(ns))
    67  	}
    68  	if ns[0] == nil || ns[0].SocketData == nil || ns[0].SocketData.SocketOptions == nil {
    69  		t.Fatalf("Unable to get server normal socket options")
    70  	}
    71  
    72  	tchan, _ := channelz.GetTopChannels(0, 0)
    73  	if len(tchan) != 1 {
    74  		t.Fatalf("There should only be one top channel, not %d", len(tchan))
    75  	}
    76  	if len(tchan[0].SubChans) != 1 {
    77  		t.Fatalf("There should only be one subchannel under top channel %d, not %d", tchan[0].ID, len(tchan[0].SubChans))
    78  	}
    79  	var id int64
    80  	for id = range tchan[0].SubChans {
    81  		break
    82  	}
    83  	sc := channelz.GetSubChannel(id)
    84  	if sc == nil {
    85  		t.Fatalf("There should only be one socket under subchannel %d, not 0", id)
    86  	}
    87  	if len(sc.Sockets) != 1 {
    88  		t.Fatalf("There should only be one socket under subchannel %d, not %d", sc.ID, len(sc.Sockets))
    89  	}
    90  	for id = range sc.Sockets {
    91  		break
    92  	}
    93  	skt := channelz.GetSocket(id)
    94  	if skt == nil || skt.SocketData == nil || skt.SocketData.SocketOptions == nil {
    95  		t.Fatalf("Unable to get client normal socket options")
    96  	}
    97  }