mirror of
https://github.com/alexedwards/scs.git
synced 2025-07-17 01:12:21 +02:00
Fix race condition in pgxstore cleanup
- Run this test with the race detector on
This commit is contained in:
@ -28,6 +28,7 @@ func New(pool *pgxpool.Pool) *PostgresStore {
|
|||||||
func NewWithCleanupInterval(pool *pgxpool.Pool, cleanupInterval time.Duration) *PostgresStore {
|
func NewWithCleanupInterval(pool *pgxpool.Pool, cleanupInterval time.Duration) *PostgresStore {
|
||||||
p := &PostgresStore{pool: pool}
|
p := &PostgresStore{pool: pool}
|
||||||
if cleanupInterval > 0 {
|
if cleanupInterval > 0 {
|
||||||
|
p.stopCleanup = make(chan bool)
|
||||||
go p.startCleanup(cleanupInterval)
|
go p.startCleanup(cleanupInterval)
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
@ -99,7 +100,6 @@ func (p *PostgresStore) All() (map[string][]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostgresStore) startCleanup(interval time.Duration) {
|
func (p *PostgresStore) startCleanup(interval time.Duration) {
|
||||||
p.stopCleanup = make(chan bool)
|
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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) {
|
func TestStopNilCleanup(t *testing.T) {
|
||||||
dsn := os.Getenv("SCS_POSTGRES_TEST_DSN")
|
dsn := os.Getenv("SCS_POSTGRES_TEST_DSN")
|
||||||
pool, err := pgxpool.New(context.Background(), dsn)
|
pool, err := pgxpool.New(context.Background(), dsn)
|
||||||
|
Reference in New Issue
Block a user