dial_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 integration
  15. import (
  16. "math/rand"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/clientv3"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/integration"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "golang.org/x/net/context"
  24. )
  25. // TestDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  26. func TestDialSetEndpointsBeforeFail(t *testing.T) {
  27. testDialSetEndpoints(t, true)
  28. }
  29. func TestDialSetEndpointsAfterFail(t *testing.T) {
  30. testDialSetEndpoints(t, false)
  31. }
  32. // testDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  33. func testDialSetEndpoints(t *testing.T, setBefore bool) {
  34. defer testutil.AfterTest(t)
  35. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  36. defer clus.Terminate(t)
  37. // get endpoint list
  38. eps := make([]string, 3)
  39. for i := range eps {
  40. eps[i] = clus.Members[i].GRPCAddr()
  41. }
  42. toKill := rand.Intn(len(eps))
  43. cfg := clientv3.Config{Endpoints: []string{eps[toKill]}, DialTimeout: 1 * time.Second}
  44. cli, err := clientv3.New(cfg)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. defer cli.Close()
  49. if setBefore {
  50. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  51. }
  52. // make a dead node
  53. clus.Members[toKill].Stop(t)
  54. clus.WaitLeader(t)
  55. if !setBefore {
  56. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  57. }
  58. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
  59. if _, err = cli.Get(ctx, "foo", clientv3.WithSerializable()); err != nil {
  60. t.Fatal(err)
  61. }
  62. cancel()
  63. }
  64. // TestSwitchSetEndpoints ensures SetEndpoints can switch one endpoint
  65. // with a new one that doesn't include original endpoint.
  66. func TestSwitchSetEndpoints(t *testing.T) {
  67. defer testutil.AfterTest(t)
  68. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  69. defer clus.Terminate(t)
  70. // get non partitioned members endpoints
  71. eps := []string{clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr()}
  72. cli := clus.Client(0)
  73. clus.Members[0].InjectPartition(t, clus.Members[1:])
  74. cli.SetEndpoints(eps...)
  75. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  76. defer cancel()
  77. if _, err := cli.Get(ctx, "foo"); err != nil {
  78. t.Fatal(err)
  79. }
  80. }
  81. func TestRejectOldCluster(t *testing.T) {
  82. defer testutil.AfterTest(t)
  83. // 2 endpoints to test multi-endpoint Status
  84. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  85. defer clus.Terminate(t)
  86. cfg := clientv3.Config{
  87. Endpoints: []string{clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr()},
  88. DialTimeout: 5 * time.Second,
  89. RejectOldCluster: true,
  90. }
  91. cli, err := clientv3.New(cfg)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. cli.Close()
  96. }
  97. // TestDialForeignEndpoint checks an endpoint that is not registered
  98. // with the balancer can be dialed.
  99. func TestDialForeignEndpoint(t *testing.T) {
  100. defer testutil.AfterTest(t)
  101. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  102. defer clus.Terminate(t)
  103. conn, err := clus.Client(0).Dial(clus.Client(1).Endpoints()[0])
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. defer conn.Close()
  108. // grpc can return a lazy connection that's not connected yet; confirm
  109. // that it can communicate with the cluster.
  110. kvc := clientv3.NewKVFromKVClient(pb.NewKVClient(conn))
  111. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  112. defer cancel()
  113. if _, gerr := kvc.Get(ctx, "abc"); gerr != nil {
  114. t.Fatal(err)
  115. }
  116. }