watch_keepalive_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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: 3,
  31. GRPCKeepAliveMinTime: time.Millisecond, // avoid too_many_pings
  32. })
  33. defer clus.Terminate(t)
  34. ccfg := clientv3.Config{
  35. Endpoints: []string{clus.Members[0].GRPCAddr()},
  36. DialTimeout: 3 * time.Second,
  37. DialKeepAliveTime: 2 * time.Second,
  38. DialKeepAliveTimeout: 2 * time.Second,
  39. }
  40. cli, err := clientv3.New(ccfg)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer cli.Close()
  45. wch := cli.Watch(context.Background(), "foo", clientv3.WithCreatedNotify())
  46. if _, ok := <-wch; !ok {
  47. t.Fatalf("watch failed on creation")
  48. }
  49. clus.Members[0].Blackhole()
  50. // expects endpoint switch to ep[1]
  51. cli.SetEndpoints(clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr())
  52. // ep[0] keepalive time-out after DialKeepAliveTime + DialKeepAliveTimeout
  53. // wait extra for processing network error for endpoint switching
  54. timeout := ccfg.DialKeepAliveTime + ccfg.DialKeepAliveTimeout + ccfg.DialTimeout
  55. time.Sleep(timeout)
  56. if _, err = clus.Client(1).Put(context.TODO(), "foo", "bar"); err != nil {
  57. t.Fatal(err)
  58. }
  59. select {
  60. case <-wch:
  61. case <-time.After(5 * time.Second):
  62. t.Fatal("took too long to receive events")
  63. }
  64. clus.Members[0].Unblackhole()
  65. clus.Members[1].Blackhole()
  66. defer clus.Members[1].Unblackhole()
  67. // wait for ep[0] recover, ep[1] fail
  68. time.Sleep(timeout)
  69. if _, err = clus.Client(0).Put(context.TODO(), "foo", "bar"); err != nil {
  70. t.Fatal(err)
  71. }
  72. select {
  73. case <-wch:
  74. case <-time.After(5 * time.Second):
  75. t.Fatal("took too long to receive events")
  76. }
  77. }