picker_wrapper_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/connectivity"
  27. _ "google.golang.org/grpc/grpclog/glogger"
  28. "google.golang.org/grpc/internal/leakcheck"
  29. "google.golang.org/grpc/internal/transport"
  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, opts balancer.PickOptions) (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 TestBlockingPickTimeout(t *testing.T) {
  60. defer leakcheck.Check(t)
  61. bp := newPickerWrapper()
  62. ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
  63. defer cancel()
  64. if _, _, err := bp.pick(ctx, true, balancer.PickOptions{}); err != context.DeadlineExceeded {
  65. t.Errorf("bp.pick returned error %v, want DeadlineExceeded", err)
  66. }
  67. }
  68. func TestBlockingPick(t *testing.T) {
  69. defer leakcheck.Check(t)
  70. bp := newPickerWrapper()
  71. // All goroutines should block because picker is nil in bp.
  72. var finishedCount uint64
  73. for i := goroutineCount; i > 0; i-- {
  74. go func() {
  75. if tr, _, err := bp.pick(context.Background(), true, balancer.PickOptions{}); err != nil || tr != testT {
  76. t.Errorf("bp.pick returned non-nil error: %v", err)
  77. }
  78. atomic.AddUint64(&finishedCount, 1)
  79. }()
  80. }
  81. time.Sleep(50 * time.Millisecond)
  82. if c := atomic.LoadUint64(&finishedCount); c != 0 {
  83. t.Errorf("finished goroutines count: %v, want 0", c)
  84. }
  85. bp.updatePicker(&testingPicker{sc: testSC, maxCalled: goroutineCount})
  86. }
  87. func TestBlockingPickNoSubAvailable(t *testing.T) {
  88. defer leakcheck.Check(t)
  89. bp := newPickerWrapper()
  90. var finishedCount uint64
  91. bp.updatePicker(&testingPicker{err: balancer.ErrNoSubConnAvailable, maxCalled: goroutineCount})
  92. // All goroutines should block because picker returns no sc available.
  93. for i := goroutineCount; i > 0; i-- {
  94. go func() {
  95. if tr, _, err := bp.pick(context.Background(), true, balancer.PickOptions{}); err != nil || tr != testT {
  96. t.Errorf("bp.pick returned non-nil error: %v", err)
  97. }
  98. atomic.AddUint64(&finishedCount, 1)
  99. }()
  100. }
  101. time.Sleep(50 * time.Millisecond)
  102. if c := atomic.LoadUint64(&finishedCount); c != 0 {
  103. t.Errorf("finished goroutines count: %v, want 0", c)
  104. }
  105. bp.updatePicker(&testingPicker{sc: testSC, maxCalled: goroutineCount})
  106. }
  107. func TestBlockingPickTransientWaitforready(t *testing.T) {
  108. defer leakcheck.Check(t)
  109. bp := newPickerWrapper()
  110. bp.updatePicker(&testingPicker{err: balancer.ErrTransientFailure, maxCalled: goroutineCount})
  111. var finishedCount uint64
  112. // All goroutines should block because picker returns transientFailure and
  113. // picks are not failfast.
  114. for i := goroutineCount; i > 0; i-- {
  115. go func() {
  116. if tr, _, err := bp.pick(context.Background(), false, balancer.PickOptions{}); err != nil || tr != testT {
  117. t.Errorf("bp.pick returned non-nil error: %v", err)
  118. }
  119. atomic.AddUint64(&finishedCount, 1)
  120. }()
  121. }
  122. time.Sleep(time.Millisecond)
  123. if c := atomic.LoadUint64(&finishedCount); c != 0 {
  124. t.Errorf("finished goroutines count: %v, want 0", c)
  125. }
  126. bp.updatePicker(&testingPicker{sc: testSC, maxCalled: goroutineCount})
  127. }
  128. func TestBlockingPickSCNotReady(t *testing.T) {
  129. defer leakcheck.Check(t)
  130. bp := newPickerWrapper()
  131. bp.updatePicker(&testingPicker{sc: testSCNotReady, maxCalled: goroutineCount})
  132. var finishedCount uint64
  133. // All goroutines should block because sc is not ready.
  134. for i := goroutineCount; i > 0; i-- {
  135. go func() {
  136. if tr, _, err := bp.pick(context.Background(), true, balancer.PickOptions{}); err != nil || tr != testT {
  137. t.Errorf("bp.pick returned non-nil error: %v", err)
  138. }
  139. atomic.AddUint64(&finishedCount, 1)
  140. }()
  141. }
  142. time.Sleep(time.Millisecond)
  143. if c := atomic.LoadUint64(&finishedCount); c != 0 {
  144. t.Errorf("finished goroutines count: %v, want 0", c)
  145. }
  146. bp.updatePicker(&testingPicker{sc: testSC, maxCalled: goroutineCount})
  147. }