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