-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathflock_internal_test.go
70 lines (49 loc) · 1.47 KB
/
flock_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2015 Tim Heckman. All rights reserved.
// Copyright 2018-2025 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.
//go:build !js && !plan9 && !wasip1
package flock
import (
"os"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFlock_fh_onError(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "go-flock-")
require.NoError(t, err)
err = tmpFile.Close()
require.NoError(t, err)
err = os.Remove(tmpFile.Name())
require.NoError(t, err)
lock := New(tmpFile.Name())
locked, err := lock.TryLock()
require.NoError(t, err)
require.True(t, locked)
newLock := New(tmpFile.Name())
locked, err = newLock.TryLock()
require.NoError(t, err)
require.False(t, locked)
assert.Nil(t, newLock.fh, "file handle should have been released and be nil")
err = lock.Unlock()
require.NoError(t, err)
}
func TestFlock_fh_onError_dir(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("not supported on Windows")
}
tmpDir := t.TempDir()
lock := New(tmpDir, SetFlag(os.O_RDONLY))
locked, err := lock.TryLock()
require.NoError(t, err)
require.True(t, locked)
newLock := New(tmpDir, SetFlag(os.O_RDONLY))
locked, err = newLock.TryLock()
require.NoError(t, err)
require.False(t, locked)
assert.Nil(t, newLock.fh, "file handle should have been released and be nil")
err = lock.Unlock()
require.NoError(t, err)
}