1
0
mirror of https://github.com/alexedwards/scs.git synced 2025-07-15 01:04:36 +02:00

Fix race condition in pgxstore cleanup

- Run this test with the race detector on
This commit is contained in:
Pete Steyert-Woods
2024-04-09 12:56:40 +01:00
parent 7e11d57e88
commit 5d95719653
2 changed files with 22 additions and 1 deletions

View File

@ -28,6 +28,7 @@ func New(pool *pgxpool.Pool) *PostgresStore {
func NewWithCleanupInterval(pool *pgxpool.Pool, cleanupInterval time.Duration) *PostgresStore {
p := &PostgresStore{pool: pool}
if cleanupInterval > 0 {
p.stopCleanup = make(chan bool)
go p.startCleanup(cleanupInterval)
}
return p
@ -99,7 +100,6 @@ func (p *PostgresStore) All() (map[string][]byte, error) {
}
func (p *PostgresStore) startCleanup(interval time.Duration) {
p.stopCleanup = make(chan bool)
ticker := time.NewTicker(interval)
for {
select {

View File

@ -5,6 +5,7 @@ import (
"context"
"os"
"reflect"
"strconv"
"testing"
"time"
@ -241,6 +242,26 @@ func TestCleanup(t *testing.T) {
}
}
func TestStopCleanup(t *testing.T) {
ctx := context.Background()
dsn := os.Getenv("SCS_POSTGRES_TEST_DSN")
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
for i := 0; i < 100; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
p := New(pool)
time.Sleep(100 * time.Millisecond)
defer p.StopCleanup()
})
}
}
func TestStopNilCleanup(t *testing.T) {
dsn := os.Getenv("SCS_POSTGRES_TEST_DSN")
pool, err := pgxpool.New(context.Background(), dsn)