Skip to content

Commit c08bb66

Browse files
authored
tests: add tests about directory support (#89)
1 parent 5169644 commit c08bb66

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

flock_internal_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package flock
99

1010
import (
1111
"os"
12+
"runtime"
1213
"testing"
1314

1415
"github.com/stretchr/testify/assert"
@@ -42,3 +43,28 @@ func TestFlock_fh_onError(t *testing.T) {
4243
err = lock.Unlock()
4344
require.NoError(t, err)
4445
}
46+
47+
func TestFlock_fh_onError_dir(t *testing.T) {
48+
if runtime.GOOS == "windows" {
49+
t.Skip("not supported on Windows")
50+
}
51+
52+
tmpDir := t.TempDir()
53+
54+
lock := New(tmpDir, SetFlag(os.O_RDONLY))
55+
56+
locked, err := lock.TryLock()
57+
require.NoError(t, err)
58+
require.True(t, locked)
59+
60+
newLock := New(tmpDir, SetFlag(os.O_RDONLY))
61+
62+
locked, err = newLock.TryLock()
63+
require.NoError(t, err)
64+
require.False(t, locked)
65+
66+
assert.Nil(t, newLock.fh, "file handle should have been released and be nil")
67+
68+
err = lock.Unlock()
69+
require.NoError(t, err)
70+
}

flock_test.go

+33-12
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,34 @@ import (
2121
type TestSuite struct {
2222
suite.Suite
2323

24+
dir bool
25+
opts []flock.Option
26+
2427
path string
2528
flock *flock.Flock
2629
}
2730

28-
func Test(t *testing.T) { suite.Run(t, &TestSuite{}) }
31+
func Test(t *testing.T) {
32+
suite.Run(t, &TestSuite{})
33+
}
34+
35+
func Test_dir(t *testing.T) {
36+
if runtime.GOOS == "windows" {
37+
t.Skip("not supported on Windows")
38+
}
39+
40+
suite.Run(t, &TestSuite{dir: true, opts: []flock.Option{flock.SetFlag(os.O_RDONLY)}})
41+
}
2942

3043
func (s *TestSuite) SetupTest() {
44+
if s.dir {
45+
s.path = s.T().TempDir()
46+
47+
s.flock = flock.New(s.path, s.opts...)
48+
49+
return
50+
}
51+
3152
tmpFile, err := os.CreateTemp(s.T().TempDir(), "go-flock-")
3253
s.Require().NoError(err)
3354

@@ -41,7 +62,7 @@ func (s *TestSuite) SetupTest() {
4162
err = os.Remove(s.path)
4263
s.Require().NoError(err)
4364

44-
s.flock = flock.New(s.path)
65+
s.flock = flock.New(s.path, s.opts...)
4566
}
4667

4768
func (s *TestSuite) TearDownTest() {
@@ -50,7 +71,7 @@ func (s *TestSuite) TearDownTest() {
5071
}
5172

5273
func (s *TestSuite) TestNew() {
53-
f := flock.New(s.path)
74+
f := flock.New(s.path, s.opts...)
5475
s.Require().NotNil(f)
5576

5677
s.Equal(f.Path(), s.path)
@@ -90,7 +111,7 @@ func (s *TestSuite) TestFlock_TryLock() {
90111

91112
// make sure we just return false with no error in cases
92113
// where we would have been blocked
93-
locked, err = flock.New(s.path).TryLock()
114+
locked, err = flock.New(s.path, s.opts...).TryLock()
94115
s.Require().NoError(err)
95116
s.False(locked)
96117
}
@@ -110,7 +131,7 @@ func (s *TestSuite) TestFlock_TryRLock() {
110131
s.True(locked)
111132

112133
// shared lock should not block.
113-
flock2 := flock.New(s.path)
134+
flock2 := flock.New(s.path, s.opts...)
114135
locked, err = flock2.TryRLock()
115136
s.Require().NoError(err)
116137

@@ -131,7 +152,7 @@ func (s *TestSuite) TestFlock_TryRLock() {
131152
_ = s.flock.Unlock()
132153
_ = flock2.Unlock()
133154
_ = s.flock.Lock()
134-
locked, err = flock.New(s.path).TryRLock()
155+
locked, err = flock.New(s.path, s.opts...).TryRLock()
135156
s.Require().NoError(err)
136157
s.False(locked)
137158
}
@@ -147,15 +168,15 @@ func (s *TestSuite) TestFlock_TryLockContext() {
147168
// context already canceled
148169
cancel()
149170

150-
locked, err = flock.New(s.path).TryLockContext(ctx, time.Second)
171+
locked, err = flock.New(s.path, s.opts...).TryLockContext(ctx, time.Second)
151172
s.Require().ErrorIs(err, context.Canceled)
152173
s.False(locked)
153174

154175
// timeout
155176
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
156177
defer cancel()
157178

158-
locked, err = flock.New(s.path).TryLockContext(ctx, time.Second)
179+
locked, err = flock.New(s.path, s.opts...).TryLockContext(ctx, time.Second)
159180
s.Require().ErrorIs(err, context.DeadlineExceeded)
160181
s.False(locked)
161182
}
@@ -171,7 +192,7 @@ func (s *TestSuite) TestFlock_TryRLockContext() {
171192
// context already canceled
172193
cancel()
173194

174-
locked, err = flock.New(s.path).TryRLockContext(ctx, time.Second)
195+
locked, err = flock.New(s.path, s.opts...).TryRLockContext(ctx, time.Second)
175196
s.Require().ErrorIs(err, context.Canceled)
176197
s.False(locked)
177198

@@ -182,7 +203,7 @@ func (s *TestSuite) TestFlock_TryRLockContext() {
182203
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
183204
defer cancel()
184205

185-
locked, err = flock.New(s.path).TryRLockContext(ctx, time.Second)
206+
locked, err = flock.New(s.path, s.opts...).TryRLockContext(ctx, time.Second)
186207
s.Require().ErrorIs(err, context.DeadlineExceeded)
187208
s.False(locked)
188209
}
@@ -224,7 +245,7 @@ func (s *TestSuite) TestFlock_Lock() {
224245
// Test that Lock() is a blocking call
225246
//
226247
ch := make(chan error, 2)
227-
gf := flock.New(s.path)
248+
gf := flock.New(s.path, s.opts...)
228249
defer func() { _ = gf.Unlock() }()
229250

230251
go func(ch chan<- error) {
@@ -266,7 +287,7 @@ func (s *TestSuite) TestFlock_RLock() {
266287
// Test that RLock() is a blocking call
267288
//
268289
ch := make(chan error, 2)
269-
gf := flock.New(s.path)
290+
gf := flock.New(s.path, s.opts...)
270291
defer func() { _ = gf.Unlock() }()
271292

272293
go func(ch chan<- error) {

0 commit comments

Comments
 (0)