channelz_linux_go110_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. testpb "google.golang.org/grpc/test/grpc_testing"
  28. )
  29. func (s) TestCZSocketMetricsSocketOption(t *testing.T) {
  30. envs := []env{tcpClearRREnv, tcpTLSRREnv}
  31. for _, e := range envs {
  32. testCZSocketMetricsSocketOption(t, e)
  33. }
  34. }
  35. func testCZSocketMetricsSocketOption(t *testing.T, e env) {
  36. czCleanup := channelz.NewChannelzStorage()
  37. defer czCleanupWrapper(czCleanup, t)
  38. te := newTest(t, e)
  39. te.startServer(&testServer{security: e.security})
  40. defer te.tearDown()
  41. cc := te.clientConn()
  42. tc := testpb.NewTestServiceClient(cc)
  43. doSuccessfulUnaryCall(tc, t)
  44. time.Sleep(10 * time.Millisecond)
  45. ss, _ := channelz.GetServers(0, 0)
  46. if len(ss) != 1 {
  47. t.Fatalf("There should be one server, not %d", len(ss))
  48. }
  49. if len(ss[0].ListenSockets) != 1 {
  50. t.Fatalf("There should be one listen socket, not %d", len(ss[0].ListenSockets))
  51. }
  52. for id := range ss[0].ListenSockets {
  53. sm := channelz.GetSocket(id)
  54. if sm == nil || sm.SocketData == nil || sm.SocketData.SocketOptions == nil {
  55. t.Fatalf("Unable to get server listen socket options")
  56. }
  57. }
  58. ns, _ := channelz.GetServerSockets(ss[0].ID, 0, 0)
  59. if len(ns) != 1 {
  60. t.Fatalf("There should be one server normal socket, not %d", len(ns))
  61. }
  62. if ns[0] == nil || ns[0].SocketData == nil || ns[0].SocketData.SocketOptions == nil {
  63. t.Fatalf("Unable to get server normal socket options")
  64. }
  65. tchan, _ := channelz.GetTopChannels(0, 0)
  66. if len(tchan) != 1 {
  67. t.Fatalf("There should only be one top channel, not %d", len(tchan))
  68. }
  69. if len(tchan[0].SubChans) != 1 {
  70. t.Fatalf("There should only be one subchannel under top channel %d, not %d", tchan[0].ID, len(tchan[0].SubChans))
  71. }
  72. var id int64
  73. for id = range tchan[0].SubChans {
  74. break
  75. }
  76. sc := channelz.GetSubChannel(id)
  77. if sc == nil {
  78. t.Fatalf("There should only be one socket under subchannel %d, not 0", id)
  79. }
  80. if len(sc.Sockets) != 1 {
  81. t.Fatalf("There should only be one socket under subchannel %d, not %d", sc.ID, len(sc.Sockets))
  82. }
  83. for id = range sc.Sockets {
  84. break
  85. }
  86. skt := channelz.GetSocket(id)
  87. if skt == nil || skt.SocketData == nil || skt.SocketData.SocketOptions == nil {
  88. t.Fatalf("Unable to get client normal socket options")
  89. }
  90. }