channelz_linux_go110_test.go 3.0 KB

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