idempotency_cleanup_service_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/Wei-Shaw/sub2api/internal/config"
  7. "github.com/stretchr/testify/require"
  8. )
  9. type idempotencyCleanupRepoStub struct {
  10. deleteCalls int
  11. lastLimit int
  12. deleteErr error
  13. }
  14. func (r *idempotencyCleanupRepoStub) CreateProcessing(context.Context, *IdempotencyRecord) (bool, error) {
  15. return false, nil
  16. }
  17. func (r *idempotencyCleanupRepoStub) GetByScopeAndKeyHash(context.Context, string, string) (*IdempotencyRecord, error) {
  18. return nil, nil
  19. }
  20. func (r *idempotencyCleanupRepoStub) TryReclaim(context.Context, int64, string, time.Time, time.Time, time.Time) (bool, error) {
  21. return false, nil
  22. }
  23. func (r *idempotencyCleanupRepoStub) ExtendProcessingLock(context.Context, int64, string, time.Time, time.Time) (bool, error) {
  24. return false, nil
  25. }
  26. func (r *idempotencyCleanupRepoStub) MarkSucceeded(context.Context, int64, int, string, time.Time) error {
  27. return nil
  28. }
  29. func (r *idempotencyCleanupRepoStub) MarkFailedRetryable(context.Context, int64, string, time.Time, time.Time) error {
  30. return nil
  31. }
  32. func (r *idempotencyCleanupRepoStub) DeleteExpired(_ context.Context, _ time.Time, limit int) (int64, error) {
  33. r.deleteCalls++
  34. r.lastLimit = limit
  35. if r.deleteErr != nil {
  36. return 0, r.deleteErr
  37. }
  38. return 1, nil
  39. }
  40. func TestNewIdempotencyCleanupService_UsesConfig(t *testing.T) {
  41. repo := &idempotencyCleanupRepoStub{}
  42. cfg := &config.Config{
  43. Idempotency: config.IdempotencyConfig{
  44. CleanupIntervalSeconds: 7,
  45. CleanupBatchSize: 321,
  46. },
  47. }
  48. svc := NewIdempotencyCleanupService(repo, cfg)
  49. require.Equal(t, 7*time.Second, svc.interval)
  50. require.Equal(t, 321, svc.batch)
  51. }
  52. func TestIdempotencyCleanupService_CleanupOnce(t *testing.T) {
  53. repo := &idempotencyCleanupRepoStub{}
  54. svc := NewIdempotencyCleanupService(repo, &config.Config{
  55. Idempotency: config.IdempotencyConfig{
  56. CleanupBatchSize: 99,
  57. },
  58. })
  59. svc.cleanupOnce()
  60. require.Equal(t, 1, repo.deleteCalls)
  61. require.Equal(t, 99, repo.lastLimit)
  62. }