picker_wrapper_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. *
  3. * Copyright 2017 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 grpc
  19. import (
  20. "context"
  21. "fmt"
  22. "sync/atomic"
  23. "testing"
  24. "time"
  25. "google.golang.org/grpc/balancer"
  26. "google.golang.org/grpc/codes"
  27. "google.golang.org/grpc/connectivity"
  28. "google.golang.org/grpc/internal/transport"
  29. "google.golang.org/grpc/status"
  30. )
  31. const goroutineCount = 5
  32. var (
  33. testT = &testTransport{}
  34. testSC = &acBalancerWrapper{ac: &addrConn{
  35. state: connectivity.Ready,
  36. transport: testT,
  37. }}
  38. testSCNotReady = &acBalancerWrapper{ac: &addrConn{
  39. state: connectivity.TransientFailure,
  40. }}
  41. )
  42. type testTransport struct {
  43. transport.ClientTransport
  44. }
  45. type testingPicker struct {
  46. err error
  47. sc balancer.SubConn
  48. maxCalled int64
  49. }
  50. func (p *testingPicker) Pick(ctx context.Context, info balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error) {
  51. if atomic.AddInt64(&p.maxCalled, -1) < 0 {
  52. return nil, nil, fmt.Errorf("pick called to many times (> goroutineCount)")
  53. }
  54. if p.err != nil {
  55. return nil, nil, p.err
  56. }
  57. return p.sc, nil, nil
  58. }
  59. func (s) TestBlockingPickTimeout(t *testing.T) {
  60. bp := newPickerWrapper()
  61. ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
  62. defer cancel()
  63. if _, _, err := bp.pick(ctx, true, balancer.PickInfo{}); status.Code(err) != codes.DeadlineExceeded {
  64. t.Errorf("bp.pick returned error %v, want DeadlineExceeded", err)
  65. }
  66. }
  67. func (s) TestBlockingPick(t *testing.T) {
  68. bp := newPickerWrapper()
  69. // All goroutines should block because picker is nil in bp.
  70. var finishedCount uint64
  71. for i := goroutineCount; i > 0; i-- {
  72. go func() {
  73. if tr, _, err := bp.pick(context.Background(), true, balancer.PickInfo{}); err != nil || tr != testT {
  74. t.Errorf("bp.pick returned non-nil error: %v", err)
  75. }
  76. atomic.AddUint64(&finishedCount, 1)
  77. }()
  78. }
  79. time.Sleep(50 * time.Millisecond)
  80. if c := atomic.LoadUint64(&finishedCount); c != 0 {
  81. t.Errorf("finished goroutines count: %v, want 0", c)
  82. }
  83. bp.updatePicker(&testingPicker{sc: testSC, maxCalled: goroutineCount})
  84. }
  85. func (s) TestBlockingPickNoSubAvailable(t *testing.T) {
  86. bp := newPickerWrapper()
  87. var finishedCount uint64
  88. bp.updatePicker(&testingPicker{err: balancer.ErrNoSubConnAvailable, maxCalled: goroutineCount})
  89. // All goroutines should block because picker returns no sc available.
  90. for i := goroutineCount; i > 0; i-- {
  91. go func() {
  92. if tr, _, err := bp.pick(context.Background(), true, balancer.PickInfo{}); err != nil || tr != testT {
  93. t.Errorf("bp.pick returned non-nil error: %v", err)
  94. }
  95. atomic.AddUint64(&finishedCount, 1)
  96. }()
  97. }
  98. time.Sleep(50 * time.Millisecond)
  99. if c := atomic.LoadUint64(&finishedCount); c != 0 {
  100. t.Errorf("finished goroutines count: %v, want 0", c)
  101. }
  102. bp.updatePicker(&testingPicker{sc: testSC, maxCalled: goroutineCount})
  103. }
  104. func (s) TestBlockingPickTransientWaitforready(t *testing.T) {
  105. bp := newPickerWrapper()
  106. bp.updatePicker(&testingPicker{err: balancer.ErrTransientFailure, maxCalled: goroutineCount})
  107. var finishedCount uint64
  108. // All goroutines should block because picker returns transientFailure and
  109. // picks are not failfast.
  110. for i := goroutineCount; i > 0; i-- {
  111. go func() {
  112. if tr, _, err := bp.pick(context.Background(), false, balancer.PickInfo{}); err != nil || tr != testT {
  113. t.Errorf("bp.pick returned non-nil error: %v", err)
  114. }
  115. atomic.AddUint64(&finishedCount, 1)
  116. }()
  117. }
  118. time.Sleep(time.Millisecond)
  119. if c := atomic.LoadUint64(&finishedCount); c != 0 {
  120. t.Errorf("finished goroutines count: %v, want 0", c)
  121. }
  122. bp.updatePicker(&testingPicker{sc: testSC, maxCalled: goroutineCount})
  123. }
  124. func (s) TestBlockingPickSCNotReady(t *testing.T) {
  125. bp := newPickerWrapper()
  126. bp.updatePicker(&testingPicker{sc: testSCNotReady, maxCalled: goroutineCount})
  127. var finishedCount uint64
  128. // All goroutines should block because sc is not ready.
  129. for i := goroutineCount; i > 0; i-- {
  130. go func() {
  131. if tr, _, err := bp.pick(context.Background(), true, balancer.PickInfo{}); err != nil || tr != testT {
  132. t.Errorf("bp.pick returned non-nil error: %v", err)
  133. }
  134. atomic.AddUint64(&finishedCount, 1)
  135. }()
  136. }
  137. time.Sleep(time.Millisecond)
  138. if c := atomic.LoadUint64(&finishedCount); c != 0 {
  139. t.Errorf("finished goroutines count: %v, want 0", c)
  140. }
  141. bp.updatePicker(&testingPicker{sc: testSC, maxCalled: goroutineCount})
  142. }