v3election_grpc_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. "context"
  17. "fmt"
  18. "testing"
  19. "time"
  20. epb "go.etcd.io/etcd/etcdserver/api/v3election/v3electionpb"
  21. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  22. "go.etcd.io/etcd/pkg/testutil"
  23. )
  24. // TestV3ElectionCampaign checks that Campaign will not give
  25. // simultaneous leadership to multiple campaigners.
  26. func TestV3ElectionCampaign(t *testing.T) {
  27. defer testutil.AfterTest(t)
  28. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  29. defer clus.Terminate(t)
  30. lease1, err1 := toGRPC(clus.RandClient()).Lease.LeaseGrant(context.TODO(), &pb.LeaseGrantRequest{TTL: 30})
  31. if err1 != nil {
  32. t.Fatal(err1)
  33. }
  34. lease2, err2 := toGRPC(clus.RandClient()).Lease.LeaseGrant(context.TODO(), &pb.LeaseGrantRequest{TTL: 30})
  35. if err2 != nil {
  36. t.Fatal(err2)
  37. }
  38. lc := toGRPC(clus.Client(0)).Election
  39. req1 := &epb.CampaignRequest{Name: []byte("foo"), Lease: lease1.ID, Value: []byte("abc")}
  40. l1, lerr1 := lc.Campaign(context.TODO(), req1)
  41. if lerr1 != nil {
  42. t.Fatal(lerr1)
  43. }
  44. campaignc := make(chan struct{})
  45. go func() {
  46. defer close(campaignc)
  47. req2 := &epb.CampaignRequest{Name: []byte("foo"), Lease: lease2.ID, Value: []byte("def")}
  48. l2, lerr2 := lc.Campaign(context.TODO(), req2)
  49. if lerr2 != nil {
  50. t.Error(lerr2)
  51. }
  52. if l1.Header.Revision >= l2.Header.Revision {
  53. t.Errorf("expected l1 revision < l2 revision, got %d >= %d", l1.Header.Revision, l2.Header.Revision)
  54. }
  55. }()
  56. select {
  57. case <-time.After(200 * time.Millisecond):
  58. case <-campaignc:
  59. t.Fatalf("got leadership before resign")
  60. }
  61. if _, uerr := lc.Resign(context.TODO(), &epb.ResignRequest{Leader: l1.Leader}); uerr != nil {
  62. t.Fatal(uerr)
  63. }
  64. select {
  65. case <-time.After(200 * time.Millisecond):
  66. t.Fatalf("campaigner unelected after resign")
  67. case <-campaignc:
  68. }
  69. lval, lverr := lc.Leader(context.TODO(), &epb.LeaderRequest{Name: []byte("foo")})
  70. if lverr != nil {
  71. t.Fatal(lverr)
  72. }
  73. if string(lval.Kv.Value) != "def" {
  74. t.Fatalf("got election value %q, expected %q", string(lval.Kv.Value), "def")
  75. }
  76. }
  77. // TestV3ElectionObserve checks that an Observe stream receives
  78. // proclamations from different leaders uninterrupted.
  79. func TestV3ElectionObserve(t *testing.T) {
  80. defer testutil.AfterTest(t)
  81. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  82. defer clus.Terminate(t)
  83. lc := toGRPC(clus.Client(0)).Election
  84. // observe leadership events
  85. observec := make(chan struct{})
  86. go func() {
  87. defer close(observec)
  88. s, err := lc.Observe(context.Background(), &epb.LeaderRequest{Name: []byte("foo")})
  89. observec <- struct{}{}
  90. if err != nil {
  91. t.Error(err)
  92. }
  93. for i := 0; i < 10; i++ {
  94. resp, rerr := s.Recv()
  95. if rerr != nil {
  96. t.Error(rerr)
  97. }
  98. respV := 0
  99. fmt.Sscanf(string(resp.Kv.Value), "%d", &respV)
  100. // leader transitions should not go backwards
  101. if respV < i {
  102. t.Errorf(`got observe value %q, expected >= "%d"`, string(resp.Kv.Value), i)
  103. }
  104. i = respV
  105. }
  106. }()
  107. select {
  108. case <-observec:
  109. case <-time.After(time.Second):
  110. t.Fatalf("observe stream took too long to start")
  111. }
  112. lease1, err1 := toGRPC(clus.RandClient()).Lease.LeaseGrant(context.TODO(), &pb.LeaseGrantRequest{TTL: 30})
  113. if err1 != nil {
  114. t.Fatal(err1)
  115. }
  116. c1, cerr1 := lc.Campaign(context.TODO(), &epb.CampaignRequest{Name: []byte("foo"), Lease: lease1.ID, Value: []byte("0")})
  117. if cerr1 != nil {
  118. t.Fatal(cerr1)
  119. }
  120. // overlap other leader so it waits on resign
  121. leader2c := make(chan struct{})
  122. go func() {
  123. defer close(leader2c)
  124. lease2, err2 := toGRPC(clus.RandClient()).Lease.LeaseGrant(context.TODO(), &pb.LeaseGrantRequest{TTL: 30})
  125. if err2 != nil {
  126. t.Error(err2)
  127. }
  128. c2, cerr2 := lc.Campaign(context.TODO(), &epb.CampaignRequest{Name: []byte("foo"), Lease: lease2.ID, Value: []byte("5")})
  129. if cerr2 != nil {
  130. t.Error(cerr2)
  131. }
  132. for i := 6; i < 10; i++ {
  133. v := []byte(fmt.Sprintf("%d", i))
  134. req := &epb.ProclaimRequest{Leader: c2.Leader, Value: v}
  135. if _, err := lc.Proclaim(context.TODO(), req); err != nil {
  136. t.Error(err)
  137. }
  138. }
  139. }()
  140. for i := 1; i < 5; i++ {
  141. v := []byte(fmt.Sprintf("%d", i))
  142. req := &epb.ProclaimRequest{Leader: c1.Leader, Value: v}
  143. if _, err := lc.Proclaim(context.TODO(), req); err != nil {
  144. t.Fatal(err)
  145. }
  146. }
  147. // start second leader
  148. lc.Resign(context.TODO(), &epb.ResignRequest{Leader: c1.Leader})
  149. select {
  150. case <-observec:
  151. case <-time.After(time.Second):
  152. t.Fatalf("observe did not observe all events in time")
  153. }
  154. <-leader2c
  155. }