server_shutdown_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. package integration
  15. import (
  16. "bytes"
  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. // TestBalancerUnderServerShutdownWatch expects that watch client
  25. // switch its endpoints when the member of the pinned endpoint fails.
  26. func TestBalancerUnderServerShutdownWatch(t *testing.T) {
  27. defer testutil.AfterTest(t)
  28. clus := integration.NewClusterV3(t, &integration.ClusterConfig{
  29. Size: 3,
  30. SkipCreatingClient: true,
  31. })
  32. defer clus.Terminate(t)
  33. eps := []string{clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr()}
  34. lead := clus.WaitLeader(t)
  35. // pin eps[lead]
  36. watchCli, err := clientv3.New(clientv3.Config{Endpoints: []string{eps[lead]}})
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. defer watchCli.Close()
  41. // wait for eps[lead] to be pinned
  42. waitPinReady(t, watchCli)
  43. // add all eps to list, so that when the original pined one fails
  44. // the client can switch to other available eps
  45. watchCli.SetEndpoints(eps...)
  46. key, val := "foo", "bar"
  47. wch := watchCli.Watch(context.Background(), key, clientv3.WithCreatedNotify())
  48. select {
  49. case <-wch:
  50. case <-time.After(3 * time.Second):
  51. t.Fatal("took too long to create watch")
  52. }
  53. donec := make(chan struct{})
  54. go func() {
  55. defer close(donec)
  56. // switch to others when eps[lead] is shut down
  57. select {
  58. case ev := <-wch:
  59. if werr := ev.Err(); werr != nil {
  60. t.Fatal(werr)
  61. }
  62. if len(ev.Events) != 1 {
  63. t.Fatalf("expected one event, got %+v", ev)
  64. }
  65. if !bytes.Equal(ev.Events[0].Kv.Value, []byte(val)) {
  66. t.Fatalf("expected %q, got %+v", val, ev.Events[0].Kv)
  67. }
  68. case <-time.After(7 * time.Second):
  69. t.Fatal("took too long to receive events")
  70. }
  71. }()
  72. // shut down eps[lead]
  73. clus.Members[lead].Terminate(t)
  74. // writes to eps[lead+1]
  75. putCli, err := clientv3.New(clientv3.Config{Endpoints: []string{eps[(lead+1)%3]}})
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. defer putCli.Close()
  80. for {
  81. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  82. _, err = putCli.Put(ctx, key, val)
  83. cancel()
  84. if err == nil {
  85. break
  86. }
  87. if err == context.DeadlineExceeded {
  88. continue
  89. }
  90. t.Fatal(err)
  91. }
  92. select {
  93. case <-donec:
  94. case <-time.After(5 * time.Second): // enough time for balancer switch
  95. t.Fatal("took too long to receive events")
  96. }
  97. }