dial_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "github.com/coreos/etcd/integration"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. "golang.org/x/net/context"
  23. )
  24. // TestDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  25. func TestDialSetEndpointsBeforeFail(t *testing.T) {
  26. testDialSetEndpoints(t, true)
  27. }
  28. func TestDialSetEndpointsAfterFail(t *testing.T) {
  29. testDialSetEndpoints(t, false)
  30. }
  31. // testDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
  32. func testDialSetEndpoints(t *testing.T, setBefore bool) {
  33. defer testutil.AfterTest(t)
  34. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  35. defer clus.Terminate(t)
  36. // get endpoint list
  37. eps := make([]string, 3)
  38. for i := range eps {
  39. eps[i] = clus.Members[i].GRPCAddr()
  40. }
  41. toKill := rand.Intn(len(eps))
  42. cfg := clientv3.Config{Endpoints: []string{eps[toKill]}, DialTimeout: 1 * time.Second}
  43. cli, err := clientv3.New(cfg)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. defer cli.Close()
  48. if setBefore {
  49. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  50. }
  51. // make a dead node
  52. clus.Members[toKill].Stop(t)
  53. clus.WaitLeader(t)
  54. if !setBefore {
  55. cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
  56. }
  57. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
  58. if _, err = cli.Get(ctx, "foo", clientv3.WithSerializable()); err != nil {
  59. t.Fatal(err)
  60. }
  61. cancel()
  62. }
  63. // TestSwitchSetEndpoints ensures SetEndpoints can switch one endpoint
  64. // with a new one that doesn't include original endpoint.
  65. func TestSwitchSetEndpoints(t *testing.T) {
  66. defer testutil.AfterTest(t)
  67. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  68. defer clus.Terminate(t)
  69. // get non partitioned members endpoints
  70. eps := []string{clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr()}
  71. cli := clus.Client(0)
  72. clus.Members[0].InjectPartition(t, clus.Members[1:])
  73. cli.SetEndpoints(eps...)
  74. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  75. defer cancel()
  76. if _, err := cli.Get(ctx, "foo"); err != nil {
  77. t.Fatal(err)
  78. }
  79. }
  80. func TestRejectOldCluster(t *testing.T) {
  81. defer testutil.AfterTest(t)
  82. // 2 endpoints to test multi-endpoint Status
  83. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  84. defer clus.Terminate(t)
  85. cfg := clientv3.Config{
  86. Endpoints: []string{clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr()},
  87. DialTimeout: 5 * time.Second,
  88. RejectOldCluster: true,
  89. }
  90. cli, err := clientv3.New(cfg)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. cli.Close()
  95. }