balancer_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3
  15. import (
  16. "errors"
  17. "testing"
  18. "time"
  19. "golang.org/x/net/context"
  20. "google.golang.org/grpc"
  21. )
  22. var (
  23. endpoints = []string{"localhost:2379", "localhost:22379", "localhost:32379"}
  24. )
  25. func TestBalancerGetUnblocking(t *testing.T) {
  26. sb := newSimpleBalancer(endpoints)
  27. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  28. t.Errorf("Initialize newSimpleBalancer should have triggered Notify() chan, but it didn't")
  29. }
  30. unblockingOpts := grpc.BalancerGetOptions{BlockingWait: false}
  31. _, _, err := sb.Get(context.Background(), unblockingOpts)
  32. if err != ErrNoAddrAvilable {
  33. t.Errorf("Get() with no up endpoints should return ErrNoAddrAvailable, got: %v", err)
  34. }
  35. down1 := sb.Up(grpc.Address{Addr: endpoints[1]})
  36. if addrs := <-sb.Notify(); len(addrs) != 1 {
  37. t.Errorf("first Up() should have triggered balancer to send the first connected address via Notify chan so that other connections can be closed")
  38. }
  39. down2 := sb.Up(grpc.Address{Addr: endpoints[2]})
  40. addrFirst, putFun, err := sb.Get(context.Background(), unblockingOpts)
  41. if err != nil {
  42. t.Errorf("Get() with up endpoints should success, got %v", err)
  43. }
  44. if addrFirst.Addr != endpoints[1] {
  45. t.Errorf("Get() didn't return expected address, got %v", addrFirst)
  46. }
  47. if putFun == nil {
  48. t.Errorf("Get() returned unexpected nil put function")
  49. }
  50. addrSecond, _, _ := sb.Get(context.Background(), unblockingOpts)
  51. if addrFirst.Addr != addrSecond.Addr {
  52. t.Errorf("Get() didn't return the same address as previous call, got %v and %v", addrFirst, addrSecond)
  53. }
  54. down1(errors.New("error"))
  55. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  56. t.Errorf("closing the only connection should triggered balancer to send the all endpoints via Notify chan so that we can establish a connection")
  57. }
  58. down2(errors.New("error"))
  59. _, _, err = sb.Get(context.Background(), unblockingOpts)
  60. if err != ErrNoAddrAvilable {
  61. t.Errorf("Get() with no up endpoints should return ErrNoAddrAvailable, got: %v", err)
  62. }
  63. }
  64. func TestBalancerGetBlocking(t *testing.T) {
  65. sb := newSimpleBalancer(endpoints)
  66. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  67. t.Errorf("Initialize newSimpleBalancer should have triggered Notify() chan, but it didn't")
  68. }
  69. blockingOpts := grpc.BalancerGetOptions{BlockingWait: true}
  70. ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*100)
  71. _, _, err := sb.Get(ctx, blockingOpts)
  72. if err != context.DeadlineExceeded {
  73. t.Errorf("Get() with no up endpoints should timeout, got %v", err)
  74. }
  75. downC := make(chan func(error), 1)
  76. go func() {
  77. // ensure sb.Up() will be called after sb.Get() to see if Up() releases blocking Get()
  78. time.Sleep(time.Millisecond * 100)
  79. downC <- sb.Up(grpc.Address{Addr: endpoints[1]})
  80. if addrs := <-sb.Notify(); len(addrs) != 1 {
  81. t.Errorf("first Up() should have triggered balancer to send the first connected address via Notify chan so that other connections can be closed")
  82. }
  83. }()
  84. addrFirst, putFun, err := sb.Get(context.Background(), blockingOpts)
  85. if err != nil {
  86. t.Errorf("Get() with up endpoints should success, got %v", err)
  87. }
  88. if addrFirst.Addr != endpoints[1] {
  89. t.Errorf("Get() didn't return expected address, got %v", addrFirst)
  90. }
  91. if putFun == nil {
  92. t.Errorf("Get() returned unexpected nil put function")
  93. }
  94. down1 := <-downC
  95. down2 := sb.Up(grpc.Address{Addr: endpoints[2]})
  96. addrSecond, _, _ := sb.Get(context.Background(), blockingOpts)
  97. if addrFirst.Addr != addrSecond.Addr {
  98. t.Errorf("Get() didn't return the same address as previous call, got %v and %v", addrFirst, addrSecond)
  99. }
  100. down1(errors.New("error"))
  101. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  102. t.Errorf("closing the only connection should triggered balancer to send the all endpoints via Notify chan so that we can establish a connection")
  103. }
  104. down2(errors.New("error"))
  105. ctx, _ = context.WithTimeout(context.Background(), time.Millisecond*100)
  106. _, _, err = sb.Get(ctx, blockingOpts)
  107. if err != context.DeadlineExceeded {
  108. t.Errorf("Get() with no up endpoints should timeout, got %v", err)
  109. }
  110. }