mirror of
https://github.com/hanzoai/migrate.git
synced 2026-09-30 22:55:10 +00:00
* used uber atomic bool instead standard in lock/unlock db * fixing local lock optimization implementation * fixing mysql local lock optimization implementation * fixing cockroachdb local lock optimization implementation * CAS wrapper to automatically restore * added test for cas with restore * added atomic lock to sqlite & mongo * added atomic lock to sqlite & mongo * fix/refactor test cas restore function * disable lock for mongo * mongo local lock before acquiring the db lock Co-authored-by: a.prinkov <[email protected]>
107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
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")
|
|
}
|
|
})
|
|
}
|
|
}
|