Skip to content

Commit

Permalink
test that a failed file lock closes the file
Browse files Browse the repository at this point in the history
  • Loading branch information
azr committed Aug 23, 2019
1 parent c6c7c77 commit 958d664
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions flock_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package flock

import (
"io/ioutil"
"os"
"testing"
)

func Test(t *testing.T) {
tmpFileFh, err := ioutil.TempFile(os.TempDir(), "go-flock-")
tmpFileFh.Close()
tmpFile := tmpFileFh.Name()
os.Remove(tmpFile)

lock := New(tmpFile)
locked, err := lock.TryLock()
if locked == false || err != nil {
t.Fatal("failed to lock")
}

newLock := New(tmpFile)
locked, err = newLock.TryLock()
if locked != false || err != nil {
t.Fatal("should have failed locking")
}

if newLock.fh != nil {
t.Fatal("file handle should have been released and be nil")
}
}

0 comments on commit 958d664

Please sign in to comment.