You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

175 lines
5.0 KiB

  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. package test
  19. import (
  20. "context"
  21. "reflect"
  22. "testing"
  23. "time"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/balancer"
  26. "google.golang.org/grpc/connectivity"
  27. "google.golang.org/grpc/credentials"
  28. "google.golang.org/grpc/grpclog"
  29. "google.golang.org/grpc/resolver"
  30. testpb "google.golang.org/grpc/test/grpc_testing"
  31. "google.golang.org/grpc/testdata"
  32. )
  33. const testBalancerName = "testbalancer"
  34. // testBalancer creates one subconn with the first address from resolved
  35. // addresses.
  36. //
  37. // It's used to test options for NewSubConn are applies correctly.
  38. type testBalancer struct {
  39. cc balancer.ClientConn
  40. sc balancer.SubConn
  41. newSubConnOptions balancer.NewSubConnOptions
  42. doneInfo []balancer.DoneInfo
  43. }
  44. func (b *testBalancer) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  45. b.cc = cc
  46. return b
  47. }
  48. func (*testBalancer) Name() string {
  49. return testBalancerName
  50. }
  51. func (b *testBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
  52. // Only create a subconn at the first time.
  53. if err == nil && b.sc == nil {
  54. b.sc, err = b.cc.NewSubConn(addrs, b.newSubConnOptions)
  55. if err != nil {
  56. grpclog.Errorf("testBalancer: failed to NewSubConn: %v", err)
  57. return
  58. }
  59. b.cc.UpdateBalancerState(connectivity.Connecting, &picker{sc: b.sc, bal: b})
  60. b.sc.Connect()
  61. }
  62. }
  63. func (b *testBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  64. grpclog.Infof("testBalancer: HandleSubConnStateChange: %p, %v", sc, s)
  65. if b.sc != sc {
  66. grpclog.Infof("testBalancer: ignored state change because sc is not recognized")
  67. return
  68. }
  69. if s == connectivity.Shutdown {
  70. b.sc = nil
  71. return
  72. }
  73. switch s {
  74. case connectivity.Ready, connectivity.Idle:
  75. b.cc.UpdateBalancerState(s, &picker{sc: sc, bal: b})
  76. case connectivity.Connecting:
  77. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrNoSubConnAvailable, bal: b})
  78. case connectivity.TransientFailure:
  79. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrTransientFailure, bal: b})
  80. }
  81. }
  82. func (b *testBalancer) Close() {
  83. }
  84. type picker struct {
  85. err error
  86. sc balancer.SubConn
  87. bal *testBalancer
  88. }
  89. func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  90. if p.err != nil {
  91. return nil, nil, p.err
  92. }
  93. return p.sc, func(d balancer.DoneInfo) { p.bal.doneInfo = append(p.bal.doneInfo, d) }, nil
  94. }
  95. func (s) TestCredsBundleFromBalancer(t *testing.T) {
  96. balancer.Register(&testBalancer{
  97. newSubConnOptions: balancer.NewSubConnOptions{
  98. CredsBundle: &testCredsBundle{},
  99. },
  100. })
  101. te := newTest(t, env{name: "creds-bundle", network: "tcp", balancer: ""})
  102. te.tapHandle = authHandle
  103. te.customDialOptions = []grpc.DialOption{
  104. grpc.WithBalancerName(testBalancerName),
  105. }
  106. creds, err := credentials.NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
  107. if err != nil {
  108. t.Fatalf("Failed to generate credentials %v", err)
  109. }
  110. te.customServerOptions = []grpc.ServerOption{
  111. grpc.Creds(creds),
  112. }
  113. te.startServer(&testServer{})
  114. defer te.tearDown()
  115. cc := te.clientConn()
  116. tc := testpb.NewTestServiceClient(cc)
  117. if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  118. t.Fatalf("Test failed. Reason: %v", err)
  119. }
  120. }
  121. func (s) TestDoneInfo(t *testing.T) {
  122. for _, e := range listTestEnv() {
  123. testDoneInfo(t, e)
  124. }
  125. }
  126. func testDoneInfo(t *testing.T, e env) {
  127. te := newTest(t, e)
  128. b := &testBalancer{}
  129. balancer.Register(b)
  130. te.customDialOptions = []grpc.DialOption{
  131. grpc.WithBalancerName(testBalancerName),
  132. }
  133. te.userAgent = failAppUA
  134. te.startServer(&testServer{security: e.security})
  135. defer te.tearDown()
  136. cc := te.clientConn()
  137. tc := testpb.NewTestServiceClient(cc)
  138. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  139. defer cancel()
  140. wantErr := detailedError
  141. if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); !reflect.DeepEqual(err, wantErr) {
  142. t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %v", err, wantErr)
  143. }
  144. if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
  145. t.Fatalf("TestService.UnaryCall(%v, _, _, _) = _, %v; want _, <nil>", ctx, err)
  146. }
  147. if len(b.doneInfo) < 1 || !reflect.DeepEqual(b.doneInfo[0].Err, wantErr) {
  148. t.Fatalf("b.doneInfo = %v; want b.doneInfo[0].Err = %v", b.doneInfo, wantErr)
  149. }
  150. if len(b.doneInfo) < 2 || !reflect.DeepEqual(b.doneInfo[1].Trailer, testTrailerMetadata) {
  151. t.Fatalf("b.doneInfo = %v; want b.doneInfo[1].Trailer = %v", b.doneInfo, testTrailerMetadata)
  152. }
  153. }