88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/rpc/server"
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/sqlite"
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/testsuite"
|
|
"github.com/pkg/errors"
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
)
|
|
|
|
func TestBlobStore(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Verbose() {
|
|
logger.SetLevel(logger.LevelDebug)
|
|
}
|
|
|
|
httpServer, err := startNewBlobStoreServer()
|
|
if err != nil {
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
defer httpServer.Close()
|
|
|
|
serverAddr := httpServer.Listener.Addr()
|
|
serverURL := &url.URL{
|
|
Host: serverAddr.String(),
|
|
}
|
|
|
|
store := NewBlobStore(serverURL)
|
|
|
|
testsuite.TestBlobStore(context.Background(), t, store)
|
|
}
|
|
|
|
func BenchmarkBlobStore(t *testing.B) {
|
|
logger.SetLevel(logger.LevelError)
|
|
|
|
httpServer, err := startNewBlobStoreServer()
|
|
if err != nil {
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
defer httpServer.Close()
|
|
|
|
serverAddr := httpServer.Listener.Addr()
|
|
serverURL := &url.URL{
|
|
Host: serverAddr.String(),
|
|
}
|
|
|
|
store := NewBlobStore(serverURL)
|
|
|
|
testsuite.BenchmarkBlobStore(t, store)
|
|
|
|
}
|
|
|
|
func getSQLiteBlobStore() (*sqlite.BlobStore, error) {
|
|
file := "./testdata/blobstore_test.sqlite"
|
|
|
|
if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
dsn := fmt.Sprintf("%s?_pragma=foreign_keys(1)&_pragma=busy_timeout=%d", file, (60 * time.Second).Milliseconds())
|
|
store := sqlite.NewBlobStore(dsn)
|
|
|
|
return store, nil
|
|
}
|
|
|
|
func startNewBlobStoreServer() (*httptest.Server, error) {
|
|
store, err := getSQLiteBlobStore()
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
server := server.NewBlobStoreServer(store)
|
|
|
|
httpServer := httptest.NewServer(server)
|
|
|
|
return httpServer, nil
|
|
}
|