ctl_v3_elect_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2016 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 e2e
  15. import (
  16. "os"
  17. "strings"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/pkg/expect"
  21. )
  22. func TestCtlV3Elect(t *testing.T) {
  23. oldenv := os.Getenv("EXPECT_DEBUG")
  24. defer os.Setenv("EXPECT_DEBUG", oldenv)
  25. os.Setenv("EXPECT_DEBUG", "1")
  26. testCtl(t, testElect)
  27. }
  28. func testElect(cx ctlCtx) {
  29. // debugging for #6934
  30. sig := cx.epc.withStopSignal(debugLockSignal)
  31. defer cx.epc.withStopSignal(sig)
  32. name := "a"
  33. holder, ch, err := ctlV3Elect(cx, name, "p1")
  34. if err != nil {
  35. cx.t.Fatal(err)
  36. }
  37. l1 := ""
  38. select {
  39. case <-time.After(2 * time.Second):
  40. cx.t.Fatalf("timed out electing")
  41. case l1 = <-ch:
  42. if !strings.HasPrefix(l1, name) {
  43. cx.t.Errorf("got %q, expected %q prefix", l1, name)
  44. }
  45. }
  46. // blocked process that won't win the election
  47. blocked, ch, err := ctlV3Elect(cx, name, "p2")
  48. if err != nil {
  49. cx.t.Fatal(err)
  50. }
  51. select {
  52. case <-time.After(100 * time.Millisecond):
  53. case <-ch:
  54. cx.t.Fatalf("should block")
  55. }
  56. // overlap with a blocker that will win the election
  57. blockAcquire, ch, err := ctlV3Elect(cx, name, "p2")
  58. if err != nil {
  59. cx.t.Fatal(err)
  60. }
  61. defer blockAcquire.Stop()
  62. select {
  63. case <-time.After(100 * time.Millisecond):
  64. case <-ch:
  65. cx.t.Fatalf("should block")
  66. }
  67. // kill blocked process with clean shutdown
  68. if err = blocked.Signal(os.Interrupt); err != nil {
  69. cx.t.Fatal(err)
  70. }
  71. if err = closeWithTimeout(blocked, time.Second); err != nil {
  72. cx.t.Fatal(err)
  73. }
  74. // kill the holder with clean shutdown
  75. if err = holder.Signal(os.Interrupt); err != nil {
  76. cx.t.Fatal(err)
  77. }
  78. if err = closeWithTimeout(holder, time.Second); err != nil {
  79. cx.t.Fatal(err)
  80. }
  81. // blockAcquire should win the election
  82. select {
  83. case <-time.After(time.Second):
  84. cx.t.Fatalf("timed out from waiting to holding")
  85. case l2 := <-ch:
  86. if l1 == l2 || !strings.HasPrefix(l2, name) {
  87. cx.t.Fatalf("expected different elect name, got l1=%q, l2=%q", l1, l2)
  88. }
  89. }
  90. }
  91. // ctlV3Elect creates a elect process with a channel listening for when it wins the election.
  92. func ctlV3Elect(cx ctlCtx, name, proposal string) (*expect.ExpectProcess, <-chan string, error) {
  93. cmdArgs := append(cx.PrefixArgs(), "elect", name, proposal)
  94. proc, err := spawnCmd(cmdArgs)
  95. outc := make(chan string, 1)
  96. if err != nil {
  97. close(outc)
  98. return proc, outc, err
  99. }
  100. proc.StopSignal = debugLockSignal
  101. go func() {
  102. s, xerr := proc.ExpectFunc(func(string) bool { return true })
  103. if xerr != nil {
  104. cx.t.Errorf("expect failed (%v)", xerr)
  105. }
  106. outc <- s
  107. }()
  108. return proc, outc, err
  109. }