feat(coherence): use testcontainers-go in tests

This commit is contained in:
Manuel de la Peña
2025-05-01 02:25:07 +02:00
parent 79d6043639
commit 7ed83dd161
3 changed files with 311 additions and 52 deletions

View File

@@ -4,10 +4,14 @@ package coherence
* Copyright © 2023, 2024 Oracle and/or its affiliates.
*/
import (
"github.com/stretchr/testify/require"
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
var (
@@ -18,35 +22,74 @@ var (
value2 = []byte("value2")
)
var testStore *Storage
const (
// coherenceImage is the default image used for running coherence in tests.
coherenceImage = "ghcr.io/oracle/coherence-ce:25.03.1-graal"
coherenceImageEnvVar string = "TEST_COHERENCE_IMAGE"
coherencePort = "1408/tcp"
coherencePort2 = "30000/tcp"
)
func TestMain(m *testing.M) {
testStore, _ = New(Config{
Reset: true,
NearCacheTimeout: time.Duration(4) * time.Second,
})
code := m.Run()
_ = testStore.Close()
os.Exit(code)
}
// newTestStore returns a new Coherence Store and ensures it is reset.
func newTestStore(t testing.TB, config ...Config) (*Storage, error) {
func newTestConfig(t testing.TB) Config {
t.Helper()
testStore, err := New(config...)
img := coherenceImage
if imgFromEnv := os.Getenv(coherenceImageEnvVar); imgFromEnv != "" {
img = imgFromEnv
}
ctx := context.Background()
c, err := testcontainers.Run(ctx,
img,
testcontainers.WithExposedPorts(coherencePort, coherencePort2),
testcontainers.WithWaitStrategy(
wait.ForListeningPort(coherencePort),
wait.ForListeningPort(coherencePort2),
),
)
testcontainers.CleanupContainer(t, c)
require.NoError(t, err)
err = testStore.Reset()
host, err := c.Host(ctx)
require.NoError(t, err)
return testStore, err
port, err := c.MappedPort(ctx, coherencePort)
require.NoError(t, err)
return Config{
Reset: true,
Address: host + ":" + port.Port(),
NearCacheTimeout: time.Duration(4) * time.Second,
}
}
func newTestStore(t testing.TB) *Storage {
return newTestStoreWithConfig(t, nil)
}
func newTestStoreWithConfig(t testing.TB, config *Config) *Storage {
t.Helper()
var cfg Config
if config == nil {
cfg = newTestConfig(t)
} else {
cfg = *config
}
storage, err := New(cfg)
require.NoError(t, err)
return storage
}
func Test_Coherence_Set_And_Get(t *testing.T) {
var val []byte
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)
@@ -60,6 +103,9 @@ func Test_Coherence_Set_And_Get(t *testing.T) {
func Test_Coherence_Set_Override(t *testing.T) {
var val []byte
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)
@@ -74,6 +120,11 @@ func Test_Coherence_Set_Override(t *testing.T) {
func Test_Coherence_Set_With_Reset(t *testing.T) {
var val []byte
cfg := newTestConfig(t)
testStore := newTestStoreWithConfig(t, &cfg)
defer testStore.Close()
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)
@@ -82,7 +133,10 @@ func Test_Coherence_Set_With_Reset(t *testing.T) {
require.Equal(t, value1, val)
// get a new store but reset it, so the subsequent Get will return nil
testStore2, err := newTestStore(t, Config{Reset: true})
testStore2 := newTestStoreWithConfig(t, &cfg)
defer testStore2.Close()
err = testStore2.Reset()
require.NoError(t, err)
val, err = testStore2.Get(key1)
@@ -95,6 +149,9 @@ func Test_Coherence_Set_With_Reset(t *testing.T) {
func Test_Coherence_Set_With_Expiry(t *testing.T) {
var val []byte
testStore := newTestStore(t)
defer testStore.Close()
// set with an expiry of 5 seconds
err := testStore.Set(key1, value1, time.Duration(5)*time.Second)
require.NoError(t, err)
@@ -108,6 +165,9 @@ func Test_Coherence_Set_With_Expiry(t *testing.T) {
func Test_Coherence_Get_Missing(t *testing.T) {
var val []byte
testStore := newTestStore(t)
defer testStore.Close()
val, err := testStore.Get(missingKey)
require.NoError(t, err)
require.True(t, len(val) == 0)
@@ -116,6 +176,9 @@ func Test_Coherence_Get_Missing(t *testing.T) {
func Test_Coherence_Reset(t *testing.T) {
var val []byte
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)
@@ -133,6 +196,7 @@ func Test_Coherence_Reset(t *testing.T) {
// reset the store, this should remove both entries
err = testStore.Reset()
require.NoError(t, err)
// check the keys have expired
val, err = testStore.Get(key1)
@@ -147,6 +211,9 @@ func Test_Coherence_Reset(t *testing.T) {
func Test_Coherence_Set_And_Delete(t *testing.T) {
var val []byte
testStore := newTestStore(t)
defer testStore.Close()
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)
@@ -163,15 +230,22 @@ func Test_Coherence_Set_And_Delete(t *testing.T) {
func Test_Coherence_With_Scope(t *testing.T) {
var val []byte
// create two session stores with different scopes
testStore1, err := newTestStore(t, Config{ScopeName: "scope1"})
require.NoError(t, err)
cfg := newTestConfig(t)
testStore2, err := newTestStore(t, Config{ScopeName: "scope2"})
require.NoError(t, err)
cfg1 := cfg
cfg1.ScopeName = "scope1"
testStore1 := newTestStoreWithConfig(t, &cfg1)
defer testStore1.Close()
// create two session stores with different scopes
cfg2 := cfg
cfg2.ScopeName = "scope2"
testStore2 := newTestStoreWithConfig(t, &cfg2)
defer testStore2.Close()
// ensure we can put the same key with different values in each scope
err = testStore1.Set(key1, value1, 0)
err := testStore1.Set(key1, value1, 0)
require.NoError(t, err)
err = testStore2.Set(key1, value2, 0)
@@ -185,12 +259,12 @@ func Test_Coherence_With_Scope(t *testing.T) {
val, err = testStore2.Get(key1)
require.NoError(t, err)
require.Equal(t, value2, val)
require.NoError(t, testStore1.Close())
require.NoError(t, testStore2.Close())
}
func Benchmark_Coherence_Set(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs()
b.ResetTimer()
@@ -203,6 +277,9 @@ func Benchmark_Coherence_Set(b *testing.B) {
}
func Benchmark_Coherence_Get(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)
@@ -217,6 +294,9 @@ func Benchmark_Coherence_Get(b *testing.B) {
}
func Benchmark_Coherence_SetAndDelete(b *testing.B) {
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs()
b.ResetTimer()