deadlock-detector_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package threading
  14. import (
  15. "sync"
  16. "testing"
  17. "time"
  18. )
  19. type fakeExiter struct {
  20. format string
  21. args []interface{}
  22. exited bool
  23. }
  24. func (f *fakeExiter) Exitf(format string, args ...interface{}) {
  25. f.format = format
  26. f.args = args
  27. f.exited = true
  28. }
  29. func TestMaxLockPeriod(t *testing.T) {
  30. lock := &sync.RWMutex{}
  31. panicked := false
  32. func() {
  33. defer func() {
  34. if r := recover(); r != nil {
  35. panicked = true
  36. }
  37. }()
  38. DeadlockWatchdogReadLock(lock, "test lock", 0)
  39. }()
  40. if !panicked {
  41. t.Errorf("expected a panic for a zero max lock period")
  42. }
  43. }
  44. func TestDeadlockWatchdogLocked(t *testing.T) {
  45. lock := &sync.RWMutex{}
  46. lock.Lock()
  47. exitCh := make(chan time.Time, 1)
  48. fake := fakeExiter{}
  49. detector := &deadlockDetector{
  50. lock: &rwMutexToLockableAdapter{lock},
  51. name: "test deadlock",
  52. exitChannelFn: func() <-chan time.Time { return exitCh },
  53. exiter: &fake,
  54. }
  55. exitCh <- time.Time{}
  56. detector.run()
  57. if !fake.exited {
  58. t.Errorf("expected to have exited")
  59. }
  60. if len(fake.args) != 1 || fake.args[0].(string) != detector.name {
  61. t.Errorf("unexpected args: %v", fake.args)
  62. }
  63. }
  64. func TestDeadlockWatchdogUnlocked(t *testing.T) {
  65. lock := &sync.RWMutex{}
  66. fake := fakeExiter{}
  67. detector := &deadlockDetector{
  68. lock: &rwMutexToLockableAdapter{lock},
  69. name: "test deadlock",
  70. exitChannelFn: func() <-chan time.Time { return time.After(time.Second * 5) },
  71. exiter: &fake,
  72. }
  73. for i := 0; i < 100; i++ {
  74. detector.runOnce()
  75. }
  76. if fake.exited {
  77. t.Errorf("expected to have not exited")
  78. }
  79. }
  80. func TestDeadlockWatchdogLocking(t *testing.T) {
  81. lock := &sync.RWMutex{}
  82. fake := fakeExiter{}
  83. go func() {
  84. for {
  85. lock.Lock()
  86. lock.Unlock()
  87. }
  88. }()
  89. detector := &deadlockDetector{
  90. lock: &rwMutexToLockableAdapter{lock},
  91. name: "test deadlock",
  92. exitChannelFn: func() <-chan time.Time { return time.After(time.Second * 5) },
  93. exiter: &fake,
  94. }
  95. for i := 0; i < 100; i++ {
  96. detector.runOnce()
  97. }
  98. if fake.exited {
  99. t.Errorf("expected to have not exited")
  100. }
  101. }