election_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2018 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 concurrency_test
  15. import (
  16. "context"
  17. "log"
  18. "strings"
  19. "testing"
  20. "time"
  21. "go.etcd.io/etcd/clientv3"
  22. "go.etcd.io/etcd/clientv3/concurrency"
  23. )
  24. func TestResumeElection(t *testing.T) {
  25. const prefix = "/resume-election/"
  26. cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. defer cli.Close()
  31. var s *concurrency.Session
  32. s, err = concurrency.NewSession(cli)
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. defer s.Close()
  37. e := concurrency.NewElection(s, prefix)
  38. // entire test should never take more than 10 seconds
  39. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  40. defer cancel()
  41. // become leader
  42. if err = e.Campaign(ctx, "candidate1"); err != nil {
  43. t.Fatalf("Campaign() returned non nil err: %s", err)
  44. }
  45. // get the leadership details of the current election
  46. var leader *clientv3.GetResponse
  47. leader, err = e.Leader(ctx)
  48. if err != nil {
  49. t.Fatalf("Leader() returned non nil err: %s", err)
  50. }
  51. // Recreate the election
  52. e = concurrency.ResumeElection(s, prefix,
  53. string(leader.Kvs[0].Key), leader.Kvs[0].CreateRevision)
  54. respChan := make(chan *clientv3.GetResponse)
  55. go func() {
  56. o := e.Observe(ctx)
  57. respChan <- nil
  58. for {
  59. select {
  60. case resp, ok := <-o:
  61. if !ok {
  62. t.Fatal("Observe() channel closed prematurely")
  63. }
  64. // Ignore any observations that candidate1 was elected
  65. if string(resp.Kvs[0].Value) == "candidate1" {
  66. continue
  67. }
  68. respChan <- &resp
  69. return
  70. }
  71. }
  72. }()
  73. // wait until observe goroutine is running
  74. <-respChan
  75. // put some random data to generate a change event, this put should be
  76. // ignored by Observe() because it is not under the election prefix.
  77. _, err = cli.Put(ctx, "foo", "bar")
  78. if err != nil {
  79. t.Fatalf("Put('foo') returned non nil err: %s", err)
  80. }
  81. // resign as leader
  82. if err := e.Resign(ctx); err != nil {
  83. t.Fatalf("Resign() returned non nil err: %s", err)
  84. }
  85. // elect a different candidate
  86. if err := e.Campaign(ctx, "candidate2"); err != nil {
  87. t.Fatalf("Campaign() returned non nil err: %s", err)
  88. }
  89. // wait for observed leader change
  90. resp := <-respChan
  91. kv := resp.Kvs[0]
  92. if !strings.HasPrefix(string(kv.Key), prefix) {
  93. t.Errorf("expected observed election to have prefix '%s' got %q", prefix, string(kv.Key))
  94. }
  95. if string(kv.Value) != "candidate2" {
  96. t.Errorf("expected new leader to be 'candidate1' got %q", string(kv.Value))
  97. }
  98. }