election_test.go 2.9 KB

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