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 9fa3b6d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions flock_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2015 Tim Heckman. All rights reserved.
// Copyright 2018 The Gofrs. All rights reserved.
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.

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 9fa3b6d

Please sign in to comment.