balancer_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. unblockingOpts := grpc.BalancerGetOptions{BlockingWait: false}
  28. _, _, err := sb.Get(context.Background(), unblockingOpts)
  29. if err != ErrNoAddrAvilable {
  30. t.Errorf("Get() with no up endpoints should return ErrNoAddrAvailable, got: %v", err)
  31. }
  32. down1 := sb.Up(grpc.Address{Addr: endpoints[1]})
  33. down2 := sb.Up(grpc.Address{Addr: endpoints[2]})
  34. addrFirst, putFun, err := sb.Get(context.Background(), unblockingOpts)
  35. if err != nil {
  36. t.Errorf("Get() with up endpoints should success, got %v", err)
  37. }
  38. if addrFirst.Addr != endpoints[1] {
  39. t.Errorf("Get() didn't return expected address, got %v", addrFirst)
  40. }
  41. if putFun == nil {
  42. t.Errorf("Get() returned unexpected nil put function")
  43. }
  44. addrSecond, _, _ := sb.Get(context.Background(), unblockingOpts)
  45. if addrFirst.Addr != addrSecond.Addr {
  46. t.Errorf("Get() didn't return the same address as previous call, got %v and %v", addrFirst, addrSecond)
  47. }
  48. down1(errors.New("error"))
  49. down2(errors.New("error"))
  50. _, _, err = sb.Get(context.Background(), unblockingOpts)
  51. if err != ErrNoAddrAvilable {
  52. t.Errorf("Get() with no up endpoints should return ErrNoAddrAvailable, got: %v", err)
  53. }
  54. }
  55. func TestBalancerGetBlocking(t *testing.T) {
  56. sb := newSimpleBalancer(endpoints)
  57. blockingOpts := grpc.BalancerGetOptions{BlockingWait: true}
  58. ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*100)
  59. _, _, err := sb.Get(ctx, blockingOpts)
  60. if err != context.DeadlineExceeded {
  61. t.Errorf("Get() with no up endpoints should timeout, got %v", err)
  62. }
  63. downC := make(chan func(error), 1)
  64. go func() {
  65. // ensure sb.Up() will be called after sb.Get() to see if Up() releases blocking Get()
  66. time.Sleep(time.Millisecond * 100)
  67. downC <- sb.Up(grpc.Address{Addr: endpoints[1]})
  68. }()
  69. addrFirst, putFun, err := sb.Get(context.Background(), blockingOpts)
  70. if err != nil {
  71. t.Errorf("Get() with up endpoints should success, got %v", err)
  72. }
  73. if addrFirst.Addr != endpoints[1] {
  74. t.Errorf("Get() didn't return expected address, got %v", addrFirst)
  75. }
  76. if putFun == nil {
  77. t.Errorf("Get() returned unexpected nil put function")
  78. }
  79. down1 := <-downC
  80. down2 := sb.Up(grpc.Address{Addr: endpoints[2]})
  81. addrSecond, _, _ := sb.Get(context.Background(), blockingOpts)
  82. if addrFirst.Addr != addrSecond.Addr {
  83. t.Errorf("Get() didn't return the same address as previous call, got %v and %v", addrFirst, addrSecond)
  84. }
  85. down1(errors.New("error"))
  86. down2(errors.New("error"))
  87. ctx, _ = context.WithTimeout(context.Background(), time.Millisecond*100)
  88. _, _, err = sb.Get(ctx, blockingOpts)
  89. if err != context.DeadlineExceeded {
  90. t.Errorf("Get() with no up endpoints should timeout, got %v", err)
  91. }
  92. }