This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
forked from ninthclowd/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
106 lines (99 loc) · 2.26 KB
/
util_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package database
import (
"errors"
"go.uber.org/atomic"
"testing"
)
func TestGenerateAdvisoryLockId(t *testing.T) {
testcases := []struct {
dbname string
additional []string
expectedID string // empty string signifies that an error is expected
}{
{
dbname: "database_name",
expectedID: "1764327054",
},
{
dbname: "database_name",
additional: []string{"schema_name_1"},
expectedID: "2453313553",
},
{
dbname: "database_name",
additional: []string{"schema_name_2"},
expectedID: "235207038",
},
{
dbname: "database_name",
additional: []string{"schema_name_1", "schema_name_2"},
expectedID: "3743845847",
},
}
for _, tc := range testcases {
t.Run(tc.dbname, func(t *testing.T) {
if id, err := GenerateAdvisoryLockId(tc.dbname, tc.additional...); err == nil {
if id != tc.expectedID {
t.Error("Generated incorrect ID:", id, "!=", tc.expectedID)
}
} else {
if tc.expectedID != "" {
t.Error("Got unexpected error:", err)
}
}
})
}
}
func TestCasRestoreOnErr(t *testing.T) {
casErr := errors.New("test lock CAS failure")
fErr := errors.New("test callback error")
testcases := []struct {
name string
lock *atomic.Bool
from bool
to bool
expectLock bool
fErr error
expectError error
}{
{
name: "Test positive CAS lock",
lock: atomic.NewBool(false),
from: false,
to: true,
expectLock: true,
fErr: nil,
expectError: nil,
},
{
name: "Test negative CAS lock",
lock: atomic.NewBool(true),
from: false,
to: true,
expectLock: true,
fErr: nil,
expectError: casErr,
},
{
name: "Test negative with callback lock",
lock: atomic.NewBool(false),
from: false,
to: true,
expectLock: false,
fErr: fErr,
expectError: fErr,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
if err := CasRestoreOnErr(tc.lock, tc.from, tc.to, casErr, func() error {
return tc.fErr
}); err != tc.expectError {
t.Error("Incorrect error value returned")
}
if tc.lock.Load() != tc.expectLock {
t.Error("Incorrect state of lock")
}
})
}
}