-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextra.go
55 lines (48 loc) · 1.5 KB
/
extra.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
//go:build assert
package assert
import "sync"
// A TryLocker represents an object that can attempt to acquire a lock and report
// whether it succeeded.
//
// [sync.Mutex] and [sync.RWMutex] implements this interface.
type TryLocker interface {
sync.Locker
TryLock() bool
}
// Locked asserts that the [TryLocker] is already locked.
//
// assert.IsDecreasing([]int{2, 1, 0})
// assert.IsDecreasing([]float{2, 1})
// assert.IsDecreasing([]string{"b", "a"})
func Locked(locker TryLocker, msgAndArgs ...any) {
if locker.TryLock() {
Fail("Expected sync.Locker to be locked", msgAndArgs...)
}
}
// Lockedf asserts that the [TryLocker] is already locked.
//
// assert.IsDecreasing([]int{2, 1, 0})
// assert.IsDecreasing([]float{2, 1})
// assert.IsDecreasing([]string{"b", "a"})
func Lockedf(locker TryLocker, msg string, args ...any) {
Locked(locker, append([]interface{}{msg}, args...)...)
}
// Unlocked asserts that the [TryLocker] is unlocked.
//
// assert.IsDecreasing([]int{2, 1, 0})
// assert.IsDecreasing([]float{2, 1})
// assert.IsDecreasing([]string{"b", "a"})
func Unlocked(locker TryLocker, msgAndArgs ...any) {
if !locker.TryLock() {
Fail("Expected sync.Locker to be unlocked", msgAndArgs...)
}
locker.Unlock()
}
// UnLockedf asserts that the [TryLocker] is unlocked.
//
// assert.IsDecreasing([]int{2, 1, 0})
// assert.IsDecreasing([]float{2, 1})
// assert.IsDecreasing([]string{"b", "a"})
func Unlockedf(locker TryLocker, msg string, args ...any) {
Unlocked(locker, append([]interface{}{msg}, args...)...)
}