Merge pull request #1672 from mdelapenya/testing-patterns

chore(testing): use require in tests
This commit is contained in:
RW
2025-04-16 07:40:42 +02:00
committed by GitHub
16 changed files with 370 additions and 501 deletions

View File

@@ -2,7 +2,6 @@ package aerospike
import ( import (
"context" "context"
"fmt"
"os" "os"
"testing" "testing"
"time" "time"
@@ -26,7 +25,9 @@ const (
) )
// startAerospikeContainer starts an Aerospike container for testing // startAerospikeContainer starts an Aerospike container for testing
func startAerospikeContainer(ctx context.Context) (testcontainers.Container, error) { func startAerospikeContainer(t testing.TB, ctx context.Context) testcontainers.Container {
t.Helper()
// Get custom image from env if specified // Get custom image from env if specified
image := aerospikeImage image := aerospikeImage
if envImage := os.Getenv(aerospikeImageEnvVar); envImage != "" { if envImage := os.Getenv(aerospikeImageEnvVar); envImage != "" {
@@ -50,37 +51,28 @@ func startAerospikeContainer(ctx context.Context) (testcontainers.Container, err
} }
// Start container // Start container
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req, ContainerRequest: req,
Started: true, Started: true,
}) })
if err != nil { testcontainers.CleanupContainer(t, ctr)
return nil, fmt.Errorf("failed to start aerospike container: %w", err) require.NoError(t, err)
}
return container, nil return ctr
} }
// newTestStore creates a client connected to the test container // newTestStore creates a client connected to the test container
func newTestStore(t testing.TB) *Storage { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
c, err := startAerospikeContainer(context.Background()) c := startAerospikeContainer(t, context.Background())
testcontainers.CleanupContainer(t, c)
if err != nil {
t.Fatalf("Failed to start Aerospike container: %v", err)
}
// Extract host and port // Extract host and port
host, err := c.Host(context.TODO()) host, err := c.Host(context.TODO())
if err != nil { require.NoError(t, err)
t.Fatalf("Failed to get container host: %v", err)
}
port, err := c.MappedPort(context.TODO(), aerospikePort) port, err := c.MappedPort(context.TODO(), aerospikePort)
if err != nil { require.NoError(t, err)
t.Fatalf("Failed to get container port: %v", err)
}
return New(Config{ return New(Config{
Hosts: []*aerospike.Host{aerospike.NewHost(host, port.Int())}, Hosts: []*aerospike.Host{aerospike.NewHost(host, port.Int())},

View File

@@ -17,7 +17,7 @@ const (
azuriteImageEnvVar = "TEST_AZURITE_IMAGE" azuriteImageEnvVar = "TEST_AZURITE_IMAGE"
) )
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
img := azuriteImage img := azuriteImage
@@ -29,14 +29,10 @@ func newTestStore(t testing.TB) (*Storage, error) {
c, err := azurite.Run(ctx, img) c, err := azurite.Run(ctx, img)
testcontainers.CleanupContainer(t, c) testcontainers.CleanupContainer(t, c)
if err != nil { require.NoError(t, err)
return nil, err
}
serviceURL, err := c.BlobServiceURL(ctx) serviceURL, err := c.BlobServiceURL(ctx)
if err != nil { require.NoError(t, err)
return nil, err
}
return New(Config{ return New(Config{
Account: azurite.AccountName, Account: azurite.AccountName,
@@ -47,7 +43,7 @@ func newTestStore(t testing.TB) (*Storage, error) {
Key: azurite.AccountKey, Key: azurite.AccountKey,
}, },
Reset: true, Reset: true,
}), nil })
} }
func Test_AzureBlob_Get(t *testing.T) { func Test_AzureBlob_Get(t *testing.T) {
@@ -56,11 +52,10 @@ func Test_AzureBlob_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -74,11 +69,10 @@ func Test_AzureBlob_Set(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -88,11 +82,10 @@ func Test_AzureBlob_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Delete(key) err = testStore.Delete(key)
@@ -114,11 +107,10 @@ func Test_AzureBlob_Override(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set(key, val, 0) err = testStore.Set(key, val, 0)
@@ -126,8 +118,7 @@ func Test_AzureBlob_Override(t *testing.T) {
} }
func Test_AzureBlob_Get_NotExist(t *testing.T) { func Test_AzureBlob_Get_NotExist(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
@@ -143,11 +134,10 @@ func Test_AzureBlob_Get_NotExist(t *testing.T) {
func Test_AzureBlob_Reset(t *testing.T) { func Test_AzureBlob_Reset(t *testing.T) {
val := []byte("doe") val := []byte("doe")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set("john2", val, 0) err = testStore.Set("john2", val, 0)
@@ -176,27 +166,25 @@ func Test_AzureBlob_Reset(t *testing.T) {
} }
func Test_AzureBlob_Conn(t *testing.T) { func Test_AzureBlob_Conn(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
require.True(t, testStore.Conn() != nil) require.True(t, testStore.Conn() != nil)
} }
func Test_AzureBlob_Close(t *testing.T) { func Test_AzureBlob_Close(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err) require.NoError(t, testStore.Close())
require.Nil(t, testStore.Close())
} }
func Benchmark_AzureBlob_Set(b *testing.B) { func Benchmark_AzureBlob_Set(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -205,11 +193,10 @@ func Benchmark_AzureBlob_Set(b *testing.B) {
} }
func Benchmark_AzureBlob_Get(b *testing.B) { func Benchmark_AzureBlob_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -223,13 +210,13 @@ func Benchmark_AzureBlob_Get(b *testing.B) {
} }
func Benchmark_AzureBlob_SetAndDelete(b *testing.B) { func Benchmark_AzureBlob_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")

View File

@@ -27,7 +27,7 @@ const (
clickhouseSuccessCode = 200 clickhouseSuccessCode = 200
) )
func getTestConnection(t testing.TB, cfg Config) (*Storage, error) { func newTestStore(t testing.TB, cfg Config) *Storage {
t.Helper() t.Helper()
img := clickhouseImage img := clickhouseImage
@@ -52,20 +52,14 @@ func getTestConnection(t testing.TB, cfg Config) (*Storage, error) {
), ),
) )
testcontainers.CleanupContainer(t, c) testcontainers.CleanupContainer(t, c)
if err != nil { require.NoError(t, err)
return nil, err
}
hostPort, err := c.ConnectionHost(ctx) hostPort, err := c.ConnectionHost(ctx)
if err != nil { require.NoError(t, err)
return nil, err
}
pair := strings.Split(hostPort, ":") pair := strings.Split(hostPort, ":")
port, err := strconv.Atoi(pair[1]) port, err := strconv.Atoi(pair[1])
if err != nil { require.NoError(t, err)
return nil, err
}
// configure the client for the testcontainers clickhouse instance // configure the client for the testcontainers clickhouse instance
cfg.Host = pair[0] cfg.Host = pair[0]
@@ -75,57 +69,53 @@ func getTestConnection(t testing.TB, cfg Config) (*Storage, error) {
cfg.Database = clickhouseDB cfg.Database = clickhouseDB
client, err := New(cfg) client, err := New(cfg)
require.NoError(t, err)
return client, err return client
} }
func Test_Connection(t *testing.T) { func Test_Connection(t *testing.T) {
client, err := getTestConnection(t, Config{ client := newTestStore(t, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(t, err)
defer client.Close() defer client.Close()
} }
func Test_Set(t *testing.T) { func Test_Set(t *testing.T) {
client, err := getTestConnection(t, Config{ client := newTestStore(t, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(t, err)
defer client.Close() defer client.Close()
err = client.Set("somekey", []byte("somevalue"), 0) err := client.Set("somekey", []byte("somevalue"), 0)
require.NoError(t, err) require.NoError(t, err)
} }
func Test_Set_With_Exp(t *testing.T) { func Test_Set_With_Exp(t *testing.T) {
client, err := getTestConnection(t, Config{ client := newTestStore(t, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(t, err)
defer client.Close() defer client.Close()
err = client.Set("setsomekeywithexp", []byte("somevalue"), time.Second*1) err := client.Set("setsomekeywithexp", []byte("somevalue"), time.Second*1)
require.NoError(t, err) require.NoError(t, err)
} }
func Test_Get(t *testing.T) { func Test_Get(t *testing.T) {
client, err := getTestConnection(t, Config{ client := newTestStore(t, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(t, err)
defer client.Close() defer client.Close()
err = client.Set("somekey", []byte("somevalue"), 0) err := client.Set("somekey", []byte("somevalue"), 0)
require.NoError(t, err) require.NoError(t, err)
value, err := client.Get("somekey") value, err := client.Get("somekey")
@@ -136,15 +126,14 @@ func Test_Get(t *testing.T) {
} }
func Test_Get_With_Exp(t *testing.T) { func Test_Get_With_Exp(t *testing.T) {
client, err := getTestConnection(t, Config{ client := newTestStore(t, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(t, err)
defer client.Close() defer client.Close()
err = client.Set("getsomekeywithexp", []byte("somevalue"), time.Second*2) err := client.Set("getsomekeywithexp", []byte("somevalue"), time.Second*2)
require.NoError(t, err) require.NoError(t, err)
value, err := client.Get("getsomekeywithexp") value, err := client.Get("getsomekeywithexp")
@@ -162,15 +151,14 @@ func Test_Get_With_Exp(t *testing.T) {
} }
func Test_Delete(t *testing.T) { func Test_Delete(t *testing.T) {
client, err := getTestConnection(t, Config{ client := newTestStore(t, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(t, err)
defer client.Close() defer client.Close()
err = client.Set("somekeytodelete", []byte("somevalue"), time.Second*5) err := client.Set("somekeytodelete", []byte("somevalue"), time.Second*5)
require.NoError(t, err) require.NoError(t, err)
err = client.Delete("somekeytodelete") err = client.Delete("somekeytodelete")
@@ -184,15 +172,14 @@ func Test_Delete(t *testing.T) {
} }
func Test_Reset(t *testing.T) { func Test_Reset(t *testing.T) {
client, err := getTestConnection(t, Config{ client := newTestStore(t, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(t, err)
defer client.Close() defer client.Close()
err = client.Set("testkey", []byte("somevalue"), 0) err := client.Set("testkey", []byte("somevalue"), 0)
require.NoError(t, err) require.NoError(t, err)
err = client.Reset() err = client.Reset()
@@ -206,29 +193,26 @@ func Test_Reset(t *testing.T) {
} }
func TestClose_ShouldReturn_NoError(t *testing.T) { func TestClose_ShouldReturn_NoError(t *testing.T) {
client, err := getTestConnection(t, Config{ client := newTestStore(t, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(t, err)
require.NoError(t, client.Close()) require.NoError(t, client.Close())
} }
func Benchmark_Clickhouse_Set(b *testing.B) { func Benchmark_Clickhouse_Set(b *testing.B) {
b.ReportAllocs() client := newTestStore(b, Config{
b.ResetTimer()
client, err := getTestConnection(b, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(b, err)
defer client.Close() defer client.Close()
b.ReportAllocs()
b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = client.Set("john", []byte("doe"), 0) err = client.Set("john", []byte("doe"), 0)
} }
@@ -237,19 +221,18 @@ func Benchmark_Clickhouse_Set(b *testing.B) {
} }
func Benchmark_Clickhouse_Get(b *testing.B) { func Benchmark_Clickhouse_Get(b *testing.B) {
b.ReportAllocs() client := newTestStore(b, Config{
b.ResetTimer()
client, err := getTestConnection(b, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(b, err)
defer client.Close() defer client.Close()
err = client.Set("john", []byte("doe"), 0) err := client.Set("john", []byte("doe"), 0)
require.NoError(b, err)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err = client.Get("john") _, err = client.Get("john")
@@ -259,18 +242,17 @@ func Benchmark_Clickhouse_Get(b *testing.B) {
} }
func Benchmark_Clickhouse_Set_And_Delete(b *testing.B) { func Benchmark_Clickhouse_Set_And_Delete(b *testing.B) {
b.ReportAllocs() client := newTestStore(b, Config{
b.ResetTimer()
client, err := getTestConnection(b, Config{
Engine: Memory, Engine: Memory,
Table: "test_table", Table: "test_table",
Clean: true, Clean: true,
}) })
require.NoError(b, err)
defer client.Close() defer client.Close()
b.ReportAllocs()
b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = client.Set("john", []byte("doe"), 0) _ = client.Set("john", []byte("doe"), 0)
err = client.Delete("john") err = client.Delete("john")

View File

@@ -21,7 +21,7 @@ const (
couchbaseBucket string = "fiber_storage" couchbaseBucket string = "fiber_storage"
) )
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
img := couchbaseImage img := couchbaseImage
@@ -40,36 +40,29 @@ func newTestStore(t testing.TB) (*Storage, error) {
testcontainers.WithWaitStrategy(wait.ForListeningPort("8091/tcp")), testcontainers.WithWaitStrategy(wait.ForListeningPort("8091/tcp")),
) )
testcontainers.CleanupContainer(t, c) testcontainers.CleanupContainer(t, c)
if err != nil { require.NoError(t, err)
return nil, err
}
conn, err := c.ConnectionString(ctx) conn, err := c.ConnectionString(ctx)
if err != nil { require.NoError(t, err)
return nil, err
}
return New(Config{ return New(Config{
Username: couchbaseUser, Username: couchbaseUser,
Password: couchbasePass, Password: couchbasePass,
Host: conn, Host: conn,
Bucket: couchbaseBucket, Bucket: couchbaseBucket,
}), nil })
} }
func TestSetCouchbase_ShouldReturnNoError(t *testing.T) { func TestSetCouchbase_ShouldReturnNoError(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("test", []byte("test"), 0) err := testStore.Set("test", []byte("test"), 0)
require.NoError(t, err) require.NoError(t, err)
} }
func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) { func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
val, err := testStore.Get("not_found_key") val, err := testStore.Get("not_found_key")
@@ -79,11 +72,10 @@ func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) {
} }
func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) { func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("test", []byte("fiber_test_value"), 0) err := testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err) require.NoError(t, err)
val, err := testStore.Get("test") val, err := testStore.Get("test")
@@ -93,11 +85,10 @@ func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) {
} }
func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) { func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("test", []byte("fiber_test_value"), 3*time.Second) err := testStore.Set("test", []byte("fiber_test_value"), 3*time.Second)
require.NoError(t, err) require.NoError(t, err)
time.Sleep(6 * time.Second) time.Sleep(6 * time.Second)
@@ -109,11 +100,10 @@ func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
} }
func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) { func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("test", []byte("fiber_test_value"), 0) err := testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Delete("test") err = testStore.Delete("test")
@@ -124,11 +114,10 @@ func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
} }
func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) { func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("test", []byte("fiber_test_value"), 0) err := testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Reset() err = testStore.Reset()
@@ -139,28 +128,25 @@ func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
} }
func TestClose_CloseShouldReturn_NoError(t *testing.T) { func TestClose_CloseShouldReturn_NoError(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
require.NoError(t, testStore.Close()) require.NoError(t, testStore.Close())
} }
func TestGetConn_ReturnsNotNil(t *testing.T) { func TestGetConn_ReturnsNotNil(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
require.True(t, testStore.Conn() != nil) require.True(t, testStore.Conn() != nil)
} }
func Benchmark_Couchbase_Set(b *testing.B) { func Benchmark_Couchbase_Set(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -169,11 +155,10 @@ func Benchmark_Couchbase_Set(b *testing.B) {
} }
func Benchmark_Couchbase_Get(b *testing.B) { func Benchmark_Couchbase_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -187,13 +172,13 @@ func Benchmark_Couchbase_Get(b *testing.B) {
} }
func Benchmark_Couchbase_SetAndDelete(b *testing.B) { func Benchmark_Couchbase_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")

View File

@@ -16,7 +16,7 @@ const (
dynamoDBImageEnvVar string = "TEST_DYNAMODB_IMAGE" dynamoDBImageEnvVar string = "TEST_DYNAMODB_IMAGE"
) )
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
img := dynamoDBImage img := dynamoDBImage
@@ -28,14 +28,10 @@ func newTestStore(t testing.TB) (*Storage, error) {
c, err := dynamodb.Run(ctx, img, dynamodb.WithDisableTelemetry()) c, err := dynamodb.Run(ctx, img, dynamodb.WithDisableTelemetry())
testcontainers.CleanupContainer(t, c) testcontainers.CleanupContainer(t, c)
if err != nil { require.NoError(t, err)
return nil, err
}
hostPort, err := c.ConnectionString(ctx) hostPort, err := c.ConnectionString(ctx)
if err != nil { require.NoError(t, err)
return nil, err
}
return New( return New(
Config{ Config{
@@ -48,7 +44,7 @@ func newTestStore(t testing.TB) (*Storage, error) {
}, },
Reset: true, Reset: true,
}, },
), nil )
} }
func Test_DynamoDB_Set(t *testing.T) { func Test_DynamoDB_Set(t *testing.T) {
@@ -57,11 +53,10 @@ func Test_DynamoDB_Set(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -71,11 +66,10 @@ func Test_DynamoDB_Set_Override(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set(key, val, 0) err = testStore.Set(key, val, 0)
@@ -88,11 +82,10 @@ func Test_DynamoDB_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -101,8 +94,7 @@ func Test_DynamoDB_Get(t *testing.T) {
} }
func Test_DynamoDB_Get_NotExist(t *testing.T) { func Test_DynamoDB_Get_NotExist(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
@@ -116,11 +108,10 @@ func Test_DynamoDB_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Delete(key) err = testStore.Delete(key)
@@ -134,11 +125,10 @@ func Test_DynamoDB_Delete(t *testing.T) {
func Test_DynamoDB_Reset(t *testing.T) { func Test_DynamoDB_Reset(t *testing.T) {
val := []byte("doe") val := []byte("doe")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set("john2", val, 0) err = testStore.Set("john2", val, 0)
@@ -157,28 +147,25 @@ func Test_DynamoDB_Reset(t *testing.T) {
} }
func Test_DynamoDB_Close(t *testing.T) { func Test_DynamoDB_Close(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
require.Nil(t, testStore.Close()) require.Nil(t, testStore.Close())
} }
func Test_DynamoDB_Conn(t *testing.T) { func Test_DynamoDB_Conn(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
require.True(t, testStore.Conn() != nil) require.True(t, testStore.Conn() != nil)
} }
func Benchmark_DynamoDB_Set(b *testing.B) { func Benchmark_DynamoDB_Set(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
testStore, err := newTestStore(b) var err error
require.NoError(b, err)
defer testStore.Close()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -187,11 +174,10 @@ func Benchmark_DynamoDB_Set(b *testing.B) {
} }
func Benchmark_DynamoDB_Get(b *testing.B) { func Benchmark_DynamoDB_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -205,13 +191,13 @@ func Benchmark_DynamoDB_Get(b *testing.B) {
} }
func Benchmark_DynamoDB_SetAndDelete(b *testing.B) { func Benchmark_DynamoDB_SetAndDelete(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
testStore, err := newTestStore(b) var err error
require.NoError(b, err)
defer testStore.Close()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")

View File

@@ -146,11 +146,12 @@ func (s *Storage) Reset() error {
GovernanceBypass: true, GovernanceBypass: true,
} }
var errs []error
for err := range s.minio.RemoveObjects(s.ctx, s.cfg.Bucket, objectsCh, opts) { for err := range s.minio.RemoveObjects(s.ctx, s.cfg.Bucket, objectsCh, opts) {
log.Println("Error detected during deletion: ", err) errs = append(errs, err.Err)
} }
return nil return errors.Join(errs...)
} }
// Close the storage // Close the storage

View File

@@ -23,7 +23,7 @@ const (
minioHealthPath = "/minio/health/live" minioHealthPath = "/minio/health/live"
) )
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
img := minioImage img := minioImage
@@ -43,14 +43,10 @@ func newTestStore(t testing.TB) (*Storage, error) {
), ),
) )
testcontainers.CleanupContainer(t, c) testcontainers.CleanupContainer(t, c)
if err != nil { require.NoError(t, err)
return nil, err
}
conn, err := c.ConnectionString(ctx) conn, err := c.ConnectionString(ctx)
if err != nil { require.NoError(t, err)
return nil, err
}
return New( return New(
Config{ Config{
@@ -60,9 +56,8 @@ func newTestStore(t testing.TB) (*Storage, error) {
AccessKeyID: c.Username, AccessKeyID: c.Username,
SecretAccessKey: c.Password, SecretAccessKey: c.Password,
}, },
Reset: true,
}, },
), nil )
} }
func Test_Get(t *testing.T) { func Test_Get(t *testing.T) {
@@ -71,11 +66,10 @@ func Test_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -92,11 +86,10 @@ func Test_Get_Empty_Key(t *testing.T) {
key = "" key = ""
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
_, err = testStore.Get(key) _, err := testStore.Get(key)
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, "the key value is required") require.EqualError(t, err, "the key value is required")
} }
@@ -106,11 +99,10 @@ func Test_Get_Not_Exists_Key(t *testing.T) {
key = "not-exists" key = "not-exists"
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
_, err = testStore.Get(key) _, err := testStore.Get(key)
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, "The specified key does not exist.") require.EqualError(t, err, "The specified key does not exist.")
} }
@@ -120,8 +112,7 @@ func Test_Get_Not_Exists_Bucket(t *testing.T) {
key = "john" key = "john"
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
// random bucket name // random bucket name
@@ -139,11 +130,10 @@ func Test_Set(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -153,12 +143,10 @@ func Test_Set_Empty_Key(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, "the key value is required") require.EqualError(t, err, "the key value is required")
@@ -170,14 +158,13 @@ func Test_Set_Not_Exists_Bucket(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
// random bucket name // random bucket name
testStore.cfg.Bucket = strconv.FormatInt(time.Now().UnixMicro(), 10) testStore.cfg.Bucket = strconv.FormatInt(time.Now().UnixMicro(), 10)
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, "The specified bucket does not exist") require.EqualError(t, err, "The specified bucket does not exist")
} }
@@ -188,11 +175,10 @@ func Test_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Delete(key) err = testStore.Delete(key)
@@ -205,11 +191,10 @@ func Test_Delete_Empty_Key(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, "the key value is required") require.EqualError(t, err, "the key value is required")
} }
@@ -219,15 +204,13 @@ func Test_Delete_Not_Exists_Bucket(t *testing.T) {
key = "john" key = "john"
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
// random bucket name // random bucket name
testStore.cfg.Bucket = strconv.FormatInt(time.Now().UnixMicro(), 10) testStore.cfg.Bucket = strconv.FormatInt(time.Now().UnixMicro(), 10)
err = testStore.Delete(key) err := testStore.Delete(key)
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, "The specified bucket does not exist") require.EqualError(t, err, "The specified bucket does not exist")
} }
@@ -237,11 +220,10 @@ func Test_Reset(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set("john2", val, 0) err = testStore.Set("john2", val, 0)
@@ -255,21 +237,31 @@ func Test_Reset(t *testing.T) {
require.Zero(t, len(result)) require.Zero(t, len(result))
} }
func Test_Close(t *testing.T) { func Test_Reset_Not_Exists_Bucket(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
defer testStore.Close()
err := testStore.RemoveBucket()
require.NoError(t, err) require.NoError(t, err)
err = testStore.Reset()
require.Error(t, err)
require.EqualError(t, err, "The specified bucket does not exist")
}
func Test_Close(t *testing.T) {
testStore := newTestStore(t)
require.NoError(t, testStore.Close()) require.NoError(t, testStore.Close())
} }
func Benchmark_Minio_Set(b *testing.B) { func Benchmark_Minio_Set(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
testStore, err := newTestStore(b) var err error
require.NoError(b, err)
defer testStore.Close()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -278,11 +270,10 @@ func Benchmark_Minio_Set(b *testing.B) {
} }
func Benchmark_Minio_Get(b *testing.B) { func Benchmark_Minio_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -296,13 +287,13 @@ func Benchmark_Minio_Get(b *testing.B) {
} }
func Benchmark_Minio_SetAndDelete(b *testing.B) { func Benchmark_Minio_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")

View File

@@ -22,7 +22,7 @@ const (
mongoDBReadyLog = "Waiting for connections" mongoDBReadyLog = "Waiting for connections"
) )
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
img := mongoDBImage img := mongoDBImage
@@ -42,19 +42,15 @@ func newTestStore(t testing.TB) (*Storage, error) {
), ),
) )
testcontainers.CleanupContainer(t, c) testcontainers.CleanupContainer(t, c)
if err != nil { require.NoError(t, err)
return nil, err
}
conn, err := c.ConnectionString(ctx) conn, err := c.ConnectionString(ctx)
if err != nil { require.NoError(t, err)
return nil, err
}
return New(Config{ return New(Config{
ConnectionURI: conn, ConnectionURI: conn,
Reset: true, Reset: true,
}), nil })
} }
func Test_MongoDB_Set(t *testing.T) { func Test_MongoDB_Set(t *testing.T) {
@@ -63,11 +59,10 @@ func Test_MongoDB_Set(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -77,11 +72,10 @@ func Test_MongoDB_Set_Override(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set(key, val, 0) err = testStore.Set(key, val, 0)
@@ -94,11 +88,10 @@ func Test_MongoDB_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -113,11 +106,10 @@ func Test_MongoDB_Set_Expiration(t *testing.T) {
exp = 1 * time.Second exp = 1 * time.Second
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, exp) err := testStore.Set(key, val, exp)
require.NoError(t, err) require.NoError(t, err)
time.Sleep(1100 * time.Millisecond) time.Sleep(1100 * time.Millisecond)
@@ -130,8 +122,7 @@ func Test_MongoDB_Set_Expiration(t *testing.T) {
func Test_MongoDB_Get_Expired(t *testing.T) { func Test_MongoDB_Get_Expired(t *testing.T) {
key := "john" key := "john"
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -140,8 +131,7 @@ func Test_MongoDB_Get_Expired(t *testing.T) {
} }
func Test_MongoDB_Get_NotExist(t *testing.T) { func Test_MongoDB_Get_NotExist(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
@@ -155,11 +145,10 @@ func Test_MongoDB_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Delete(key) err = testStore.Delete(key)
@@ -173,11 +162,10 @@ func Test_MongoDB_Delete(t *testing.T) {
func Test_MongoDB_Reset(t *testing.T) { func Test_MongoDB_Reset(t *testing.T) {
val := []byte("doe") val := []byte("doe")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set("john2", val, 0) err = testStore.Set("john2", val, 0)
@@ -196,28 +184,25 @@ func Test_MongoDB_Reset(t *testing.T) {
} }
func Test_MongoDB_Close(t *testing.T) { func Test_MongoDB_Close(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err) require.NoError(t, testStore.Close())
require.Nil(t, testStore.Close())
} }
func Test_MongoDB_Conn(t *testing.T) { func Test_MongoDB_Conn(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
require.True(t, testStore.Conn() != nil) require.True(t, testStore.Conn() != nil)
} }
func Benchmark_MongoDB_Set(b *testing.B) { func Benchmark_MongoDB_Set(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -226,11 +211,10 @@ func Benchmark_MongoDB_Set(b *testing.B) {
} }
func Benchmark_MongoDB_Get(b *testing.B) { func Benchmark_MongoDB_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -244,13 +228,13 @@ func Benchmark_MongoDB_Get(b *testing.B) {
} }
func Benchmark_MongoDB_SetAndDelete(b *testing.B) { func Benchmark_MongoDB_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")

View File

@@ -22,7 +22,7 @@ const (
mysqlDatabase string = "fiber" mysqlDatabase string = "fiber"
) )
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
ctx := context.Background() ctx := context.Background()
@@ -30,14 +30,12 @@ func newTestStore(t testing.TB) (*Storage, error) {
c := mustStartMySQL(t) c := mustStartMySQL(t)
conn, err := c.ConnectionString(ctx) conn, err := c.ConnectionString(ctx)
if err != nil { require.NoError(t, err)
return nil, err
}
return New(Config{ return New(Config{
ConnectionURI: conn, ConnectionURI: conn,
Reset: true, Reset: true,
}), nil })
} }
func mustStartMySQL(t testing.TB) *mysql.MySQLContainer { func mustStartMySQL(t testing.TB) *mysql.MySQLContainer {
@@ -64,12 +62,6 @@ func mustStartMySQL(t testing.TB) *mysql.MySQLContainer {
} }
func Test_MYSQL_New(t *testing.T) { func Test_MYSQL_New(t *testing.T) {
testStore, err := newTestStore(t)
require.NoError(t, err)
require.True(t, testStore.db != nil)
require.NoError(t, testStore.Close())
c := mustStartMySQL(t) c := mustStartMySQL(t)
dsn, err := c.ConnectionString(context.Background()) dsn, err := c.ConnectionString(context.Background())
@@ -91,11 +83,10 @@ func Test_MYSQL_Set(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -105,11 +96,10 @@ func Test_MYSQL_Set_Override(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set(key, val, 0) err = testStore.Set(key, val, 0)
@@ -122,11 +112,10 @@ func Test_MYSQL_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -141,11 +130,10 @@ func Test_MYSQL_Set_Expiration(t *testing.T) {
exp = 1 * time.Second exp = 1 * time.Second
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, exp) err := testStore.Set(key, val, exp)
require.NoError(t, err) require.NoError(t, err)
time.Sleep(1100 * time.Millisecond) time.Sleep(1100 * time.Millisecond)
@@ -158,8 +146,7 @@ func Test_MYSQL_Set_Expiration(t *testing.T) {
func Test_MYSQL_Get_Expired(t *testing.T) { func Test_MYSQL_Get_Expired(t *testing.T) {
key := "john" key := "john"
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -168,8 +155,7 @@ func Test_MYSQL_Get_Expired(t *testing.T) {
} }
func Test_MYSQL_Get_NotExist(t *testing.T) { func Test_MYSQL_Get_NotExist(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
@@ -183,11 +169,10 @@ func Test_MYSQL_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Delete(key) err = testStore.Delete(key)
@@ -201,11 +186,10 @@ func Test_MYSQL_Delete(t *testing.T) {
func Test_MYSQL_Reset(t *testing.T) { func Test_MYSQL_Reset(t *testing.T) {
val := []byte("doe") val := []byte("doe")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set("john2", val, 0) err = testStore.Set("john2", val, 0)
@@ -227,11 +211,10 @@ func Test_MYSQL_GC(t *testing.T) {
testVal := []byte("doe") testVal := []byte("doe")
// This key should expire // This key should expire
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", testVal, time.Nanosecond) err := testStore.Set("john", testVal, time.Nanosecond)
require.NoError(t, err) require.NoError(t, err)
testStore.gc(time.Now()) testStore.gc(time.Now())
@@ -252,11 +235,10 @@ func Test_MYSQL_GC(t *testing.T) {
func Test_MYSQL_Non_UTF8(t *testing.T) { func Test_MYSQL_Non_UTF8(t *testing.T) {
val := []byte("0xF5") val := []byte("0xF5")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("0xF6", val, 0) err := testStore.Set("0xF6", val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get("0xF6") result, err := testStore.Get("0xF6")
@@ -265,28 +247,25 @@ func Test_MYSQL_Non_UTF8(t *testing.T) {
} }
func Test_MYSQL_Close(t *testing.T) { func Test_MYSQL_Close(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err) require.NoError(t, testStore.Close())
require.Nil(t, testStore.Close())
} }
func Test_MYSQL_Conn(t *testing.T) { func Test_MYSQL_Conn(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
require.True(t, testStore.Conn() != nil) require.True(t, testStore.Conn() != nil)
} }
func Benchmark_MYSQL_Set(b *testing.B) { func Benchmark_MYSQL_Set(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -295,11 +274,10 @@ func Benchmark_MYSQL_Set(b *testing.B) {
} }
func Benchmark_MYSQL_Get(b *testing.B) { func Benchmark_MYSQL_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -313,13 +291,13 @@ func Benchmark_MYSQL_Get(b *testing.B) {
} }
func Benchmark_MYSQL_SetAndDelete(b *testing.B) { func Benchmark_MYSQL_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")

View File

@@ -75,7 +75,7 @@ func createTLSCerts(t testing.TB) (*tlscert.Certificate, *tlscert.Certificate, *
return caCert, clientCert, natsCert return caCert, clientCert, natsCert
} }
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
ca, client, natsCert := createTLSCerts(t) ca, client, natsCert := createTLSCerts(t)
@@ -141,7 +141,7 @@ func newTestStore(t testing.TB) (*Storage, error) {
Bucket: "test", Bucket: "test",
Storage: jetstream.MemoryStorage, Storage: jetstream.MemoryStorage,
}, },
}), nil })
} }
func Test_Storage_Nats_Set(t *testing.T) { func Test_Storage_Nats_Set(t *testing.T) {
@@ -150,11 +150,10 @@ func Test_Storage_Nats_Set(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
keys, err := testStore.Keys() keys, err := testStore.Keys()
@@ -169,11 +168,10 @@ func Test_Storage_Nats_Set_Overwrite(t *testing.T) {
val2 = []byte("overwritten") val2 = []byte("overwritten")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val1, 0) err := testStore.Set(key, val1, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set(key, val2, 30*time.Second) err = testStore.Set(key, val2, 30*time.Second)
@@ -193,11 +191,10 @@ func Test_Storage_Nats_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 30*time.Second) err := testStore.Set(key, val, 30*time.Second)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -216,11 +213,10 @@ func Test_Storage_Nats_Set_Expiration(t *testing.T) {
exp = 1 * time.Second exp = 1 * time.Second
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, exp) err := testStore.Set(key, val, exp)
require.NoError(t, err) require.NoError(t, err)
time.Sleep(1100 * time.Millisecond) time.Sleep(1100 * time.Millisecond)
@@ -241,8 +237,7 @@ func Test_Storage_Nats_Set_Long_Expiration_with_Keys(t *testing.T) {
exp = 5 * time.Second exp = 5 * time.Second
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
keys, err := testStore.Keys() keys, err := testStore.Keys()
@@ -269,8 +264,7 @@ func Test_Storage_Nats_Set_Long_Expiration_with_Keys(t *testing.T) {
} }
func Test_Storage_Nats_Get_NotExist(t *testing.T) { func Test_Storage_Nats_Get_NotExist(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
@@ -288,11 +282,10 @@ func Test_Storage_Nats_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
keys, err := testStore.Keys() keys, err := testStore.Keys()
@@ -312,13 +305,12 @@ func Test_Storage_Nats_Delete(t *testing.T) {
} }
func Test_Storage_Nats_Reset(t *testing.T) { func Test_Storage_Nats_Reset(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
val := []byte("doe") val := []byte("doe")
err = testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set("john2", val, 0) err = testStore.Set("john2", val, 0)
@@ -345,14 +337,12 @@ func Test_Storage_Nats_Reset(t *testing.T) {
} }
func Test_Storage_Nats_Close(t *testing.T) { func Test_Storage_Nats_Close(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err) require.NoError(t, testStore.Close())
require.Nil(t, testStore.Close())
} }
func Test_Storage_Nats_Conn(t *testing.T) { func Test_Storage_Nats_Conn(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
n, k := testStore.Conn() n, k := testStore.Conn()
@@ -361,13 +351,13 @@ func Test_Storage_Nats_Conn(t *testing.T) {
} }
func Benchmark_Nats_Set(b *testing.B) { func Benchmark_Nats_Set(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -376,11 +366,10 @@ func Benchmark_Nats_Set(b *testing.B) {
} }
func Benchmark_Nats_Get(b *testing.B) { func Benchmark_Nats_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -394,13 +383,13 @@ func Benchmark_Nats_Get(b *testing.B) {
} }
func Benchmark_Nats_SetAndDelete(b *testing.B) { func Benchmark_Nats_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")

View File

@@ -46,6 +46,11 @@ func TestMain(m *testing.M) {
if err != nil { if err != nil {
log.Fatalf("Failed to get connection string: %v", err) log.Fatalf("Failed to get connection string: %v", err)
} }
defer func() {
if err := neo4jContainer.Terminate(ctx); err != nil {
log.Printf("Failed to terminate Neo4j container: %v", err)
}
}()
// Initialize Neo4j store with test container credentials // Initialize Neo4j store with test container credentials
store := New(Config{ store := New(Config{
@@ -58,11 +63,6 @@ func TestMain(m *testing.M) {
testStore = store testStore = store
defer testStore.Close() defer testStore.Close()
defer func() {
if err := neo4jContainer.Terminate(ctx); err != nil {
log.Printf("Failed to terminate Neo4j container: %v", err)
}
}()
code := m.Run() code := m.Run()
@@ -187,7 +187,7 @@ func Test_Neo4jStore_Non_UTF8(t *testing.T) {
} }
func Test_Neo4jStore_Close(t *testing.T) { func Test_Neo4jStore_Close(t *testing.T) {
require.Nil(t, testStore.Close()) require.NoError(t, testStore.Close())
} }
func Test_Neo4jStore_Conn(t *testing.T) { func Test_Neo4jStore_Conn(t *testing.T) {

View File

@@ -23,7 +23,7 @@ const (
postgresDatabase string = "fiber" postgresDatabase string = "fiber"
) )
func newTestConfig(t testing.TB) (Config, error) { func newTestConfig(t testing.TB) Config {
t.Helper() t.Helper()
ctx := context.Background() ctx := context.Background()
@@ -42,42 +42,32 @@ func newTestConfig(t testing.TB) (Config, error) {
testcontainers.CleanupContainer(t, c) testcontainers.CleanupContainer(t, c)
require.NoError(t, err) require.NoError(t, err)
var cfg Config
conn, err := c.ConnectionString(ctx, "sslmode=disable") conn, err := c.ConnectionString(ctx, "sslmode=disable")
if err != nil { require.NoError(t, err)
return cfg, err
}
cfg = Config{ cfg := Config{
ConnectionURI: conn, ConnectionURI: conn,
Reset: true, Reset: true,
} }
return cfg, nil return cfg
} }
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
cfg, err := newTestConfig(t) return newTestStoreWithConfig(t, newTestConfig(t))
if err != nil {
return nil, err
}
return newTestStoreWithConfig(t, cfg)
} }
func newTestStoreWithConfig(t testing.TB, cfg Config) (*Storage, error) { func newTestStoreWithConfig(t testing.TB, cfg Config) *Storage {
t.Helper() t.Helper()
return New(cfg), nil return New(cfg)
} }
func TestNoCreateUser(t *testing.T) { func TestNoCreateUser(t *testing.T) {
cfg, err := newTestConfig(t) cfg := newTestConfig(t)
require.NoError(t, err)
testStore, err := newTestStoreWithConfig(t, cfg) testStore := newTestStoreWithConfig(t, cfg)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
// Create a new user // Create a new user
@@ -88,7 +78,7 @@ func TestNoCreateUser(t *testing.T) {
username := "testuser" + strconv.Itoa(int(time.Now().UnixNano())) username := "testuser" + strconv.Itoa(int(time.Now().UnixNano()))
password := "testpassword" password := "testpassword"
_, err = conn.Exec(ctx, "CREATE USER "+username+" WITH PASSWORD '"+password+"'") _, err := conn.Exec(ctx, "CREATE USER "+username+" WITH PASSWORD '"+password+"'")
require.NoError(t, err) require.NoError(t, err)
_, err = conn.Exec(ctx, "GRANT CONNECT ON DATABASE "+postgresDatabase+" TO "+username) _, err = conn.Exec(ctx, "GRANT CONNECT ON DATABASE "+postgresDatabase+" TO "+username)
@@ -213,12 +203,11 @@ func TestNoCreateUser(t *testing.T) {
} }
func Test_Should_Panic_On_Wrong_Schema(t *testing.T) { func Test_Should_Panic_On_Wrong_Schema(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
// Create a test table with wrong schema // Create a test table with wrong schema
_, err = testStore.Conn().Exec(context.Background(), ` _, err := testStore.Conn().Exec(context.Background(), `
CREATE TABLE IF NOT EXISTS test_schema_table ( CREATE TABLE IF NOT EXISTS test_schema_table (
k VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '', k VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '',
v BYTEA NOT NULL, v BYTEA NOT NULL,
@@ -243,11 +232,10 @@ func Test_Postgres_Set(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -257,11 +245,10 @@ func Test_Postgres_Set_Override(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set(key, val, 0) err = testStore.Set(key, val, 0)
@@ -274,11 +261,10 @@ func Test_Postgres_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -293,11 +279,10 @@ func Test_Postgres_Set_Expiration(t *testing.T) {
exp = 1 * time.Second exp = 1 * time.Second
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, exp) err := testStore.Set(key, val, exp)
require.NoError(t, err) require.NoError(t, err)
time.Sleep(1100 * time.Millisecond) time.Sleep(1100 * time.Millisecond)
@@ -310,8 +295,7 @@ func Test_Postgres_Set_Expiration(t *testing.T) {
func Test_Postgres_Get_Expired(t *testing.T) { func Test_Postgres_Get_Expired(t *testing.T) {
key := "john" key := "john"
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -320,8 +304,7 @@ func Test_Postgres_Get_Expired(t *testing.T) {
} }
func Test_Postgres_Get_NotExist(t *testing.T) { func Test_Postgres_Get_NotExist(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
@@ -335,11 +318,10 @@ func Test_Postgres_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Delete(key) err = testStore.Delete(key)
@@ -353,11 +335,10 @@ func Test_Postgres_Delete(t *testing.T) {
func Test_Postgres_Reset(t *testing.T) { func Test_Postgres_Reset(t *testing.T) {
val := []byte("doe") val := []byte("doe")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set("john2", val, 0) err = testStore.Set("john2", val, 0)
@@ -378,12 +359,11 @@ func Test_Postgres_Reset(t *testing.T) {
func Test_Postgres_GC(t *testing.T) { func Test_Postgres_GC(t *testing.T) {
testVal := []byte("doe") testVal := []byte("doe")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
// This key should expire // This key should expire
err = testStore.Set("john", testVal, time.Nanosecond) err := testStore.Set("john", testVal, time.Nanosecond)
require.NoError(t, err) require.NoError(t, err)
testStore.gc(time.Now()) testStore.gc(time.Now())
@@ -404,11 +384,10 @@ func Test_Postgres_GC(t *testing.T) {
func Test_Postgres_Non_UTF8(t *testing.T) { func Test_Postgres_Non_UTF8(t *testing.T) {
val := []byte("0xF5") val := []byte("0xF5")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("0xF6", val, 0) err := testStore.Set("0xF6", val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get("0xF6") result, err := testStore.Get("0xF6")
@@ -425,28 +404,25 @@ func Test_SslRequiredMode(t *testing.T) {
} }
func Test_Postgres_Conn(t *testing.T) { func Test_Postgres_Conn(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
require.True(t, testStore.Conn() != nil) require.True(t, testStore.Conn() != nil)
} }
func Test_Postgres_Close(t *testing.T) { func Test_Postgres_Close(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err) require.NoError(t, testStore.Close())
require.Nil(t, testStore.Close())
} }
func Benchmark_Postgres_Set(b *testing.B) { func Benchmark_Postgres_Set(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -455,11 +431,10 @@ func Benchmark_Postgres_Set(b *testing.B) {
} }
func Benchmark_Postgres_Get(b *testing.B) { func Benchmark_Postgres_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -473,13 +448,13 @@ func Benchmark_Postgres_Get(b *testing.B) {
} }
func Benchmark_Postgres_SetAndDelete(b *testing.B) { func Benchmark_Postgres_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")

View File

@@ -6,6 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/minio" "github.com/testcontainers/testcontainers-go/modules/minio"
"github.com/testcontainers/testcontainers-go/wait" "github.com/testcontainers/testcontainers-go/wait"
@@ -15,8 +16,6 @@ const (
bucket = "testbucket" bucket = "testbucket"
) )
var testStore *Storage
const ( const (
// minioImage is the default image used for running S3 in tests. // minioImage is the default image used for running S3 in tests.
minioImage = "docker.io/minio/minio:latest" minioImage = "docker.io/minio/minio:latest"
@@ -25,7 +24,7 @@ const (
minioPass string = "minio-password" minioPass string = "minio-password"
) )
func TestMain(m *testing.M) { func newTestStore(t testing.TB) *Storage {
img := minioImage img := minioImage
if imgFromEnv := os.Getenv(minioImageEnvVar); imgFromEnv != "" { if imgFromEnv := os.Getenv(minioImageEnvVar); imgFromEnv != "" {
img = imgFromEnv img = imgFromEnv
@@ -51,7 +50,7 @@ func TestMain(m *testing.M) {
panic(err) panic(err)
} }
testStore = New( testStore := New(
Config{ Config{
Bucket: bucket, Bucket: bucket,
Endpoint: "http://" + conn, Endpoint: "http://" + conn,
@@ -65,12 +64,9 @@ func TestMain(m *testing.M) {
) )
// Create test bucket. // Create test bucket.
_ = testStore.CreateBucket(bucket) err = testStore.CreateBucket(bucket)
require.NoError(t, err)
// Do not delete test bucket, as the container is disposed after each test.
exitVal := m.Run() return testStore
// Delete test bucket.
_ = testStore.DeleteBucket(bucket)
os.Exit(exitVal)
} }

View File

@@ -11,6 +11,9 @@ import (
func Test_S3_CreateDeleteBucket(t *testing.T) { func Test_S3_CreateDeleteBucket(t *testing.T) {
bkt := "test-new-bucket" bkt := "test-new-bucket"
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.CreateBucket(bkt) err := testStore.CreateBucket(bkt)
require.NoError(t, err) require.NoError(t, err)
@@ -21,6 +24,9 @@ func Test_S3_CreateDeleteBucket(t *testing.T) {
func Test_S3_DeleteMany(t *testing.T) { func Test_S3_DeleteMany(t *testing.T) {
val := []byte("doe") val := []byte("doe")
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
@@ -61,6 +67,9 @@ func Test_S3_SetWithChecksum(t *testing.T) {
types.ChecksumAlgorithmSha256: sha256sum, types.ChecksumAlgorithmSha256: sha256sum,
} }
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.SetWithChecksum(key, val, checksum) err := testStore.SetWithChecksum(key, val, checksum)
require.NoError(t, err) require.NoError(t, err)

View File

@@ -12,6 +12,9 @@ func Test_S3_Set(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -22,6 +25,9 @@ func Test_S3_Set_Override(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
@@ -35,6 +41,9 @@ func Test_S3_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
@@ -44,6 +53,9 @@ func Test_S3_Get(t *testing.T) {
} }
func Test_S3_Get_NotExist(t *testing.T) { func Test_S3_Get_NotExist(t *testing.T) {
testStore := newTestStore(t)
defer testStore.Close()
result, err := testStore.Get("notexist") result, err := testStore.Get("notexist")
require.NoError(t, err) require.NoError(t, err)
require.Zero(t, len(result)) require.Zero(t, len(result))
@@ -55,6 +67,9 @@ func Test_S3_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
@@ -69,6 +84,9 @@ func Test_S3_Delete(t *testing.T) {
func Test_S3_Reset(t *testing.T) { func Test_S3_Reset(t *testing.T) {
val := []byte("doe") val := []byte("doe")
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
@@ -88,14 +106,20 @@ func Test_S3_Reset(t *testing.T) {
} }
func Test_S3_Close(t *testing.T) { func Test_S3_Close(t *testing.T) {
require.Nil(t, testStore.Close()) testStore := newTestStore(t)
require.NoError(t, testStore.Close())
} }
func Test_S3_Conn(t *testing.T) { func Test_S3_Conn(t *testing.T) {
testStore := newTestStore(t)
defer testStore.Close()
require.True(t, testStore.Conn() != nil) require.True(t, testStore.Conn() != nil)
} }
func Benchmark_S3_Set(b *testing.B) { func Benchmark_S3_Set(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
@@ -108,6 +132,9 @@ func Benchmark_S3_Set(b *testing.B) {
} }
func Benchmark_S3_Get(b *testing.B) { func Benchmark_S3_Get(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
err := testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
@@ -122,6 +149,9 @@ func Benchmark_S3_Get(b *testing.B) {
} }
func Benchmark_S3_SetAndDelete(b *testing.B) { func Benchmark_S3_SetAndDelete(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()

View File

@@ -18,7 +18,7 @@ const (
scyllaDBEnvVar = "TEST_SCYLLADB_IMAGE" scyllaDBEnvVar = "TEST_SCYLLADB_IMAGE"
) )
func newTestStore(t testing.TB) (*Storage, error) { func newTestStore(t testing.TB) *Storage {
t.Helper() t.Helper()
img := scyllaDBImage img := scyllaDBImage
@@ -30,19 +30,13 @@ func newTestStore(t testing.TB) (*Storage, error) {
c, err := scylladb.Run(ctx, img) c, err := scylladb.Run(ctx, img)
testcontainers.CleanupContainer(t, c) testcontainers.CleanupContainer(t, c)
if err != nil { require.NoError(t, err)
return nil, err
}
connectionHost, err := c.NonShardAwareConnectionHost(ctx) connectionHost, err := c.NonShardAwareConnectionHost(ctx)
if err != nil { require.NoError(t, err)
return nil, err
}
scyllaPort, err := c.MappedPort(ctx, "9042/tcp") scyllaPort, err := c.MappedPort(ctx, "9042/tcp")
if err != nil { require.NoError(t, err)
return nil, err
}
return New(Config{ return New(Config{
Hosts: []string{connectionHost}, Hosts: []string{connectionHost},
@@ -51,7 +45,7 @@ func newTestStore(t testing.TB) (*Storage, error) {
// Disable initial host lookup in tests. // Disable initial host lookup in tests.
// See https://github.com/apache/cassandra-gocql-driver/issues/1020#issuecomment-362494859 // See https://github.com/apache/cassandra-gocql-driver/issues/1020#issuecomment-362494859
DisableInitialHostLookup: true, DisableInitialHostLookup: true,
}), nil })
} }
func Test_Scylla_Set(t *testing.T) { func Test_Scylla_Set(t *testing.T) {
@@ -61,11 +55,10 @@ func Test_Scylla_Set(t *testing.T) {
value = []byte("doe") value = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, value, 0) err := testStore.Set(key, value, 0)
require.NoError(t, err) require.NoError(t, err)
} }
@@ -76,11 +69,10 @@ func Test_Scylla_Set_Override_Get(t *testing.T) {
valOverride = []byte("doe2") valOverride = []byte("doe2")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, valInitial, 0) err := testStore.Set(key, valInitial, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -101,11 +93,10 @@ func Test_Scylla_Get(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
result, err := testStore.Get(key) result, err := testStore.Get(key)
@@ -120,11 +111,10 @@ func Test_Scylla_Set_Expiration_Get(t *testing.T) {
exp = 1 * time.Second exp = 1 * time.Second
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, exp) err := testStore.Set(key, val, exp)
require.NoError(t, err) require.NoError(t, err)
time.Sleep(1001 * time.Millisecond) time.Sleep(1001 * time.Millisecond)
@@ -135,8 +125,7 @@ func Test_Scylla_Set_Expiration_Get(t *testing.T) {
} }
func Test_Scylla_Get_NotExist(t *testing.T) { func Test_Scylla_Get_NotExist(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
result, err := testStore.Get("not-exist") result, err := testStore.Get("not-exist")
@@ -150,11 +139,10 @@ func Test_Scylla_Delete(t *testing.T) {
val = []byte("doe") val = []byte("doe")
) )
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set(key, val, 0) err := testStore.Set(key, val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Delete(key) err = testStore.Delete(key)
@@ -168,11 +156,10 @@ func Test_Scylla_Delete(t *testing.T) {
func Test_Scylla_Reset(t *testing.T) { func Test_Scylla_Reset(t *testing.T) {
var val = []byte("doe") var val = []byte("doe")
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john1", val, 0) err := testStore.Set("john1", val, 0)
require.NoError(t, err) require.NoError(t, err)
err = testStore.Set("john2", val, 0) err = testStore.Set("john2", val, 0)
@@ -191,27 +178,25 @@ func Test_Scylla_Reset(t *testing.T) {
} }
func Test_Scylla_Close(t *testing.T) { func Test_Scylla_Close(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err) require.NoError(t, testStore.Close())
require.Nil(t, testStore.Close())
} }
func Test_Scylla_Conn(t *testing.T) { func Test_Scylla_Conn(t *testing.T) {
testStore, err := newTestStore(t) testStore := newTestStore(t)
require.NoError(t, err)
defer testStore.Close() defer testStore.Close()
require.True(t, testStore.Conn() != nil) require.True(t, testStore.Conn() != nil)
} }
func Benchmark_Scylla_Set(b *testing.B) { func Benchmark_Scylla_Set(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0) err = testStore.Set("john", []byte("doe"), 0)
} }
@@ -220,11 +205,10 @@ func Benchmark_Scylla_Set(b *testing.B) {
} }
func Benchmark_Scylla_Get(b *testing.B) { func Benchmark_Scylla_Get(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0) err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err) require.NoError(b, err)
b.ReportAllocs() b.ReportAllocs()
@@ -238,13 +222,13 @@ func Benchmark_Scylla_Get(b *testing.B) {
} }
func Benchmark_Scylla_SetAndDelete(b *testing.B) { func Benchmark_Scylla_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b) testStore := newTestStore(b)
require.NoError(b, err)
defer testStore.Close() defer testStore.Close()
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
var err error
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0) _ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john") err = testStore.Delete("john")