github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/server/lockfile_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package server 22 23 import ( 24 "fmt" 25 "io/ioutil" 26 "math/rand" 27 "os" 28 "os/exec" 29 "path" 30 "path/filepath" 31 "strconv" 32 "testing" 33 "time" 34 35 "github.com/stretchr/testify/assert" 36 ) 37 38 func TestAcquire(t *testing.T) { 39 t.Run("process B can obtain the lock after A exits", func(t *testing.T) { 40 path := tempPath() 41 assert.NoError(t, newLockfileCommand(path, "", true).Run()) 42 _, err := os.Stat(path) 43 assert.True(t, os.IsNotExist(err)) // check temp file was removed 44 assert.NoError(t, newLockfileCommand(path, "", true).Run()) 45 }) 46 47 t.Run("process B can obtain the lock after A exits, even if A didn't remove the lock file", func(t *testing.T) { 48 path := tempPath() 49 assert.NoError(t, newLockfileCommand(path, "", false).Run()) 50 _, err := os.Stat(path) 51 assert.False(t, os.IsNotExist(err)) // check temp file was *not* removed 52 assert.NoError(t, newLockfileCommand(path, "", true).Run()) 53 }) 54 55 t.Run("if process A holds the lock, B must not be able to obtain it", func(t *testing.T) { 56 path := tempPath() 57 58 procA := newLockfileCommand(path, "1s", false) 59 procB := newLockfileCommand(path, "1s", false) 60 61 assert.NoError(t, procA.Start()) 62 assert.NoError(t, procB.Start()) 63 64 // one process will acquireLockfile and hold the lock, and the other will fail to acquireLockfile. 65 errA, errB := procA.Wait(), procB.Wait() 66 67 if errA != nil { 68 assert.NoError(t, errB) 69 } else { 70 assert.Error(t, errB) 71 } 72 }) 73 } 74 75 func TestCreateAndAcquire(t *testing.T) { 76 tempDir, err := ioutil.TempDir("", "TestCreateAndAcquire") 77 assert.NoError(t, err) 78 defer os.RemoveAll(tempDir) 79 80 tempSubDir := path.Join(tempDir, "testDir") 81 82 lock, err := createAndAcquireLockfile(path.Join(tempSubDir, "testLockfile"), os.ModePerm) 83 assert.NoError(t, err) 84 err = lock.releaseLockfile() 85 assert.NoError(t, err) 86 87 // check createAndAcquireLockfile() created the missing directory 88 _, err = os.Stat(tempSubDir) 89 assert.False(t, os.IsNotExist(err)) 90 } 91 92 // TestAcquireAndReleaseFile is invoked as a separate process by other tests in lockfile_test.go 93 // to exercise the file locking capabilities. The test is a no-op if run as part 94 // of the broader test suite. Given it's run as a separate process, we explicitly use error 95 // exit codes as opposed to failing assertions on errors 96 func TestAcquireAndReleaseFile(t *testing.T) { 97 // immediately return if this test wasn't invoked by another test in the 98 // nolint: goconst 99 if os.Getenv("LOCKFILE_SUPERVISED_PROCESS") != "true" { 100 t.Skip() 101 } 102 103 var ( 104 lockPath = os.Getenv("WITH_LOCK_PATH") 105 removeLock = os.Getenv("WITH_REMOVE_LOCK") 106 sleepDuration = os.Getenv("WITH_SLEEP_DURATION") 107 ) 108 109 lock, err := acquireLockfile(lockPath) 110 if err != nil { 111 os.Exit(1) 112 } 113 114 if sleepDuration != "" { 115 duration, err := time.ParseDuration(sleepDuration) 116 if err != nil { 117 os.Exit(1) 118 } 119 120 time.Sleep(duration) 121 } 122 123 if removeLock == "true" { 124 err := lock.releaseLockfile() 125 if err != nil { 126 os.Exit(1) 127 } 128 } 129 } 130 131 func tempPath() string { 132 return filepath.Join(os.TempDir(), "lockfile_test_"+strconv.Itoa(os.Getpid())+"_"+strconv.Itoa(rand.Intn(100000))) 133 } 134 135 func newLockfileCommand(lockPath string, sleepDuration string, removeLock bool) *exec.Cmd { 136 removeLockStr := "false" 137 if removeLock { 138 removeLockStr = "true" 139 } 140 141 cmd := exec.Command("go", "test", "-run", "TestAcquireAndReleaseFile") 142 cmd.Env = os.Environ() 143 cmd.Env = append( 144 cmd.Env, 145 "LOCKFILE_SUPERVISED_PROCESS=true", 146 fmt.Sprintf("WITH_LOCK_PATH=%s", lockPath), 147 fmt.Sprintf("WITH_SLEEP_DURATION=%s", sleepDuration), 148 fmt.Sprintf("WITH_REMOVE_LOCK=%s", removeLockStr), 149 ) 150 151 return cmd 152 }