Skip to content

Commit 700936d

Browse files
committed
test: tokenLock queries
Signed-off-by: ADIR <adir@il.ibm.com>
1 parent 3c9fbd5 commit 700936d

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

token/services/db/sql/postgres/tokenlock.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ func NewTokenLockStore(dbs *common2.RWDB, tableNames common.TableNames) (*TokenL
4040
}
4141

4242
func (db *TokenLockStore) Cleanup(ctx context.Context, leaseExpiry time.Duration) error {
43-
_, err := db.logStaleLocks(ctx, leaseExpiry)
44-
if err != nil {
43+
if _, err := db.logStaleLocks(ctx, leaseExpiry); err != nil {
4544
db.Logger.Warnf("Could not log stale locks: %v", err)
4645
}
4746
tokenLocks, tokenRequests := q.Table(db.Table.TokenLocks), q.Table(db.Table.Requests)
@@ -57,14 +56,14 @@ func (db *TokenLockStore) Cleanup(ctx context.Context, leaseExpiry time.Duration
5756
Build()
5857

5958
db.Logger.Debug(query)
60-
_, err = db.WriteDB.ExecContext(ctx, query, args...)
59+
_, err := db.WriteDB.ExecContext(ctx, query, args...)
6160
if err != nil {
6261
db.Logger.Errorf("query failed: %s", query)
6362
}
6463
return err
6564
}
6665

67-
func (db *TokenLockStore) logStaleLocks(ctx context.Context, leaseExpiry time.Duration) ([]LockEntry, error) {
66+
func (db *TokenLockStore) logStaleLocks(ctx context.Context, leaseExpiry time.Duration) ([]lockEntry, error) {
6867
if !db.Logger.IsEnabledFor(zapcore.InfoLevel) {
6968
return nil, nil
7069
}
@@ -84,7 +83,7 @@ func (db *TokenLockStore) logStaleLocks(ctx context.Context, leaseExpiry time.Du
8483
return nil, err
8584
}
8685

87-
it := common4.NewIterator(rows, func(entry *LockEntry) error {
86+
it := common4.NewIterator(rows, func(entry *lockEntry) error {
8887
entry.LeaseExpiry = leaseExpiry
8988
return rows.Scan(&entry.ConsumerTxID, &entry.TokenID.TxId, &entry.TokenID.Index, &entry.Status, &entry.CreatedAt, &entry.Now)
9089
})
@@ -97,7 +96,7 @@ func (db *TokenLockStore) logStaleLocks(ctx context.Context, leaseExpiry time.Du
9796
return lockEntries, nil
9897
}
9998

100-
type LockEntry struct {
99+
type lockEntry struct {
101100
ConsumerTxID string
102101
TokenID token.ID
103102
Status *driver.TxStatus
@@ -106,11 +105,11 @@ type LockEntry struct {
106105
LeaseExpiry time.Duration
107106
}
108107

109-
func (e LockEntry) Expired() bool {
108+
func (e lockEntry) Expired() bool {
110109
return e.CreatedAt.Add(e.LeaseExpiry).Before(e.Now)
111110
}
112111

113-
func (e LockEntry) String() string {
112+
func (e lockEntry) String() string {
114113
if expired := e.Expired(); e.Status == nil && expired {
115114
return fmt.Sprintf("Expired lock created at [%v] for token [%s] consumed by [%s]", e.CreatedAt, e.TokenID, e.ConsumerTxID)
116115
} else if e.Status != nil && *e.Status == driver.Deleted && !expired {

token/services/db/sql/postgres/tokenlock_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,21 @@ func mockTokenLockStore(db *sql.DB) *common.TokenLockStore {
4545
return store.TokenLockStore
4646
}
4747

48-
func TestLogStaleLocks(t *testing.T) {
48+
func TestCleanup(t *testing.T) {
4949
gomega.RegisterTestingT(t)
5050
db, mockDB, err := sqlmock.New()
5151
gomega.Expect(err).ToNot(gomega.HaveOccurred())
5252

5353
input := driver.Deleted
5454

55-
record := LockEntry{
56-
ConsumerTxID: "1234",
57-
TokenID: token.ID{TxId: "5678", Index: 5},
58-
Status: &input,
59-
CreatedAt: time.Date(2025, time.June, 8, 10, 0, 0, 0, time.UTC),
60-
}
55+
consumerTxID := "1234"
56+
tokenID := token.ID{TxId: "5678", Index: 5}
57+
status := &input
58+
createdAt := time.Date(2025, time.June, 8, 10, 0, 0, 0, time.UTC)
59+
6160
var timeNow time.Time
6261
output := []driver2.Value{
63-
record.ConsumerTxID, record.TokenID.TxId, record.TokenID.Index, *record.Status, record.CreatedAt, timeNow,
62+
consumerTxID, tokenID.TxId, tokenID.Index, *status, createdAt, timeNow,
6463
}
6564
mockDB.
6665
ExpectQuery("SELECT TOKEN_LOCKS.consumer_tx_id, TOKEN_LOCKS.tx_id, TOKEN_LOCKS.idx, REQUESTS.status, TOKEN_LOCKS.created_at, NOW\\(\\) AS now " +

0 commit comments

Comments
 (0)