|
| 1 | +//go:build integration |
| 2 | +// +build integration |
| 3 | + |
| 4 | +package config_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/LeanerCloud/cloud-commitments-platform/internal/config" |
| 12 | + "github.com/LeanerCloud/cloud-commitments-platform/internal/database/postgres/testhelpers" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// requireLastCollectedAt asserts recommendations_state.last_collected_at |
| 18 | +// equals want. |
| 19 | +func requireLastCollectedAt(ctx context.Context, t *testing.T, store *config.PostgresStore, want time.Time) { |
| 20 | + t.Helper() |
| 21 | + fr, err := store.GetRecommendationsFreshness(ctx) |
| 22 | + require.NoError(t, err) |
| 23 | + require.NotNil(t, fr.LastCollectedAt) |
| 24 | + assert.True(t, want.Equal(*fr.LastCollectedAt), |
| 25 | + "last_collected_at = %s, want %s", fr.LastCollectedAt.UTC(), want) |
| 26 | +} |
| 27 | + |
| 28 | +// TestPostgresStore_UpsertRecommendations_LastCollectedAtNeverMovesBackwards |
| 29 | +// pins issue #111: a collection that started earlier but commits later must |
| 30 | +// not move last_collected_at back to its older timestamp. |
| 31 | +func TestPostgresStore_UpsertRecommendations_LastCollectedAtNeverMovesBackwards(t *testing.T) { |
| 32 | + ctx := context.Background() |
| 33 | + store := setupRecommendationsStore(ctx, t) |
| 34 | + |
| 35 | + newer := time.Now().UTC().Truncate(time.Second) |
| 36 | + older := newer.Add(-3 * time.Minute) |
| 37 | + |
| 38 | + require.NoError(t, store.UpsertRecommendations(ctx, newer, nil, nil)) |
| 39 | + requireLastCollectedAt(ctx, t, store, newer) |
| 40 | + |
| 41 | + require.NoError(t, store.UpsertRecommendations(ctx, older, nil, nil)) |
| 42 | + requireLastCollectedAt(ctx, t, store, newer) |
| 43 | + |
| 44 | + // A genuinely newer collection still advances it. |
| 45 | + newest := newer.Add(time.Minute) |
| 46 | + require.NoError(t, store.UpsertRecommendations(ctx, newest, nil, nil)) |
| 47 | + requireLastCollectedAt(ctx, t, store, newest) |
| 48 | +} |
| 49 | + |
| 50 | +// TestPostgresStore_UpsertRecommendations_LastCollectedAtConcurrentCommit |
| 51 | +// covers the race in issue #111 on real Postgres: the newer collection holds |
| 52 | +// the state row's lock and commits while the older one is blocked on it. The |
| 53 | +// blocked UPDATE must re-evaluate GREATEST against the committed newer value |
| 54 | +// rather than overwrite it with the older timestamp. |
| 55 | +func TestPostgresStore_UpsertRecommendations_LastCollectedAtConcurrentCommit(t *testing.T) { |
| 56 | + ctx := context.Background() |
| 57 | + db := testhelpers.RequireMigratedDatabase(ctx, t, getMigrationsPath()).DB |
| 58 | + store := config.NewPostgresStore(db) |
| 59 | + |
| 60 | + base := time.Now().UTC().Truncate(time.Second) |
| 61 | + older := base.Add(-2 * time.Minute) |
| 62 | + newer := base |
| 63 | + |
| 64 | + require.NoError(t, store.UpsertRecommendations(ctx, base.Add(-time.Hour), nil, nil)) |
| 65 | + |
| 66 | + // The newer run's state write, held open so it owns the row lock. |
| 67 | + tx, err := db.Begin(ctx) |
| 68 | + require.NoError(t, err) |
| 69 | + t.Cleanup(func() { _ = tx.Rollback(context.Background()) }) |
| 70 | + _, err = tx.Exec(ctx, `UPDATE recommendations_state SET last_collected_at = GREATEST(last_collected_at, $1) WHERE id = 1`, newer) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + // The older run commits after it: its UPDATE blocks on the row lock. |
| 74 | + done := make(chan error, 1) |
| 75 | + go func() { done <- store.UpsertRecommendations(ctx, older, nil, nil) }() |
| 76 | + |
| 77 | + require.Eventually(t, func() bool { |
| 78 | + var waiting int |
| 79 | + if qerr := db.QueryRow(ctx, `SELECT count(*) FROM pg_locks WHERE NOT granted`).Scan(&waiting); qerr != nil { |
| 80 | + return false |
| 81 | + } |
| 82 | + return waiting > 0 |
| 83 | + }, 10*time.Second, 20*time.Millisecond, "older run's UPDATE never blocked on the state row lock") |
| 84 | + |
| 85 | + require.NoError(t, tx.Commit(ctx)) |
| 86 | + |
| 87 | + select { |
| 88 | + case err := <-done: |
| 89 | + require.NoError(t, err) |
| 90 | + case <-time.After(10 * time.Second): |
| 91 | + t.Fatal("older run's UpsertRecommendations did not finish after the newer run committed") |
| 92 | + } |
| 93 | + |
| 94 | + requireLastCollectedAt(ctx, t, store, newer) |
| 95 | +} |
0 commit comments