diff --git a/channelz/service/service_nonlinux_test.go b/channelz/service/service_nonlinux_test.go deleted file mode 100644 index 734f32aa..00000000 --- a/channelz/service/service_nonlinux_test.go +++ /dev/null @@ -1,77 +0,0 @@ -// +build !linux appengine - -/* - * - * Copyright 2018 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -// Non-linux system does not support socket option. Therefore, the function -// socketProtoToStruct defined in this file skips the parsing of socket option field. - -package service - -import ( - "github.com/golang/protobuf/ptypes" - channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1" -) - -func socketProtoToStruct(s *channelzpb.Socket) *dummySocket { - ds := &dummySocket{} - pdata := s.GetData() - ds.streamsStarted = pdata.GetStreamsStarted() - ds.streamsSucceeded = pdata.GetStreamsSucceeded() - ds.streamsFailed = pdata.GetStreamsFailed() - ds.messagesSent = pdata.GetMessagesSent() - ds.messagesReceived = pdata.GetMessagesReceived() - ds.keepAlivesSent = pdata.GetKeepAlivesSent() - if t, err := ptypes.Timestamp(pdata.GetLastLocalStreamCreatedTimestamp()); err == nil { - if !t.Equal(emptyTime) { - ds.lastLocalStreamCreatedTimestamp = t - } - } - if t, err := ptypes.Timestamp(pdata.GetLastRemoteStreamCreatedTimestamp()); err == nil { - if !t.Equal(emptyTime) { - ds.lastRemoteStreamCreatedTimestamp = t - } - } - if t, err := ptypes.Timestamp(pdata.GetLastMessageSentTimestamp()); err == nil { - if !t.Equal(emptyTime) { - ds.lastMessageSentTimestamp = t - } - } - if t, err := ptypes.Timestamp(pdata.GetLastMessageReceivedTimestamp()); err == nil { - if !t.Equal(emptyTime) { - ds.lastMessageReceivedTimestamp = t - } - } - if v := pdata.GetLocalFlowControlWindow(); v != nil { - ds.localFlowControlWindow = v.Value - } - if v := pdata.GetRemoteFlowControlWindow(); v != nil { - ds.remoteFlowControlWindow = v.Value - } - if v := s.GetSecurity(); v != nil { - ds.security = protoToSecurity(v) - } - if local := s.GetLocal(); local != nil { - ds.localAddr = protoToAddr(local) - } - if remote := s.GetRemote(); remote != nil { - ds.remoteAddr = protoToAddr(remote) - } - ds.remoteName = s.GetRemoteName() - return ds -} diff --git a/channelz/service/service_linux_test.go b/channelz/service/service_sktopt_test.go similarity index 75% rename from channelz/service/service_linux_test.go rename to channelz/service/service_sktopt_test.go index bd2ec1c7..894f57d4 100644 --- a/channelz/service/service_linux_test.go +++ b/channelz/service/service_sktopt_test.go @@ -1,4 +1,5 @@ // +build linux,!appengine +// +build 386 amd64 /* * @@ -37,6 +38,12 @@ import ( "google.golang.org/grpc/internal/channelz" ) +func init() { + // Assign protoToSocketOption to protoToSocketOpt in order to enable socket option + // data conversion from proto message to channelz defined struct. + protoToSocketOpt = protoToSocketOption +} + func convertToDuration(d *durpb.Duration) (sec int64, usec int64) { if d != nil { if dur, err := ptypes.Duration(d); err == nil { @@ -117,57 +124,6 @@ func protoToSocketOption(skopts []*channelzpb.SocketOption) *channelz.SocketOpti return skdata } -func socketProtoToStruct(s *channelzpb.Socket) *dummySocket { - ds := &dummySocket{} - pdata := s.GetData() - ds.streamsStarted = pdata.GetStreamsStarted() - ds.streamsSucceeded = pdata.GetStreamsSucceeded() - ds.streamsFailed = pdata.GetStreamsFailed() - ds.messagesSent = pdata.GetMessagesSent() - ds.messagesReceived = pdata.GetMessagesReceived() - ds.keepAlivesSent = pdata.GetKeepAlivesSent() - if t, err := ptypes.Timestamp(pdata.GetLastLocalStreamCreatedTimestamp()); err == nil { - if !t.Equal(emptyTime) { - ds.lastLocalStreamCreatedTimestamp = t - } - } - if t, err := ptypes.Timestamp(pdata.GetLastRemoteStreamCreatedTimestamp()); err == nil { - if !t.Equal(emptyTime) { - ds.lastRemoteStreamCreatedTimestamp = t - } - } - if t, err := ptypes.Timestamp(pdata.GetLastMessageSentTimestamp()); err == nil { - if !t.Equal(emptyTime) { - ds.lastMessageSentTimestamp = t - } - } - if t, err := ptypes.Timestamp(pdata.GetLastMessageReceivedTimestamp()); err == nil { - if !t.Equal(emptyTime) { - ds.lastMessageReceivedTimestamp = t - } - } - if v := pdata.GetLocalFlowControlWindow(); v != nil { - ds.localFlowControlWindow = v.Value - } - if v := pdata.GetRemoteFlowControlWindow(); v != nil { - ds.remoteFlowControlWindow = v.Value - } - if v := pdata.GetOption(); v != nil { - ds.socketOptions = protoToSocketOption(v) - } - if v := s.GetSecurity(); v != nil { - ds.security = protoToSecurity(v) - } - if local := s.GetLocal(); local != nil { - ds.localAddr = protoToAddr(local) - } - if remote := s.GetRemote(); remote != nil { - ds.remoteAddr = protoToAddr(remote) - } - ds.remoteName = s.GetRemoteName() - return ds -} - func TestGetSocketOptions(t *testing.T) { channelz.NewChannelzStorage() ss := []*dummySocket{ diff --git a/channelz/service/service_test.go b/channelz/service/service_test.go index 8f5642ff..10e00e8c 100644 --- a/channelz/service/service_test.go +++ b/channelz/service/service_test.go @@ -38,6 +38,13 @@ func init() { channelz.TurnOn() } +type protoToSocketOptFunc func([]*channelzpb.SocketOption) *channelz.SocketOptionData + +// protoToSocketOpt is used in function socketProtoToStruct to extract socket option +// data from unmarshaled proto message. +// It is only defined under linux, non-appengine environment on x86 architecture. +var protoToSocketOpt protoToSocketOptFunc + // emptyTime is used for detecting unset value of time.Time type. // For go1.7 and earlier, ptypes.Timestamp will fill in the loc field of time.Time // with &utcLoc. However zero value of a time.Time type value loc field is nil. @@ -166,6 +173,57 @@ func serverProtoToStruct(s *channelzpb.Server) *dummyServer { return ds } +func socketProtoToStruct(s *channelzpb.Socket) *dummySocket { + ds := &dummySocket{} + pdata := s.GetData() + ds.streamsStarted = pdata.GetStreamsStarted() + ds.streamsSucceeded = pdata.GetStreamsSucceeded() + ds.streamsFailed = pdata.GetStreamsFailed() + ds.messagesSent = pdata.GetMessagesSent() + ds.messagesReceived = pdata.GetMessagesReceived() + ds.keepAlivesSent = pdata.GetKeepAlivesSent() + if t, err := ptypes.Timestamp(pdata.GetLastLocalStreamCreatedTimestamp()); err == nil { + if !t.Equal(emptyTime) { + ds.lastLocalStreamCreatedTimestamp = t + } + } + if t, err := ptypes.Timestamp(pdata.GetLastRemoteStreamCreatedTimestamp()); err == nil { + if !t.Equal(emptyTime) { + ds.lastRemoteStreamCreatedTimestamp = t + } + } + if t, err := ptypes.Timestamp(pdata.GetLastMessageSentTimestamp()); err == nil { + if !t.Equal(emptyTime) { + ds.lastMessageSentTimestamp = t + } + } + if t, err := ptypes.Timestamp(pdata.GetLastMessageReceivedTimestamp()); err == nil { + if !t.Equal(emptyTime) { + ds.lastMessageReceivedTimestamp = t + } + } + if v := pdata.GetLocalFlowControlWindow(); v != nil { + ds.localFlowControlWindow = v.Value + } + if v := pdata.GetRemoteFlowControlWindow(); v != nil { + ds.remoteFlowControlWindow = v.Value + } + if v := pdata.GetOption(); v != nil && protoToSocketOpt != nil { + ds.socketOptions = protoToSocketOpt(v) + } + if v := s.GetSecurity(); v != nil { + ds.security = protoToSecurity(v) + } + if local := s.GetLocal(); local != nil { + ds.localAddr = protoToAddr(local) + } + if remote := s.GetRemote(); remote != nil { + ds.remoteAddr = protoToAddr(remote) + } + ds.remoteName = s.GetRemoteName() + return ds +} + func protoToSecurity(protoSecurity *channelzpb.Security) credentials.ChannelzSecurityValue { switch v := protoSecurity.Model.(type) { case *channelzpb.Security_Tls_: diff --git a/channelz/service/util_386_test.go b/channelz/service/util_sktopt_386_test.go similarity index 100% rename from channelz/service/util_386_test.go rename to channelz/service/util_sktopt_386_test.go diff --git a/channelz/service/util_amd64_test.go b/channelz/service/util_sktopt_amd64_test.go similarity index 100% rename from channelz/service/util_amd64_test.go rename to channelz/service/util_sktopt_amd64_test.go