v3_election_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/Godeps/_workspace/src/golang.org/x/net/context"
  20. "github.com/coreos/etcd/clientv3/concurrency"
  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 := concurrency.NewElection(clus.RandClient(), "test-election")
  38. cctx, cancel := context.WithCancel(context.TODO())
  39. defer cancel()
  40. s, ok := <-b.Observe(cctx)
  41. if !ok {
  42. t.Fatalf("could not observe election; channel closed")
  43. }
  44. electedc <- string(s.Kvs[0].Value)
  45. // wait for next election round
  46. <-ch
  47. }
  48. donec <- struct{}{}
  49. }(nextc[i])
  50. }
  51. // elect some leaders
  52. for i := 0; i < leaders; i++ {
  53. go func() {
  54. e := concurrency.NewElection(clus.RandClient(), "test-election")
  55. ev := fmt.Sprintf("electval-%v", time.Now().UnixNano())
  56. if err := e.Campaign(context.TODO(), ev); err != nil {
  57. t.Fatalf("failed volunteer (%v)", err)
  58. }
  59. // wait for followers to accept leadership
  60. for j := 0; j < followers; j++ {
  61. s := <-electedc
  62. if s != ev {
  63. t.Errorf("wrong election value got %s, wanted %s", s, ev)
  64. }
  65. }
  66. // let next leader take over
  67. if err := e.Resign(); err != nil {
  68. t.Fatalf("failed resign (%v)", err)
  69. }
  70. // tell followers to start listening for next leader
  71. for j := 0; j < followers; j++ {
  72. nextc[j] <- struct{}{}
  73. }
  74. }()
  75. }
  76. // wait on followers
  77. for i := 0; i < followers; i++ {
  78. <-donec
  79. }
  80. }
  81. // TestElectionFailover tests that an election will
  82. func TestElectionFailover(t *testing.T) {
  83. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  84. defer clus.Terminate(t)
  85. defer dropSessionLease(clus)
  86. cctx, cancel := context.WithCancel(context.TODO())
  87. defer cancel()
  88. // first leader (elected)
  89. e := concurrency.NewElection(clus.clients[0], "test-election")
  90. if err := e.Campaign(context.TODO(), "foo"); err != nil {
  91. t.Fatalf("failed volunteer (%v)", err)
  92. }
  93. // check first leader
  94. resp, ok := <-e.Observe(cctx)
  95. if !ok {
  96. t.Fatalf("could not wait for first election; channel closed")
  97. }
  98. s := string(resp.Kvs[0].Value)
  99. if s != "foo" {
  100. t.Fatalf("wrong election result. got %s, wanted foo", s)
  101. }
  102. // next leader
  103. electedc := make(chan struct{})
  104. go func() {
  105. ee := concurrency.NewElection(clus.clients[1], "test-election")
  106. if eer := ee.Campaign(context.TODO(), "bar"); eer != nil {
  107. t.Fatal(eer)
  108. }
  109. electedc <- struct{}{}
  110. }()
  111. // invoke leader failover
  112. session, serr := concurrency.NewSession(clus.clients[0])
  113. if serr != nil {
  114. t.Fatal(serr)
  115. }
  116. if err := session.Close(); err != nil {
  117. t.Fatal(err)
  118. }
  119. // check new leader
  120. e = concurrency.NewElection(clus.clients[2], "test-election")
  121. resp, ok = <-e.Observe(cctx)
  122. if !ok {
  123. t.Fatalf("could not wait for second election; channel closed")
  124. }
  125. s = string(resp.Kvs[0].Value)
  126. if s != "bar" {
  127. t.Fatalf("wrong election result. got %s, wanted bar", s)
  128. }
  129. // leader must ack election (otherwise, Campaign may see closed conn)
  130. <-electedc
  131. }