ctl_v3_lock_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "go.etcd.io/etcd/pkg/expect"
  21. )
  22. func TestCtlV3Lock(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, testLock)
  27. }
  28. func testLock(cx ctlCtx) {
  29. name := "a"
  30. holder, ch, err := ctlV3Lock(cx, name)
  31. if err != nil {
  32. cx.t.Fatal(err)
  33. }
  34. l1 := ""
  35. select {
  36. case <-time.After(2 * time.Second):
  37. cx.t.Fatalf("timed out locking")
  38. case l1 = <-ch:
  39. if !strings.HasPrefix(l1, name) {
  40. cx.t.Errorf("got %q, expected %q prefix", l1, name)
  41. }
  42. }
  43. // blocked process that won't acquire the lock
  44. blocked, ch, err := ctlV3Lock(cx, name)
  45. if err != nil {
  46. cx.t.Fatal(err)
  47. }
  48. select {
  49. case <-time.After(100 * time.Millisecond):
  50. case <-ch:
  51. cx.t.Fatalf("should block")
  52. }
  53. // overlap with a blocker that will acquire the lock
  54. blockAcquire, ch, err := ctlV3Lock(cx, name)
  55. if err != nil {
  56. cx.t.Fatal(err)
  57. }
  58. defer blockAcquire.Stop()
  59. select {
  60. case <-time.After(100 * time.Millisecond):
  61. case <-ch:
  62. cx.t.Fatalf("should block")
  63. }
  64. // kill blocked process with clean shutdown
  65. if err = blocked.Signal(os.Interrupt); err != nil {
  66. cx.t.Fatal(err)
  67. }
  68. if err = closeWithTimeout(blocked, time.Second); err != nil {
  69. cx.t.Fatal(err)
  70. }
  71. // kill the holder with clean shutdown
  72. if err = holder.Signal(os.Interrupt); err != nil {
  73. cx.t.Fatal(err)
  74. }
  75. if err = closeWithTimeout(holder, 200*time.Millisecond+time.Second); err != nil {
  76. cx.t.Fatal(err)
  77. }
  78. // blockAcquire should acquire the lock
  79. select {
  80. case <-time.After(time.Second):
  81. cx.t.Fatalf("timed out from waiting to holding")
  82. case l2 := <-ch:
  83. if l1 == l2 || !strings.HasPrefix(l2, name) {
  84. cx.t.Fatalf("expected different lock name, got l1=%q, l2=%q", l1, l2)
  85. }
  86. }
  87. }
  88. // ctlV3Lock creates a lock process with a channel listening for when it acquires the lock.
  89. func ctlV3Lock(cx ctlCtx, name string) (*expect.ExpectProcess, <-chan string, error) {
  90. cmdArgs := append(cx.PrefixArgs(), "lock", name)
  91. proc, err := spawnCmd(cmdArgs)
  92. outc := make(chan string, 1)
  93. if err != nil {
  94. close(outc)
  95. return proc, outc, err
  96. }
  97. go func() {
  98. s, xerr := proc.ExpectFunc(func(string) bool { return true })
  99. if xerr != nil {
  100. cx.t.Errorf("expect failed (%v)", xerr)
  101. }
  102. outc <- s
  103. }()
  104. return proc, outc, err
  105. }