Files
grafana/pkg/infra/remotecache/database_storage_test.go
Serge Zaitsev a95fb3a37c Chore: Omit integration tests if short test flag is passed (#108777)
* omit integration tests if short test flag is passed

* Update pkg/services/ngalert/models/receivers_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* Update pkg/tests/api/alerting/api_ruler_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* Update pkg/tests/api/alerting/api_ruler_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* Update pkg/tests/api/alerting/api_ruler_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* Update pkg/tests/api/alerting/api_ruler_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* Update pkg/tests/api/alerting/api_ruler_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* Update pkg/services/ngalert/models/receivers_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* Update pkg/cmd/grafana-cli/commands/datamigrations/to_unified_storage_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* Update pkg/services/ngalert/models/receivers_test.go

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>

* fix the rest

* false positive

---------

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>
2025-07-28 13:38:54 +02:00

84 lines
2.1 KiB
Go

package remotecache
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
)
func TestIntegrationDatabaseStorageGarbageCollection(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
sqlstore := db.InitTestDB(t)
db := &databaseCache{
SQLStore: sqlstore,
log: log.New("remotecache.database"),
}
obj := []byte("foolbar")
// set time.now to 2 weeks ago
var err error
getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
err = db.Set(context.Background(), "key1", obj, 1000*time.Second)
assert.Equal(t, err, nil)
err = db.Set(context.Background(), "key2", obj, 1000*time.Second)
assert.Equal(t, err, nil)
err = db.Set(context.Background(), "key3", obj, 1000*time.Second)
assert.Equal(t, err, nil)
// insert object that should never expire
err = db.Set(context.Background(), "key4", obj, 0)
assert.Equal(t, err, nil)
getTime = time.Now
err = db.Set(context.Background(), "key5", obj, 1000*time.Second)
assert.Equal(t, err, nil)
// run GC
db.internalRunGC()
// try to read values
_, err = db.Get(context.Background(), "key1")
assert.Equal(t, err, ErrCacheItemNotFound, "expected cache item not found. got: ", err)
_, err = db.Get(context.Background(), "key2")
assert.Equal(t, err, ErrCacheItemNotFound)
_, err = db.Get(context.Background(), "key3")
assert.Equal(t, err, ErrCacheItemNotFound)
_, err = db.Get(context.Background(), "key4")
assert.Equal(t, err, nil)
_, err = db.Get(context.Background(), "key5")
assert.Equal(t, err, nil)
}
func TestIntegrationSecondSet(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
var err error
sqlstore := db.InitTestDB(t)
db := &databaseCache{
SQLStore: sqlstore,
log: log.New("remotecache.database"),
}
obj := []byte("hey!")
err = db.Set(context.Background(), "killa-gorilla", obj, 0)
assert.Equal(t, err, nil)
err = db.Set(context.Background(), "killa-gorilla", obj, 0)
assert.Equal(t, err, nil)
}