|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package repository |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/Wei-Shaw/sub2api/internal/service" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestOpsRepositoryBatchInsertErrorLogs(t *testing.T) { |
| 15 | + ctx := context.Background() |
| 16 | + _, _ = integrationDB.ExecContext(ctx, "TRUNCATE ops_error_logs RESTART IDENTITY") |
| 17 | + |
| 18 | + repo := NewOpsRepository(integrationDB).(*opsRepository) |
| 19 | + now := time.Now().UTC() |
| 20 | + inserted, err := repo.BatchInsertErrorLogs(ctx, []*service.OpsInsertErrorLogInput{ |
| 21 | + { |
| 22 | + RequestID: "batch-ops-1", |
| 23 | + ErrorPhase: "upstream", |
| 24 | + ErrorType: "upstream_error", |
| 25 | + Severity: "error", |
| 26 | + StatusCode: 429, |
| 27 | + ErrorMessage: "rate limited", |
| 28 | + CreatedAt: now, |
| 29 | + }, |
| 30 | + { |
| 31 | + RequestID: "batch-ops-2", |
| 32 | + ErrorPhase: "internal", |
| 33 | + ErrorType: "api_error", |
| 34 | + Severity: "error", |
| 35 | + StatusCode: 500, |
| 36 | + ErrorMessage: "internal error", |
| 37 | + CreatedAt: now.Add(time.Millisecond), |
| 38 | + }, |
| 39 | + }) |
| 40 | + require.NoError(t, err) |
| 41 | + require.EqualValues(t, 2, inserted) |
| 42 | + |
| 43 | + var count int |
| 44 | + require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM ops_error_logs WHERE request_id IN ('batch-ops-1', 'batch-ops-2')").Scan(&count)) |
| 45 | + require.Equal(t, 2, count) |
| 46 | +} |
| 47 | + |
| 48 | +func TestEnqueueSchedulerOutbox_DeduplicatesIdempotentEvents(t *testing.T) { |
| 49 | + ctx := context.Background() |
| 50 | + _, _ = integrationDB.ExecContext(ctx, "TRUNCATE scheduler_outbox RESTART IDENTITY") |
| 51 | + |
| 52 | + accountID := int64(12345) |
| 53 | + require.NoError(t, enqueueSchedulerOutbox(ctx, integrationDB, service.SchedulerOutboxEventAccountChanged, &accountID, nil, nil)) |
| 54 | + require.NoError(t, enqueueSchedulerOutbox(ctx, integrationDB, service.SchedulerOutboxEventAccountChanged, &accountID, nil, nil)) |
| 55 | + |
| 56 | + var count int |
| 57 | + require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM scheduler_outbox WHERE event_type = $1", service.SchedulerOutboxEventAccountChanged).Scan(&count)) |
| 58 | + require.Equal(t, 1, count) |
| 59 | + |
| 60 | + time.Sleep(schedulerOutboxDedupWindow + 150*time.Millisecond) |
| 61 | + require.NoError(t, enqueueSchedulerOutbox(ctx, integrationDB, service.SchedulerOutboxEventAccountChanged, &accountID, nil, nil)) |
| 62 | + require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM scheduler_outbox WHERE event_type = $1", service.SchedulerOutboxEventAccountChanged).Scan(&count)) |
| 63 | + require.Equal(t, 2, count) |
| 64 | +} |
| 65 | + |
| 66 | +func TestEnqueueSchedulerOutbox_DoesNotDeduplicateLastUsed(t *testing.T) { |
| 67 | + ctx := context.Background() |
| 68 | + _, _ = integrationDB.ExecContext(ctx, "TRUNCATE scheduler_outbox RESTART IDENTITY") |
| 69 | + |
| 70 | + accountID := int64(67890) |
| 71 | + payload1 := map[string]any{"last_used": map[string]int64{"67890": 100}} |
| 72 | + payload2 := map[string]any{"last_used": map[string]int64{"67890": 200}} |
| 73 | + require.NoError(t, enqueueSchedulerOutbox(ctx, integrationDB, service.SchedulerOutboxEventAccountLastUsed, &accountID, nil, payload1)) |
| 74 | + require.NoError(t, enqueueSchedulerOutbox(ctx, integrationDB, service.SchedulerOutboxEventAccountLastUsed, &accountID, nil, payload2)) |
| 75 | + |
| 76 | + var count int |
| 77 | + require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM scheduler_outbox WHERE event_type = $1", service.SchedulerOutboxEventAccountLastUsed).Scan(&count)) |
| 78 | + require.Equal(t, 2, count) |
| 79 | +} |
0 commit comments