67 lines
899 B
Go
67 lines
899 B
Go
package flock
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_Lock_basic(t *testing.T) {
|
|
ch := make(chan int, 1)
|
|
f, err := Lock("/tmp/fsutil-test-lock")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
go func() {
|
|
time.Sleep(time.Second)
|
|
ch <- 10
|
|
Unlock(f)
|
|
}()
|
|
|
|
select {
|
|
case x := <-ch:
|
|
t.Fatal(x)
|
|
default:
|
|
|
|
}
|
|
|
|
f2, _ := Lock("/tmp/fsutil-test-lock")
|
|
defer Unlock(f2)
|
|
select {
|
|
case i := <-ch:
|
|
if i != 10 {
|
|
t.Fatal(i)
|
|
}
|
|
default:
|
|
t.Fatal("No value available.")
|
|
}
|
|
|
|
}
|
|
|
|
func Test_Lock_badPath(t *testing.T) {
|
|
_, err := Lock("./dne/file.lock")
|
|
if err == nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestTryLock(t *testing.T) {
|
|
lockPath := "/tmp/fsutil-test-lock"
|
|
f, err := TryLock(lockPath)
|
|
if err != nil {
|
|
t.Fatalf("%#v", err)
|
|
t.Fatal(err)
|
|
}
|
|
|
|
f2, err := TryLock(lockPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f2 != nil {
|
|
t.Fatal(f2)
|
|
}
|
|
|
|
if err := Unlock(f); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|