watch_keepalive_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // TestWatchKeepAlive tests when watch discovers it cannot talk to
  25. // blackholed endpoint, client balancer switches to healthy one.
  26. // TODO: test server-to-client keepalive ping
  27. func TestWatchKeepAlive(t *testing.T) {
  28. defer testutil.AfterTest(t)
  29. clus := integration.NewClusterV3(t, &integration.ClusterConfig{
  30. Size: 2,
  31. GRPCKeepAliveMinTime: 1 * time.Millisecond},
  32. ) // avoid too_many_pings
  33. defer clus.Terminate(t)
  34. ccfg := clientv3.Config{
  35. Endpoints: []string{clus.Members[0].GRPCAddr()},
  36. DialTimeout: 1 * time.Second,
  37. DialKeepAliveTime: 1 * time.Second,
  38. DialKeepAliveTimeout: 500 * time.Millisecond,
  39. }
  40. // gRPC internal implementation related.
  41. pingInterval := ccfg.DialKeepAliveTime + ccfg.DialKeepAliveTimeout
  42. // 3s for slow machine to process watch and reset connections
  43. // TODO: only send healthy endpoint to gRPC so gRPC wont waste time to
  44. // dial for unhealthy endpoint.
  45. // then we can reduce 3s to 1s.
  46. timeout := pingInterval + 3*time.Second
  47. cli, err := clientv3.New(ccfg)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. defer cli.Close()
  52. wch := cli.Watch(context.Background(), "foo", clientv3.WithCreatedNotify())
  53. if _, ok := <-wch; !ok {
  54. t.Fatalf("watch failed on creation")
  55. }
  56. // endpoint can switch to ep[1] when it detects the failure of ep0
  57. cli.SetEndpoints(clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr())
  58. clus.Members[0].Blackhole()
  59. if _, err = clus.Client(1).Put(context.TODO(), "foo", "bar"); err != nil {
  60. t.Fatal(err)
  61. }
  62. select {
  63. case <-wch:
  64. case <-time.After(timeout):
  65. t.Error("took too long to receive watch events")
  66. }
  67. clus.Members[0].Unblackhole()
  68. // waiting for moving ep0 out of unhealthy, so that it can be re-pined.
  69. time.Sleep(ccfg.DialTimeout)
  70. clus.Members[1].Blackhole()
  71. // make sure client0 can connect to member 0 after remove the blackhole.
  72. if _, err = clus.Client(0).Get(context.TODO(), "foo"); err != nil {
  73. t.Fatal(err)
  74. }
  75. if _, err = clus.Client(0).Put(context.TODO(), "foo", "bar1"); err != nil {
  76. t.Fatal(err)
  77. }
  78. select {
  79. case <-wch:
  80. case <-time.After(timeout):
  81. t.Error("took too long to receive watch events")
  82. }
  83. }