ctl_v3_lock_test.go 2.7 KB

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