server_shutdown_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
  43. _, err = watchCli.Get(ctx, "foo")
  44. cancel()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. // add all eps to list, so that when the original pined one fails
  49. // the client can switch to other available eps
  50. watchCli.SetEndpoints(eps...)
  51. key, val := "foo", "bar"
  52. wch := watchCli.Watch(context.Background(), key, clientv3.WithCreatedNotify())
  53. select {
  54. case <-wch:
  55. case <-time.After(3 * time.Second):
  56. t.Fatal("took too long to create watch")
  57. }
  58. donec := make(chan struct{})
  59. go func() {
  60. defer close(donec)
  61. // switch to others when eps[lead] is shut down
  62. select {
  63. case ev := <-wch:
  64. if werr := ev.Err(); werr != nil {
  65. t.Fatal(werr)
  66. }
  67. if len(ev.Events) != 1 {
  68. t.Fatalf("expected one event, got %+v", ev)
  69. }
  70. if !bytes.Equal(ev.Events[0].Kv.Value, []byte(val)) {
  71. t.Fatalf("expected %q, got %+v", val, ev.Events[0].Kv)
  72. }
  73. case <-time.After(7 * time.Second):
  74. t.Fatal("took too long to receive events")
  75. }
  76. }()
  77. // shut down eps[lead]
  78. clus.Members[lead].Terminate(t)
  79. // writes to eps[lead+1]
  80. putCli, err := clientv3.New(clientv3.Config{Endpoints: []string{eps[(lead+1)%3]}})
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. defer putCli.Close()
  85. for {
  86. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  87. _, err = putCli.Put(ctx, key, val)
  88. cancel()
  89. if err == nil {
  90. break
  91. }
  92. if err == context.DeadlineExceeded {
  93. continue
  94. }
  95. t.Fatal(err)
  96. }
  97. select {
  98. case <-donec:
  99. case <-time.After(5 * time.Second): // enough time for balancer switch
  100. t.Fatal("took too long to receive events")
  101. }
  102. }