keymutex_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 keymutex
  14. import (
  15. "testing"
  16. "time"
  17. )
  18. const (
  19. callbackTimeout = 1 * time.Second
  20. )
  21. func Test_SingleLock_NoUnlock(t *testing.T) {
  22. // Arrange
  23. km := NewKeyMutex()
  24. key := "fakeid"
  25. callbackCh := make(chan interface{})
  26. // Act
  27. go lockAndCallback(km, key, callbackCh)
  28. // Assert
  29. verifyCallbackHappens(t, callbackCh)
  30. }
  31. func Test_SingleLock_SingleUnlock(t *testing.T) {
  32. // Arrange
  33. km := NewKeyMutex()
  34. key := "fakeid"
  35. callbackCh := make(chan interface{})
  36. // Act & Assert
  37. go lockAndCallback(km, key, callbackCh)
  38. verifyCallbackHappens(t, callbackCh)
  39. km.UnlockKey(key)
  40. }
  41. func Test_DoubleLock_DoubleUnlock(t *testing.T) {
  42. // Arrange
  43. km := NewKeyMutex()
  44. key := "fakeid"
  45. callbackCh1stLock := make(chan interface{})
  46. callbackCh2ndLock := make(chan interface{})
  47. // Act & Assert
  48. go lockAndCallback(km, key, callbackCh1stLock)
  49. verifyCallbackHappens(t, callbackCh1stLock)
  50. go lockAndCallback(km, key, callbackCh2ndLock)
  51. verifyCallbackDoesntHappens(t, callbackCh2ndLock)
  52. km.UnlockKey(key)
  53. verifyCallbackHappens(t, callbackCh2ndLock)
  54. km.UnlockKey(key)
  55. }
  56. func lockAndCallback(km KeyMutex, id string, callbackCh chan<- interface{}) {
  57. km.LockKey(id)
  58. callbackCh <- true
  59. }
  60. func verifyCallbackHappens(t *testing.T, callbackCh <-chan interface{}) bool {
  61. select {
  62. case <-callbackCh:
  63. return true
  64. case <-time.After(callbackTimeout):
  65. t.Fatalf("Timed out waiting for callback.")
  66. return false
  67. }
  68. }
  69. func verifyCallbackDoesntHappens(t *testing.T, callbackCh <-chan interface{}) bool {
  70. select {
  71. case <-callbackCh:
  72. t.Fatalf("Unexpected callback.")
  73. return false
  74. case <-time.After(callbackTimeout):
  75. return true
  76. }
  77. }
  78. func verifyNoError(t *testing.T, err error, name string) {
  79. if err != nil {
  80. t.Fatalf("Unexpected response on %q. Expected: <no error> Actual: <%v>", name, err)
  81. }
  82. }
  83. func verifyError(t *testing.T, err error, name string) {
  84. if err == nil {
  85. t.Fatalf("Unexpected response on %q. Expected: <error> Actual: <no error>", name)
  86. }
  87. }
  88. func verifyMsg(t *testing.T, expected, actual string) {
  89. if actual != expected {
  90. t.Fatalf("Unexpected testMsg value. Expected: <%v> Actual: <%v>", expected, actual)
  91. }
  92. }