Skip to content

Commit 71bfde6

Browse files
committed
fix/refactor test cas restore function
1 parent c4fd509 commit 71bfde6

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

database/util_test.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package database
22

33
import (
4+
"errors"
45
"go.uber.org/atomic"
56
"testing"
67
)
@@ -48,12 +49,14 @@ func TestGenerateAdvisoryLockId(t *testing.T) {
4849
}
4950

5051
func TestCasRestoreOnErr(t *testing.T) {
52+
casErr := errors.New("test lock CAS failure")
53+
fErr := errors.New("test callback error")
54+
5155
testcases := []struct {
5256
name string
5357
lock *atomic.Bool
5458
from bool
5559
to bool
56-
casErr error
5760
fErr error
5861
expectLock bool
5962
expectError error
@@ -63,46 +66,41 @@ func TestCasRestoreOnErr(t *testing.T) {
6366
lock: atomic.NewBool(false),
6467
from: false,
6568
to: true,
66-
casErr: ErrLocked,
6769
fErr: nil,
68-
expectError: nil,
6970
expectLock: true,
71+
expectError: nil,
7072
},
7173
{
7274
name: "Test negative CAS lock",
7375
lock: atomic.NewBool(true),
7476
from: false,
7577
to: true,
76-
casErr: ErrLocked,
7778
fErr: nil,
7879
expectLock: true,
79-
expectError: ErrLocked,
80+
expectError: casErr,
8081
},
8182
{
8283
name: "Test negative with callback lock",
8384
lock: atomic.NewBool(false),
8485
from: false,
8586
to: true,
86-
casErr: ErrLocked,
87-
fErr: ErrNotLocked,
87+
fErr: fErr,
8888
expectLock: false,
89-
expectError: ErrNotLocked,
89+
expectError: fErr,
9090
},
9191
}
9292

9393
for _, tc := range testcases {
9494
t.Run(tc.name, func(t *testing.T) {
95-
err := CasRestoreOnErr(tc.lock, tc.from, tc.to, tc.casErr, func() error {
95+
if err := CasRestoreOnErr(tc.lock, tc.from, tc.to, casErr, func() error {
9696
return tc.fErr
97-
})
97+
}); err != tc.expectError {
98+
t.Error("Incorrect error value returned")
99+
}
98100

99101
if tc.lock.Load() != tc.expectLock {
100102
t.Error("Incorrect state of lock")
101103
}
102-
103-
if err != tc.expectError {
104-
t.Error("Incorrect error value returned")
105-
}
106104
})
107105
}
108106
}

0 commit comments

Comments
 (0)