network_partition_test.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 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. // +build !cluster_proxy
  15. package integration
  16. import (
  17. "context"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/integration"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. )
  24. // TestNetworkPartitionBalancerPut tests when one member becomes isolated,
  25. // first Put request fails, and following retry succeeds with client balancer
  26. // switching to others.
  27. func TestNetworkPartitionBalancerPut(t *testing.T) {
  28. testNetworkPartitionBalancer(t, func(cli *clientv3.Client, ctx context.Context) error {
  29. _, err := cli.Put(ctx, "a", "b")
  30. return err
  31. })
  32. }
  33. // TestNetworkPartitionBalancerGet tests when one member becomes isolated,
  34. // first Get request fails, and following retry succeeds with client balancer
  35. // switching to others.
  36. func TestNetworkPartitionBalancerGet(t *testing.T) {
  37. testNetworkPartitionBalancer(t, func(cli *clientv3.Client, ctx context.Context) error {
  38. _, err := cli.Get(ctx, "a")
  39. return err
  40. })
  41. }
  42. func testNetworkPartitionBalancer(t *testing.T, op func(*clientv3.Client, context.Context) error) {
  43. defer testutil.AfterTest(t)
  44. clus := integration.NewClusterV3(t, &integration.ClusterConfig{
  45. Size: 3,
  46. GRPCKeepAliveMinTime: time.Millisecond, // avoid too_many_pings
  47. })
  48. defer clus.Terminate(t)
  49. // expect pin ep[0]
  50. ccfg := clientv3.Config{
  51. Endpoints: []string{clus.Members[0].GRPCAddr()},
  52. DialTimeout: 3 * time.Second,
  53. DialKeepAliveTime: 2 * time.Second,
  54. DialKeepAliveTimeout: 2 * time.Second,
  55. }
  56. cli, err := clientv3.New(ccfg)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. defer cli.Close()
  61. // add other endpoints for later endpoint switch
  62. cli.SetEndpoints(clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr(), clus.Members[1].GRPCAddr())
  63. time.Sleep(3 * time.Second)
  64. clus.Members[0].InjectPartition(t, clus.Members[1:])
  65. defer clus.Members[0].RecoverPartition(t, clus.Members[1:])
  66. for i := 0; i < 2; i++ {
  67. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  68. err = op(cli, ctx)
  69. cancel()
  70. if err == nil {
  71. break
  72. }
  73. if err != context.DeadlineExceeded {
  74. t.Fatalf("#%d: expected %v, got %v", i, context.DeadlineExceeded, err)
  75. }
  76. // give enough time for endpoint switch
  77. // TODO: remove random sleep by syncing directly with balancer
  78. if i == 0 {
  79. time.Sleep(5 * time.Second)
  80. }
  81. }
  82. if err != nil {
  83. t.Fatalf("balancer did not switch in time (%v)", err)
  84. }
  85. }