You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.0 KiB

  1. // Copyright (c) 2015-2021 MinIO, Inc.
  2. //
  3. // This file is part of MinIO Object Storage stack
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. package cmd
  18. import (
  19. "runtime"
  20. "testing"
  21. "time"
  22. )
  23. // WARNING:
  24. //
  25. // Expected source line number is hard coded, 35, in the
  26. // following test. Adding new code before this test or changing its
  27. // position will cause the line number to change and the test to FAIL
  28. // Tests getSource().
  29. func TestGetSource(t *testing.T) {
  30. currentSource := func() string { return getSource(2) }
  31. gotSource := currentSource()
  32. // Hard coded line number, 34, in the "expectedSource" value
  33. expectedSource := "[namespace-lock_test.go:34:TestGetSource()]"
  34. if gotSource != expectedSource {
  35. t.Errorf("expected : %s, got : %s", expectedSource, gotSource)
  36. }
  37. }
  38. // Test lock race
  39. func TestNSLockRace(t *testing.T) {
  40. t.Skip("long test skip it")
  41. ctx := t.Context()
  42. for i := 0; i < 10000; i++ {
  43. nsLk := newNSLock(false)
  44. // lk1; ref=1
  45. if !nsLk.lock(ctx, "volume", "path", "source", "opsID", false, time.Second) {
  46. t.Fatal("failed to acquire lock")
  47. }
  48. // lk2
  49. lk2ch := make(chan struct{})
  50. go func() {
  51. defer close(lk2ch)
  52. nsLk.lock(ctx, "volume", "path", "source", "opsID", false, 1*time.Millisecond)
  53. }()
  54. time.Sleep(1 * time.Millisecond) // wait for goroutine to advance; ref=2
  55. // Unlock the 1st lock; ref=1 after this line
  56. nsLk.unlock("volume", "path", false)
  57. // Taking another lockMapMutex here allows queuing up additional lockers. This should
  58. // not be required but makes reproduction much easier.
  59. nsLk.lockMapMutex.Lock()
  60. // lk3 blocks.
  61. lk3ch := make(chan bool)
  62. go func() {
  63. lk3ch <- nsLk.lock(ctx, "volume", "path", "source", "opsID", false, 0)
  64. }()
  65. // lk4, blocks.
  66. lk4ch := make(chan bool)
  67. go func() {
  68. lk4ch <- nsLk.lock(ctx, "volume", "path", "source", "opsID", false, 0)
  69. }()
  70. runtime.Gosched()
  71. // unlock the manual lock
  72. nsLk.lockMapMutex.Unlock()
  73. // To trigger the race:
  74. // 1) lk3 or lk4 need to advance and increment the ref on the existing resource,
  75. // successfully acquiring the lock.
  76. // 2) lk2 then needs to advance and remove the resource from lockMap.
  77. // 3) lk3 or lk4 (whichever didn't execute in step 1) then executes and creates
  78. // a new entry in lockMap and acquires a lock for the same resource.
  79. <-lk2ch
  80. lk3ok := <-lk3ch
  81. lk4ok := <-lk4ch
  82. if lk3ok && lk4ok {
  83. t.Fatalf("multiple locks acquired; iteration=%d, lk3=%t, lk4=%t", i, lk3ok, lk4ok)
  84. }
  85. }
  86. }