Files
tokens-reef/backend/internal/service/idempotency_cleanup_service_test.go
Developer da36506b89 fix: resolve P0/P1 code quality issues
P0 fixes:
- ModelError.Is(): use exact matching instead of substring contains()
- shouldClearStickySession: add context param for cancellation/tracing

P1 fixes:
- TODO stubs: return 501 Not Implemented errors
- validateInstanceSignature: deduplicate to shared validateCodeSignature()
- Error messages: standardize to English only
- http.go: remove pseudo if-else with duplicate branches
2026-03-31 11:39:18 +08:00

70 lines
1.9 KiB
Go

package service
import (
"context"
"testing"
"time"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/stretchr/testify/require"
)
type idempotencyCleanupRepoStub struct {
deleteCalls int
lastLimit int
deleteErr error
}
func (r *idempotencyCleanupRepoStub) CreateProcessing(context.Context, *IdempotencyRecord) (bool, error) {
return false, nil
}
func (r *idempotencyCleanupRepoStub) GetByScopeAndKeyHash(context.Context, string, string) (*IdempotencyRecord, error) {
return nil, nil
}
func (r *idempotencyCleanupRepoStub) TryReclaim(context.Context, int64, string, time.Time, time.Time, time.Time) (bool, error) {
return false, nil
}
func (r *idempotencyCleanupRepoStub) ExtendProcessingLock(context.Context, int64, string, time.Time, time.Time) (bool, error) {
return false, nil
}
func (r *idempotencyCleanupRepoStub) MarkSucceeded(context.Context, int64, int, string, time.Time) error {
return nil
}
func (r *idempotencyCleanupRepoStub) MarkFailedRetryable(context.Context, int64, string, time.Time, time.Time) error {
return nil
}
func (r *idempotencyCleanupRepoStub) DeleteExpired(_ context.Context, _ time.Time, limit int) (int64, error) {
r.deleteCalls++
r.lastLimit = limit
if r.deleteErr != nil {
return 0, r.deleteErr
}
return 1, nil
}
func TestNewIdempotencyCleanupService_UsesConfig(t *testing.T) {
repo := &idempotencyCleanupRepoStub{}
cfg := &config.Config{
Idempotency: config.IdempotencyConfig{
CleanupIntervalSeconds: 7,
CleanupBatchSize: 321,
},
}
svc := NewIdempotencyCleanupService(repo, cfg)
require.Equal(t, 7*time.Second, svc.interval)
require.Equal(t, 321, svc.batch)
}
func TestIdempotencyCleanupService_CleanupOnce(t *testing.T) {
repo := &idempotencyCleanupRepoStub{}
svc := NewIdempotencyCleanupService(repo, &config.Config{
Idempotency: config.IdempotencyConfig{
CleanupBatchSize: 99,
},
})
svc.cleanupOnce()
require.Equal(t, 1, repo.deleteCalls)
require.Equal(t, 99, repo.lastLimit)
}