network_partition_test.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. SkipCreatingClient: true,
  49. })
  50. defer clus.Terminate(t)
  51. // expect pin ep[0]
  52. ccfg := clientv3.Config{
  53. Endpoints: []string{clus.Members[0].GRPCAddr()},
  54. DialTimeout: 3 * time.Second,
  55. DialKeepAliveTime: 2 * time.Second,
  56. DialKeepAliveTimeout: 2 * time.Second,
  57. }
  58. cli, err := clientv3.New(ccfg)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. defer cli.Close()
  63. // add other endpoints for later endpoint switch
  64. cli.SetEndpoints(clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr())
  65. clus.Members[0].InjectPartition(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. // TODO: separate put and get test for error checking.
  74. // we do not really expect ErrTimeout on get.
  75. if err != context.DeadlineExceeded && err != rpctypes.ErrTimeout {
  76. t.Errorf("#%d: expected %v or %v, got %v", i, context.DeadlineExceeded, rpctypes.ErrTimeout, err)
  77. }
  78. // give enough time for endpoint switch
  79. // TODO: remove random sleep by syncing directly with balancer
  80. if i == 0 {
  81. time.Sleep(5 * time.Second)
  82. }
  83. }
  84. if err != nil {
  85. t.Errorf("balancer did not switch in time (%v)", err)
  86. }
  87. }