v3_election_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2016 CoreOS, Inc.
  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. "fmt"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/clientv3/concurrency"
  20. "github.com/coreos/etcd/contrib/recipes"
  21. )
  22. // TestElectionWait tests if followers can correctly wait for elections.
  23. func TestElectionWait(t *testing.T) {
  24. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  25. defer clus.Terminate(t)
  26. defer dropSessionLease(clus)
  27. leaders := 3
  28. followers := 3
  29. electedc := make(chan string)
  30. nextc := []chan struct{}{}
  31. // wait for all elections
  32. donec := make(chan struct{})
  33. for i := 0; i < followers; i++ {
  34. nextc = append(nextc, make(chan struct{}))
  35. go func(ch chan struct{}) {
  36. for j := 0; j < leaders; j++ {
  37. b := recipe.NewElection(clus.RandClient(), "test-election")
  38. s, err := b.Wait()
  39. if err != nil {
  40. t.Fatalf("could not wait for election (%v)", err)
  41. }
  42. electedc <- s
  43. // wait for next election round
  44. <-ch
  45. }
  46. donec <- struct{}{}
  47. }(nextc[i])
  48. }
  49. // elect some leaders
  50. for i := 0; i < leaders; i++ {
  51. go func() {
  52. e := recipe.NewElection(clus.RandClient(), "test-election")
  53. ev := fmt.Sprintf("electval-%v", time.Now().UnixNano())
  54. if err := e.Volunteer(ev); err != nil {
  55. t.Fatalf("failed volunteer (%v)", err)
  56. }
  57. // wait for followers to accept leadership
  58. for j := 0; j < followers; j++ {
  59. s := <-electedc
  60. if s != ev {
  61. t.Errorf("wrong election value got %s, wanted %s", s, ev)
  62. }
  63. }
  64. // let next leader take over
  65. if err := e.Resign(); err != nil {
  66. t.Fatalf("failed resign (%v)", err)
  67. }
  68. // tell followers to start listening for next leader
  69. for j := 0; j < followers; j++ {
  70. nextc[j] <- struct{}{}
  71. }
  72. }()
  73. }
  74. // wait on followers
  75. for i := 0; i < followers; i++ {
  76. <-donec
  77. }
  78. }
  79. // TestElectionFailover tests that an election will
  80. func TestElectionFailover(t *testing.T) {
  81. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  82. defer clus.Terminate(t)
  83. defer dropSessionLease(clus)
  84. // first leader (elected)
  85. e := recipe.NewElection(clus.clients[0], "test-election")
  86. if err := e.Volunteer("foo"); err != nil {
  87. t.Fatalf("failed volunteer (%v)", err)
  88. }
  89. // check first leader
  90. s, err := e.Wait()
  91. if err != nil {
  92. t.Fatalf("could not wait for first election (%v)", err)
  93. }
  94. if s != "foo" {
  95. t.Fatalf("wrong election result. got %s, wanted foo", s)
  96. }
  97. // next leader
  98. electedc := make(chan struct{})
  99. go func() {
  100. ee := recipe.NewElection(clus.clients[1], "test-election")
  101. if eer := ee.Volunteer("bar"); eer != nil {
  102. t.Fatal(eer)
  103. }
  104. electedc <- struct{}{}
  105. }()
  106. // invoke leader failover
  107. session, serr := concurrency.NewSession(clus.clients[0])
  108. if serr != nil {
  109. t.Fatal(serr)
  110. }
  111. err = session.Close()
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. // check new leader
  116. e = recipe.NewElection(clus.clients[2], "test-election")
  117. s, err = e.Wait()
  118. if err != nil {
  119. t.Fatalf("could not wait for second election (%v)", err)
  120. }
  121. if s != "bar" {
  122. t.Fatalf("wrong election result. got %s, wanted bar", s)
  123. }
  124. // leader must ack election (otherwise, Volunteer may see closed conn)
  125. <-electedc
  126. }