idempotency_helper_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package handler
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "net/http"
  7. "net/http/httptest"
  8. "sync"
  9. "sync/atomic"
  10. "testing"
  11. "time"
  12. "github.com/Wei-Shaw/sub2api/internal/server/middleware"
  13. "github.com/Wei-Shaw/sub2api/internal/service"
  14. "github.com/gin-gonic/gin"
  15. "github.com/stretchr/testify/require"
  16. )
  17. type userStoreUnavailableRepoStub struct{}
  18. func (userStoreUnavailableRepoStub) CreateProcessing(context.Context, *service.IdempotencyRecord) (bool, error) {
  19. return false, errors.New("store unavailable")
  20. }
  21. func (userStoreUnavailableRepoStub) GetByScopeAndKeyHash(context.Context, string, string) (*service.IdempotencyRecord, error) {
  22. return nil, errors.New("store unavailable")
  23. }
  24. func (userStoreUnavailableRepoStub) TryReclaim(context.Context, int64, string, time.Time, time.Time, time.Time) (bool, error) {
  25. return false, errors.New("store unavailable")
  26. }
  27. func (userStoreUnavailableRepoStub) ExtendProcessingLock(context.Context, int64, string, time.Time, time.Time) (bool, error) {
  28. return false, errors.New("store unavailable")
  29. }
  30. func (userStoreUnavailableRepoStub) MarkSucceeded(context.Context, int64, int, string, time.Time) error {
  31. return errors.New("store unavailable")
  32. }
  33. func (userStoreUnavailableRepoStub) MarkFailedRetryable(context.Context, int64, string, time.Time, time.Time) error {
  34. return errors.New("store unavailable")
  35. }
  36. func (userStoreUnavailableRepoStub) DeleteExpired(context.Context, time.Time, int) (int64, error) {
  37. return 0, errors.New("store unavailable")
  38. }
  39. type userMemoryIdempotencyRepoStub struct {
  40. mu sync.Mutex
  41. nextID int64
  42. data map[string]*service.IdempotencyRecord
  43. }
  44. func newUserMemoryIdempotencyRepoStub() *userMemoryIdempotencyRepoStub {
  45. return &userMemoryIdempotencyRepoStub{
  46. nextID: 1,
  47. data: make(map[string]*service.IdempotencyRecord),
  48. }
  49. }
  50. func (r *userMemoryIdempotencyRepoStub) key(scope, keyHash string) string {
  51. return scope + "|" + keyHash
  52. }
  53. func (r *userMemoryIdempotencyRepoStub) clone(in *service.IdempotencyRecord) *service.IdempotencyRecord {
  54. if in == nil {
  55. return nil
  56. }
  57. out := *in
  58. if in.LockedUntil != nil {
  59. v := *in.LockedUntil
  60. out.LockedUntil = &v
  61. }
  62. if in.ResponseBody != nil {
  63. v := *in.ResponseBody
  64. out.ResponseBody = &v
  65. }
  66. if in.ResponseStatus != nil {
  67. v := *in.ResponseStatus
  68. out.ResponseStatus = &v
  69. }
  70. if in.ErrorReason != nil {
  71. v := *in.ErrorReason
  72. out.ErrorReason = &v
  73. }
  74. return &out
  75. }
  76. func (r *userMemoryIdempotencyRepoStub) CreateProcessing(_ context.Context, record *service.IdempotencyRecord) (bool, error) {
  77. r.mu.Lock()
  78. defer r.mu.Unlock()
  79. k := r.key(record.Scope, record.IdempotencyKeyHash)
  80. if _, ok := r.data[k]; ok {
  81. return false, nil
  82. }
  83. cp := r.clone(record)
  84. cp.ID = r.nextID
  85. r.nextID++
  86. r.data[k] = cp
  87. record.ID = cp.ID
  88. return true, nil
  89. }
  90. func (r *userMemoryIdempotencyRepoStub) GetByScopeAndKeyHash(_ context.Context, scope, keyHash string) (*service.IdempotencyRecord, error) {
  91. r.mu.Lock()
  92. defer r.mu.Unlock()
  93. return r.clone(r.data[r.key(scope, keyHash)]), nil
  94. }
  95. func (r *userMemoryIdempotencyRepoStub) TryReclaim(_ context.Context, id int64, fromStatus string, now, newLockedUntil, newExpiresAt time.Time) (bool, error) {
  96. r.mu.Lock()
  97. defer r.mu.Unlock()
  98. for _, rec := range r.data {
  99. if rec.ID != id {
  100. continue
  101. }
  102. if rec.Status != fromStatus {
  103. return false, nil
  104. }
  105. if rec.LockedUntil != nil && rec.LockedUntil.After(now) {
  106. return false, nil
  107. }
  108. rec.Status = service.IdempotencyStatusProcessing
  109. rec.LockedUntil = &newLockedUntil
  110. rec.ExpiresAt = newExpiresAt
  111. rec.ErrorReason = nil
  112. return true, nil
  113. }
  114. return false, nil
  115. }
  116. func (r *userMemoryIdempotencyRepoStub) ExtendProcessingLock(_ context.Context, id int64, requestFingerprint string, newLockedUntil, newExpiresAt time.Time) (bool, error) {
  117. r.mu.Lock()
  118. defer r.mu.Unlock()
  119. for _, rec := range r.data {
  120. if rec.ID != id {
  121. continue
  122. }
  123. if rec.Status != service.IdempotencyStatusProcessing || rec.RequestFingerprint != requestFingerprint {
  124. return false, nil
  125. }
  126. rec.LockedUntil = &newLockedUntil
  127. rec.ExpiresAt = newExpiresAt
  128. return true, nil
  129. }
  130. return false, nil
  131. }
  132. func (r *userMemoryIdempotencyRepoStub) MarkSucceeded(_ context.Context, id int64, responseStatus int, responseBody string, expiresAt time.Time) error {
  133. r.mu.Lock()
  134. defer r.mu.Unlock()
  135. for _, rec := range r.data {
  136. if rec.ID != id {
  137. continue
  138. }
  139. rec.Status = service.IdempotencyStatusSucceeded
  140. rec.LockedUntil = nil
  141. rec.ExpiresAt = expiresAt
  142. rec.ResponseStatus = &responseStatus
  143. rec.ResponseBody = &responseBody
  144. rec.ErrorReason = nil
  145. return nil
  146. }
  147. return nil
  148. }
  149. func (r *userMemoryIdempotencyRepoStub) MarkFailedRetryable(_ context.Context, id int64, errorReason string, lockedUntil, expiresAt time.Time) error {
  150. r.mu.Lock()
  151. defer r.mu.Unlock()
  152. for _, rec := range r.data {
  153. if rec.ID != id {
  154. continue
  155. }
  156. rec.Status = service.IdempotencyStatusFailedRetryable
  157. rec.LockedUntil = &lockedUntil
  158. rec.ExpiresAt = expiresAt
  159. rec.ErrorReason = &errorReason
  160. return nil
  161. }
  162. return nil
  163. }
  164. func (r *userMemoryIdempotencyRepoStub) DeleteExpired(_ context.Context, _ time.Time, _ int) (int64, error) {
  165. return 0, nil
  166. }
  167. func withUserSubject(userID int64) gin.HandlerFunc {
  168. return func(c *gin.Context) {
  169. c.Set(string(middleware.ContextKeyUser), middleware.AuthSubject{UserID: userID})
  170. c.Next()
  171. }
  172. }
  173. func TestExecuteUserIdempotentJSONFallbackWithoutCoordinator(t *testing.T) {
  174. gin.SetMode(gin.TestMode)
  175. service.SetDefaultIdempotencyCoordinator(nil)
  176. var executed int
  177. router := gin.New()
  178. router.Use(withUserSubject(1))
  179. router.POST("/idempotent", func(c *gin.Context) {
  180. executeUserIdempotentJSON(c, "user.test.scope", map[string]any{"a": 1}, time.Minute, func(ctx context.Context) (any, error) {
  181. executed++
  182. return gin.H{"ok": true}, nil
  183. })
  184. })
  185. req := httptest.NewRequest(http.MethodPost, "/idempotent", bytes.NewBufferString(`{"a":1}`))
  186. req.Header.Set("Content-Type", "application/json")
  187. rec := httptest.NewRecorder()
  188. router.ServeHTTP(rec, req)
  189. require.Equal(t, http.StatusOK, rec.Code)
  190. require.Equal(t, 1, executed)
  191. }
  192. func TestExecuteUserIdempotentJSONFailCloseOnStoreUnavailable(t *testing.T) {
  193. gin.SetMode(gin.TestMode)
  194. service.SetDefaultIdempotencyCoordinator(service.NewIdempotencyCoordinator(userStoreUnavailableRepoStub{}, service.DefaultIdempotencyConfig()))
  195. t.Cleanup(func() {
  196. service.SetDefaultIdempotencyCoordinator(nil)
  197. })
  198. var executed int
  199. router := gin.New()
  200. router.Use(withUserSubject(2))
  201. router.POST("/idempotent", func(c *gin.Context) {
  202. executeUserIdempotentJSON(c, "user.test.scope", map[string]any{"a": 1}, time.Minute, func(ctx context.Context) (any, error) {
  203. executed++
  204. return gin.H{"ok": true}, nil
  205. })
  206. })
  207. req := httptest.NewRequest(http.MethodPost, "/idempotent", bytes.NewBufferString(`{"a":1}`))
  208. req.Header.Set("Content-Type", "application/json")
  209. req.Header.Set("Idempotency-Key", "k1")
  210. rec := httptest.NewRecorder()
  211. router.ServeHTTP(rec, req)
  212. require.Equal(t, http.StatusServiceUnavailable, rec.Code)
  213. require.Equal(t, 0, executed)
  214. }
  215. func TestExecuteUserIdempotentJSONConcurrentRetrySingleSideEffectAndReplay(t *testing.T) {
  216. gin.SetMode(gin.TestMode)
  217. repo := newUserMemoryIdempotencyRepoStub()
  218. cfg := service.DefaultIdempotencyConfig()
  219. cfg.ProcessingTimeout = 2 * time.Second
  220. service.SetDefaultIdempotencyCoordinator(service.NewIdempotencyCoordinator(repo, cfg))
  221. t.Cleanup(func() {
  222. service.SetDefaultIdempotencyCoordinator(nil)
  223. })
  224. var executed atomic.Int32
  225. router := gin.New()
  226. router.Use(withUserSubject(3))
  227. router.POST("/idempotent", func(c *gin.Context) {
  228. executeUserIdempotentJSON(c, "user.test.scope", map[string]any{"a": 1}, time.Minute, func(ctx context.Context) (any, error) {
  229. executed.Add(1)
  230. time.Sleep(80 * time.Millisecond)
  231. return gin.H{"ok": true}, nil
  232. })
  233. })
  234. call := func() (int, http.Header) {
  235. req := httptest.NewRequest(http.MethodPost, "/idempotent", bytes.NewBufferString(`{"a":1}`))
  236. req.Header.Set("Content-Type", "application/json")
  237. req.Header.Set("Idempotency-Key", "same-user-key")
  238. rec := httptest.NewRecorder()
  239. router.ServeHTTP(rec, req)
  240. return rec.Code, rec.Header()
  241. }
  242. var status1, status2 int
  243. var wg sync.WaitGroup
  244. wg.Add(2)
  245. go func() { defer wg.Done(); status1, _ = call() }()
  246. go func() { defer wg.Done(); status2, _ = call() }()
  247. wg.Wait()
  248. require.Contains(t, []int{http.StatusOK, http.StatusConflict}, status1)
  249. require.Contains(t, []int{http.StatusOK, http.StatusConflict}, status2)
  250. require.Equal(t, int32(1), executed.Load())
  251. status3, headers3 := call()
  252. require.Equal(t, http.StatusOK, status3)
  253. require.Equal(t, "true", headers3.Get("X-Idempotency-Replayed"))
  254. require.Equal(t, int32(1), executed.Load())
  255. }