Skip to content

Commit

Permalink
Merge pull request #32 from egonelbre/master
Browse files Browse the repository at this point in the history
Add New method and deprecate NewFlock

Signed-off-by: Tim Heckman <t@heckman.io>
  • Loading branch information
theckman committed Oct 5, 2018
2 parents 6cb7b4e + 4a2baac commit 7f43ea2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -22,7 +22,7 @@ go get -u github.com/gofrs/flock
```Go
import "github.com/gofrs/flock"

fileLock := flock.NewFlock("/var/lock/go-lock.lock")
fileLock := flock.New("/var/lock/go-lock.lock")

locked, err := fileLock.TryLock()

Expand Down
10 changes: 9 additions & 1 deletion flock.go
Expand Up @@ -33,10 +33,18 @@ type Flock struct {
r bool
}

// New returns a new instance of *Flock. The only parameter
// it takes is the path to the desired lockfile.
func New(path string) *Flock {
return &Flock{path: path}
}

// NewFlock returns a new instance of *Flock. The only parameter
// it takes is the path to the desired lockfile.
//
// Deprecated: Use New instead.
func NewFlock(path string) *Flock {
return &Flock{path: path}
return New(path)
}

// Close is equivalent to calling Unlock.
Expand Down
6 changes: 3 additions & 3 deletions flock_example_test.go
Expand Up @@ -15,7 +15,7 @@ import (
)

func ExampleFlock_Locked() {
f := flock.NewFlock(os.TempDir() + "/go-lock.lock")
f := flock.New(os.TempDir() + "/go-lock.lock")
f.TryLock() // unchecked errors here

fmt.Printf("locked: %v\n", f.Locked())
Expand All @@ -29,7 +29,7 @@ func ExampleFlock_Locked() {

func ExampleFlock_TryLock() {
// should probably put these in /var/lock
fileLock := flock.NewFlock(os.TempDir() + "/go-lock.lock")
fileLock := flock.New(os.TempDir() + "/go-lock.lock")

locked, err := fileLock.TryLock()

Expand All @@ -50,7 +50,7 @@ func ExampleFlock_TryLock() {

func ExampleFlock_TryLockContext() {
// should probably put these in /var/lock
fileLock := flock.NewFlock(os.TempDir() + "/go-lock.lock")
fileLock := flock.New(os.TempDir() + "/go-lock.lock")

lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
Expand Down
24 changes: 12 additions & 12 deletions flock_test.go
Expand Up @@ -36,18 +36,18 @@ func (t *TestSuite) SetUpTest(c *C) {
defer os.Remove(t.path)
tmpFile.Close()

t.flock = flock.NewFlock(t.path)
t.flock = flock.New(t.path)
}

func (t *TestSuite) TearDownTest(c *C) {
t.flock.Unlock()
os.Remove(t.path)
}

func (t *TestSuite) TestNewFlock(c *C) {
func (t *TestSuite) TestNew(c *C) {
var f *flock.Flock

f = flock.NewFlock(t.path)
f = flock.New(t.path)
c.Assert(f, Not(IsNil))
c.Check(f.Path(), Equals, t.path)
c.Check(f.Locked(), Equals, false)
Expand Down Expand Up @@ -97,7 +97,7 @@ func (t *TestSuite) TestFlock_TryLock(c *C) {

// make sure we just return false with no error in cases
// where we would have been blocked
locked, err = flock.NewFlock(t.path).TryLock()
locked, err = flock.New(t.path).TryLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, false)
}
Expand All @@ -120,7 +120,7 @@ func (t *TestSuite) TestFlock_TryRLock(c *C) {
c.Check(locked, Equals, true)

// shared lock should not block.
flock2 := flock.NewFlock(t.path)
flock2 := flock.New(t.path)
locked, err = flock2.TryRLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, true)
Expand All @@ -130,7 +130,7 @@ func (t *TestSuite) TestFlock_TryRLock(c *C) {
t.flock.Unlock()
flock2.Unlock()
t.flock.Lock()
locked, err = flock.NewFlock(t.path).TryRLock()
locked, err = flock.New(t.path).TryRLock()
c.Assert(err, IsNil)
c.Check(locked, Equals, false)
}
Expand All @@ -144,14 +144,14 @@ func (t *TestSuite) TestFlock_TryLockContext(c *C) {

// context already canceled
cancel()
locked, err = flock.NewFlock(t.path).TryLockContext(ctx, time.Second)
locked, err = flock.New(t.path).TryLockContext(ctx, time.Second)
c.Assert(err, Equals, context.Canceled)
c.Check(locked, Equals, false)

// timeout
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
locked, err = flock.NewFlock(t.path).TryLockContext(ctx, time.Second)
locked, err = flock.New(t.path).TryLockContext(ctx, time.Second)
c.Assert(err, Equals, context.DeadlineExceeded)
c.Check(locked, Equals, false)
}
Expand All @@ -165,7 +165,7 @@ func (t *TestSuite) TestFlock_TryRLockContext(c *C) {

// context already canceled
cancel()
locked, err = flock.NewFlock(t.path).TryRLockContext(ctx, time.Second)
locked, err = flock.New(t.path).TryRLockContext(ctx, time.Second)
c.Assert(err, Equals, context.Canceled)
c.Check(locked, Equals, false)

Expand All @@ -174,7 +174,7 @@ func (t *TestSuite) TestFlock_TryRLockContext(c *C) {
t.flock.Lock()
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
locked, err = flock.NewFlock(t.path).TryRLockContext(ctx, time.Second)
locked, err = flock.New(t.path).TryRLockContext(ctx, time.Second)
c.Assert(err, Equals, context.DeadlineExceeded)
c.Check(locked, Equals, false)
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func (t *TestSuite) TestFlock_Lock(c *C) {
// Test that Lock() is a blocking call
//
ch := make(chan error, 2)
gf := flock.NewFlock(t.path)
gf := flock.New(t.path)
defer gf.Unlock()

go func(ch chan<- error) {
Expand Down Expand Up @@ -264,7 +264,7 @@ func (t *TestSuite) TestFlock_RLock(c *C) {
// Test that RLock() is a blocking call
//
ch := make(chan error, 2)
gf := flock.NewFlock(t.path)
gf := flock.New(t.path)
defer gf.Unlock()

go func(ch chan<- error) {
Expand Down

0 comments on commit 7f43ea2

Please sign in to comment.