Skip to content

Commit 958d664

Browse files
committed
test that a failed file lock closes the file
1 parent c6c7c77 commit 958d664

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

flock_internal_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package flock
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
)
8+
9+
func Test(t *testing.T) {
10+
tmpFileFh, err := ioutil.TempFile(os.TempDir(), "go-flock-")
11+
tmpFileFh.Close()
12+
tmpFile := tmpFileFh.Name()
13+
os.Remove(tmpFile)
14+
15+
lock := New(tmpFile)
16+
locked, err := lock.TryLock()
17+
if locked == false || err != nil {
18+
t.Fatal("failed to lock")
19+
}
20+
21+
newLock := New(tmpFile)
22+
locked, err = newLock.TryLock()
23+
if locked != false || err != nil {
24+
t.Fatal("should have failed locking")
25+
}
26+
27+
if newLock.fh != nil {
28+
t.Fatal("file handle should have been released and be nil")
29+
}
30+
}

0 commit comments

Comments
 (0)