Skip to content

Commit

Permalink
Move CheckLock function into testing only
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <derek.nola@suse.com>
  • Loading branch information
dereknola committed Oct 16, 2024
1 parent ef8cf0e commit 6f2ce21
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
14 changes: 0 additions & 14 deletions pkg/flock/flock_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ limitations under the License.
package flock

import (
"os/exec"
"strings"

"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -49,14 +46,3 @@ func AcquireShared(path string) (int, error) {
func Release(lock int) error {
return unix.Flock(lock, unix.LOCK_UN)
}

// CheckLock checks whether any process is using the lock
func CheckLock(path string) bool {
lockByte, _ := exec.Command("lsof", "-w", "-F", "lfn", path).Output()
locks := string(lockByte)
if locks == "" {
return false
}
readWriteLock := strings.Split(locks, "\n")[2]
return readWriteLock == "lR" || readWriteLock == "lW"
}
17 changes: 15 additions & 2 deletions pkg/flock/flock_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ limitations under the License.
package flock

import (
"os/exec"
"strings"
"testing"
)

// checkLock checks whether any process is using the lock
func checkLock(path string) bool {
lockByte, _ := exec.Command("lsof", "-w", "-F", "lfn", path).Output()
locks := string(lockByte)
if locks == "" {
return false
}
readWriteLock := strings.Split(locks, "\n")[2]
return readWriteLock == "lR" || readWriteLock == "lW"
}

func Test_UnitFlock(t *testing.T) {
tests := []struct {
name string
Expand All @@ -45,15 +58,15 @@ func Test_UnitFlock(t *testing.T) {
return
}

if got := CheckLock(tt.path); got != tt.wantCheck {
if got := checkLock(tt.path); got != tt.wantCheck {
t.Errorf("CheckLock() = %+v\nWant = %+v", got, tt.wantCheck)
}

if err := Release(lock); (err != nil) != tt.wantErr {
t.Errorf("Release() error = %v, wantErr %v", err, tt.wantErr)
}

if got := CheckLock(tt.path); got == tt.wantCheck {
if got := checkLock(tt.path); got == tt.wantCheck {
t.Errorf("CheckLock() = %+v\nWant = %+v", got, !tt.wantCheck)
}
})
Expand Down

0 comments on commit 6f2ce21

Please sign in to comment.