@ -19,9 +19,22 @@ limitations under the License.
package flock
package flock
import (
import (
"os/exec"
"strings"
"testing"
"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 ) {
func Test_UnitFlock ( t * testing . T ) {
tests := [ ] struct {
tests := [ ] struct {
name string
name string
@ -45,7 +58,7 @@ func Test_UnitFlock(t *testing.T) {
return
return
}
}
if got := C heckLock( tt . path ) ; got != tt . wantCheck {
if got := c heckLock( tt . path ) ; got != tt . wantCheck {
t . Errorf ( "CheckLock() = %+v\nWant = %+v" , got , tt . wantCheck )
t . Errorf ( "CheckLock() = %+v\nWant = %+v" , got , tt . wantCheck )
}
}
@ -53,7 +66,7 @@ func Test_UnitFlock(t *testing.T) {
t . Errorf ( "Release() error = %v, wantErr %v" , err , tt . wantErr )
t . Errorf ( "Release() error = %v, wantErr %v" , err , tt . wantErr )
}
}
if got := C heckLock( tt . path ) ; got == tt . wantCheck {
if got := c heckLock( tt . path ) ; got == tt . wantCheck {
t . Errorf ( "CheckLock() = %+v\nWant = %+v" , got , ! tt . wantCheck )
t . Errorf ( "CheckLock() = %+v\nWant = %+v" , got , ! tt . wantCheck )
}
}
} )
} )