2020-05-20 12:28:08 +00:00
|
|
|
package locker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dbMock struct {
|
|
|
|
db *sql.DB
|
|
|
|
mock sqlmock.Sqlmock
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockDB(t *testing.T) *dbMock {
|
|
|
|
mockDB := dbMock{}
|
|
|
|
var err error
|
|
|
|
mockDB.db, mockDB.mock, err = sqlmock.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error occured while creating stub db %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mockDB.mock.MatchExpectationsInOrder(true)
|
|
|
|
|
|
|
|
return &mockDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *dbMock) expectCommit() *dbMock {
|
|
|
|
db.mock.ExpectCommit()
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *dbMock) expectRollback() *dbMock {
|
|
|
|
db.mock.ExpectRollback()
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *dbMock) expectBegin() *dbMock {
|
|
|
|
db.mock.ExpectBegin()
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *dbMock) expectSavepoint() *dbMock {
|
|
|
|
db.mock.ExpectExec("SAVEPOINT").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *dbMock) expectReleaseSavepoint() *dbMock {
|
|
|
|
db.mock.ExpectExec("RELEASE SAVEPOINT").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (db *dbMock) expectRenew(lockerID, view, instanceID string, affectedRows int64) *dbMock {
|
2020-05-20 12:28:08 +00:00
|
|
|
query := db.mock.
|
2022-04-19 06:26:12 +00:00
|
|
|
ExpectExec(`INSERT INTO table\.locks \(locker_id, locked_until, view_name, instance_id\) VALUES \(\$1, now\(\)\+\$2::INTERVAL, \$3\, \$4\) ON CONFLICT \(view_name, instance_id\) DO UPDATE SET locker_id = \$1, locked_until = now\(\)\+\$2::INTERVAL WHERE locks\.view_name = \$3 AND locks\.instance_id = \$4 AND \(locks\.locker_id = \$1 OR locks\.locked_until < now\(\)\)`).
|
|
|
|
WithArgs(lockerID, sqlmock.AnyArg(), view, instanceID).
|
2020-05-20 12:28:08 +00:00
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
|
|
|
if affectedRows == 0 {
|
|
|
|
query.WillReturnResult(sqlmock.NewResult(0, 0))
|
|
|
|
} else {
|
|
|
|
query.WillReturnResult(sqlmock.NewResult(1, affectedRows))
|
|
|
|
}
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_locker_Renew(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
db *dbMock
|
|
|
|
}
|
|
|
|
type args struct {
|
2022-04-19 06:26:12 +00:00
|
|
|
tableName string
|
|
|
|
lockerID string
|
|
|
|
viewModel string
|
|
|
|
instanceID string
|
|
|
|
waitTime time.Duration
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "renew succeeded",
|
|
|
|
fields: fields{
|
|
|
|
db: mockDB(t).
|
|
|
|
expectBegin().
|
|
|
|
expectSavepoint().
|
2022-04-19 06:26:12 +00:00
|
|
|
expectRenew("locker", "view", "instanceID", 1).
|
2020-05-20 12:28:08 +00:00
|
|
|
expectReleaseSavepoint().
|
|
|
|
expectCommit(),
|
|
|
|
},
|
2022-04-19 06:26:12 +00:00
|
|
|
args: args{tableName: "table.locks", lockerID: "locker", viewModel: "view", instanceID: "instanceID", waitTime: 1 * time.Second},
|
2020-05-20 12:28:08 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "renew now rows updated",
|
|
|
|
fields: fields{
|
|
|
|
db: mockDB(t).
|
|
|
|
expectBegin().
|
|
|
|
expectSavepoint().
|
2022-04-19 06:26:12 +00:00
|
|
|
expectRenew("locker", "view", "instanceID", 0).
|
2020-05-20 12:28:08 +00:00
|
|
|
expectRollback(),
|
|
|
|
},
|
2022-04-19 06:26:12 +00:00
|
|
|
args: args{tableName: "table.locks", lockerID: "locker", viewModel: "view", instanceID: "instanceID", waitTime: 1 * time.Second},
|
2020-05-20 12:28:08 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-04-19 06:26:12 +00:00
|
|
|
if err := Renew(tt.fields.db.db, tt.args.tableName, tt.args.lockerID, tt.args.viewModel, tt.args.instanceID, tt.args.waitTime); (err != nil) != tt.wantErr {
|
2020-05-20 12:28:08 +00:00
|
|
|
t.Errorf("locker.Renew() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if err := tt.fields.db.mock.ExpectationsWereMet(); err != nil {
|
|
|
|
t.Errorf("not all database expectations met: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|