2023-10-24 22:52:33 +02:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/driver/sqlite"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/testsuite"
|
2023-11-29 11:10:29 +01:00
|
|
|
"github.com/allegro/bigcache/v3"
|
2023-10-24 22:52:33 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlobStore(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
if testing.Verbose() {
|
|
|
|
logger.SetLevel(logger.LevelDebug)
|
|
|
|
}
|
|
|
|
|
|
|
|
file := "./testdata/blobstore_test.sqlite"
|
|
|
|
|
|
|
|
if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
dsn := fmt.Sprintf("%s?_pragma=foreign_keys(1)&_pragma=busy_timeout=%d", file, (60 * time.Second).Milliseconds())
|
|
|
|
|
|
|
|
backend := sqlite.NewBlobStore(dsn)
|
2023-11-29 11:10:29 +01:00
|
|
|
|
|
|
|
cache, err := bigcache.New(context.Background(), bigcache.DefaultConfig(time.Minute))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
store := NewBlobStore(backend, cache)
|
2023-10-24 22:52:33 +02:00
|
|
|
|
|
|
|
testsuite.TestBlobStore(context.Background(), t, store)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBlobStore(t *testing.B) {
|
|
|
|
logger.SetLevel(logger.LevelError)
|
|
|
|
|
|
|
|
file := "./testdata/blobstore_test.sqlite"
|
|
|
|
|
|
|
|
if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
dsn := fmt.Sprintf("%s?_pragma=foreign_keys(1)&_pragma=busy_timeout=%d", file, (60 * time.Second).Milliseconds())
|
|
|
|
backend := sqlite.NewBlobStore(dsn)
|
2023-11-29 11:10:29 +01:00
|
|
|
|
|
|
|
cache, err := bigcache.New(context.Background(), bigcache.DefaultConfig(time.Minute))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
store := NewBlobStore(backend, cache)
|
2023-10-24 22:52:33 +02:00
|
|
|
|
|
|
|
testsuite.BenchmarkBlobStore(t, store)
|
|
|
|
}
|