google.golang.org/grpc@v1.72.2/channelz/service/service_sktopt_test.go (about) 1 //go:build linux && (386 || amd64) 2 // +build linux 3 // +build 386 amd64 4 5 /* 6 * 7 * Copyright 2018 gRPC authors. 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 */ 22 23 // SocketOptions is only supported on linux system. The functions defined in 24 // this file are to parse the socket option field and the test is specifically 25 // to verify the behavior of socket option parsing. 26 27 package service 28 29 import ( 30 "context" 31 "testing" 32 "time" 33 34 "github.com/google/go-cmp/cmp" 35 "golang.org/x/sys/unix" 36 "google.golang.org/grpc/internal/channelz" 37 "google.golang.org/grpc/internal/testutils" 38 "google.golang.org/protobuf/testing/protocmp" 39 "google.golang.org/protobuf/types/known/durationpb" 40 41 channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1" 42 ) 43 44 func (s) TestGetSocketOptions(t *testing.T) { 45 ss := &channelz.Socket{ 46 SocketOptions: &channelz.SocketOptionData{ 47 Linger: &unix.Linger{Onoff: 1, Linger: 2}, 48 RecvTimeout: &unix.Timeval{Sec: 10, Usec: 1}, 49 SendTimeout: &unix.Timeval{}, 50 TCPInfo: &unix.TCPInfo{State: 1}, 51 }, 52 } 53 svr := newCZServer() 54 czServer := channelz.RegisterServer("test svr") 55 defer channelz.RemoveEntry(czServer.ID) 56 id := channelz.RegisterSocket(&channelz.Socket{SocketType: channelz.SocketTypeNormal, RefName: "0", Parent: czServer, SocketOptions: ss.SocketOptions}) 57 defer channelz.RemoveEntry(id.ID) 58 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 59 defer cancel() 60 resp, _ := svr.GetSocket(ctx, &channelzpb.GetSocketRequest{SocketId: id.ID}) 61 { 62 got, want := resp.GetSocket().GetRef(), &channelzpb.SocketRef{SocketId: id.ID, Name: "0"} 63 if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" { 64 t.Fatal("resp.GetSocket() ref (-got +want): ", diff) 65 } 66 } 67 { 68 got := resp.GetSocket().GetData().GetOption() 69 want := []*channelzpb.SocketOption{{ 70 Name: "SO_LINGER", 71 Additional: testutils.MarshalAny( 72 t, 73 &channelzpb.SocketOptionLinger{Active: true, Duration: durationpb.New(2 * time.Second)}, 74 ), 75 }, { 76 Name: "SO_RCVTIMEO", 77 Additional: testutils.MarshalAny( 78 t, 79 &channelzpb.SocketOptionTimeout{Duration: durationpb.New(10*time.Second + time.Microsecond)}, 80 ), 81 }, { 82 Name: "SO_SNDTIMEO", 83 Additional: testutils.MarshalAny( 84 t, 85 &channelzpb.SocketOptionTimeout{Duration: durationpb.New(0)}, 86 ), 87 }, { 88 Name: "TCP_INFO", 89 Additional: testutils.MarshalAny( 90 t, 91 &channelzpb.SocketOptionTcpInfo{TcpiState: 1}, 92 ), 93 }} 94 if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" { 95 t.Fatal("resp.GetSocket() options (-got +want): ", diff) 96 } 97 } 98 }