From 4a2baacd88d3b69203c5028b48e6222ab7f166f9 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Tue, 18 Sep 2018 09:39:43 +0300 Subject: [PATCH] Add New method and deprecate NewFlock --- README.md | 2 +- flock.go | 10 +++++++++- flock_example_test.go | 6 +++--- flock_test.go | 24 ++++++++++++------------ 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index fface56..42d580f 100644 --- a/README.md +++ b/README.md @@ -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() diff --git a/flock.go b/flock.go index 7442bf0..5783a49 100644 --- a/flock.go +++ b/flock.go @@ -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. diff --git a/flock_example_test.go b/flock_example_test.go index 14d4287..0c2afa5 100644 --- a/flock_example_test.go +++ b/flock_example_test.go @@ -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()) @@ -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() @@ -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() diff --git a/flock_test.go b/flock_test.go index f672411..e277e52 100644 --- a/flock_test.go +++ b/flock_test.go @@ -36,7 +36,7 @@ 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) { @@ -44,10 +44,10 @@ func (t *TestSuite) TearDownTest(c *C) { 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) @@ -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) } @@ -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) @@ -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) } @@ -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) } @@ -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) @@ -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) } @@ -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) { @@ -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) {