diff --git a/store.go b/store.go index d2bd176..f71416f 100644 --- a/store.go +++ b/store.go @@ -72,6 +72,16 @@ func WithJournal(endpointURL, organisation, bucketName string) StoreOption { } } +// WithPurgeInterval changes how often the background expiry sweep runs. +// Usage example: `storeInstance, err := store.New(":memory:", store.WithPurgeInterval(20*time.Millisecond))` +func WithPurgeInterval(interval time.Duration) StoreOption { + return func(storeInstance *Store) { + if interval > 0 { + storeInstance.purgeInterval = interval + } + } +} + // Usage example: `storeInstance, err := store.New(":memory:")` // Usage example: `storeInstance, err := store.New("/tmp/go-store.db", store.WithJournal("http://127.0.0.1:8086", "core", "events"))` func New(databasePath string, options ...StoreOption) (*Store, error) { diff --git a/store_test.go b/store_test.go index be32e88..6262073 100644 --- a/store_test.go +++ b/store_test.go @@ -1263,14 +1263,8 @@ func TestStore_PurgeExpired_Bad_RowsAffectedError(t *testing.T) { } func TestStore_PurgeExpired_Good_BackgroundPurge(t *testing.T) { - storeInstance, _ := New(":memory:") - // Override purge interval for testing: restart the goroutine with a short interval. - storeInstance.cancelPurge() - storeInstance.purgeWaitGroup.Wait() - storeInstance.purgeInterval = 20 * time.Millisecond - purgeContext, cancel := context.WithCancel(context.Background()) - storeInstance.cancelPurge = cancel - storeInstance.startBackgroundPurge(purgeContext) + storeInstance, err := New(":memory:", WithPurgeInterval(20*time.Millisecond)) + require.NoError(t, err) defer storeInstance.Close() require.NoError(t, storeInstance.SetWithTTL("g", "ephemeral", "v", 1*time.Millisecond)) @@ -1282,7 +1276,7 @@ func TestStore_PurgeExpired_Good_BackgroundPurge(t *testing.T) { // The expired key should have been removed by the background goroutine. // Use a raw query to check the row is actually gone (not just filtered by Get). var count int - err := storeInstance.database.QueryRow("SELECT COUNT(*) FROM entries WHERE group_name = ?", "g").Scan(&count) + err = storeInstance.database.QueryRow("SELECT COUNT(*) FROM entries WHERE group_name = ?", "g").Scan(&count) require.NoError(t, err) assert.Equal(t, 1, count, "background purge should have deleted the expired row") }