Skip to content

Commit

Permalink
add TestMain for all storages
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Sep 15, 2023
1 parent 55eca1c commit 6335aa4
Showing 1 changed file with 27 additions and 52 deletions.
79 changes: 27 additions & 52 deletions coherence/coherence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package coherence
*/
import (
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)
Expand All @@ -17,6 +18,19 @@ var (
value2 = []byte("value2")
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore, _ = New(Config{
Reset: true,
})

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) {
t.Helper()
Expand All @@ -32,27 +46,20 @@ func newTestStore(t testing.TB, config ...Config) (*Storage, error) {
func Test_Coherence_Set_And_Get(t *testing.T) {
var val []byte

testStore, err := newTestStore(t)
require.NoError(t, err)

err = testStore.Set(key1, value1, 0)
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)

val, err = testStore.Get(key1)
require.NoError(t, err)
require.Equal(t, value1, val)

require.NotNil(t, testStore.Conn())
require.Equal(t, nil, testStore.Close())
}

func Test_Coherence_Set_Override(t *testing.T) {
var val []byte

testStore, err := newTestStore(t)
require.NoError(t, err)

err = testStore.Set(key1, value1, 0)
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)

err = testStore.Set(key1, value2, 0)
Expand All @@ -61,72 +68,55 @@ func Test_Coherence_Set_Override(t *testing.T) {
val, err = testStore.Get(key1)
require.NoError(t, err)
require.Equal(t, value2, val)

require.Equal(t, nil, testStore.Close())
}

func Test_Coherence_Set_With_Reset(t *testing.T) {
var val []byte

testStore, err := newTestStore(t)
require.NoError(t, err)

err = testStore.Set(key1, value1, 0)
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)

val, err = testStore.Get(key1)
require.NoError(t, err)
require.Equal(t, value1, val)

// get a new store but reset it, so the subsequent Get will return nil
testStore, err = newTestStore(t, Config{Reset: true})
testStore2, err := newTestStore(t, Config{Reset: true})
require.NoError(t, err)

val, err = testStore.Get(key1)
val, err = testStore2.Get(key1)
require.NoError(t, err)
require.True(t, len(val) == 0)

require.Equal(t, nil, testStore.Close())
require.Equal(t, nil, testStore2.Close())
}

func Test_Coherence_Set_With_Expiry(t *testing.T) {
var val []byte

testStore, err := newTestStore(t)
require.NoError(t, err)

// set with an expiry of 5 seconds
err = testStore.Set(key1, value1, time.Duration(5)*time.Second)
err := testStore.Set(key1, value1, time.Duration(5)*time.Second)
require.NoError(t, err)
time.Sleep(time.Duration(6) * time.Second)

val, err = testStore.Get(key1)
require.NoError(t, err)
require.True(t, len(val) == 0)

require.Equal(t, nil, testStore.Close())
}

func Test_Coherence_Get_Missing(t *testing.T) {
var val []byte

testStore, err := newTestStore(t)
require.NoError(t, err)

val, err = testStore.Get(missingKey)
val, err := testStore.Get(missingKey)
require.NoError(t, err)
require.True(t, len(val) == 0)

require.Equal(t, nil, testStore.Close())
}

func Test_Coherence_Reset(t *testing.T) {
var val []byte

testStore, err := newTestStore(t)
require.NoError(t, err)

err = testStore.Set(key1, value1, 0)
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)

err = testStore.Set(key2, value2, 0)
Expand All @@ -152,17 +142,12 @@ func Test_Coherence_Reset(t *testing.T) {
val, err = testStore.Get(key2)
require.NoError(t, err)
require.True(t, len(val) == 0)

require.Equal(t, nil, testStore.Close())
}

func Test_Coherence_Set_And_Delete(t *testing.T) {
var val []byte

testStore, err := newTestStore(t)
require.NoError(t, err)

err = testStore.Set(key1, value1, 0)
err := testStore.Set(key1, value1, 0)
require.NoError(t, err)

err = testStore.Delete(key1)
Expand All @@ -172,8 +157,6 @@ func Test_Coherence_Set_And_Delete(t *testing.T) {
val, err = testStore.Get(key1)
require.NoError(t, err)
require.True(t, len(val) == 0)

require.Equal(t, nil, testStore.Close())
}

// TestCoherenceWithScope ensures we can create multiple session stores with multiple scopes.
Expand Down Expand Up @@ -208,12 +191,10 @@ func Test_Coherence_With_Scope(t *testing.T) {
}

func Benchmark_Coherence_Set(b *testing.B) {
testStore, err := newTestStore(b)
require.NoError(b, err)

b.ReportAllocs()
b.ResetTimer()

var err error
for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0)
}
Expand All @@ -222,10 +203,7 @@ func Benchmark_Coherence_Set(b *testing.B) {
}

func Benchmark_Coherence_Get(b *testing.B) {
testStore, err := newTestStore(b)
require.NoError(b, err)

err = testStore.Set("john", []byte("doe"), 0)
err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)

b.ReportAllocs()
Expand All @@ -239,10 +217,7 @@ func Benchmark_Coherence_Get(b *testing.B) {
}

func Benchmark_Coherence_Delete(b *testing.B) {
testStore, err := newTestStore(b)
require.NoError(b, err)

err = testStore.Set("john", []byte("doe"), 0)
err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)

b.ReportAllocs()
Expand Down

0 comments on commit 6335aa4

Please sign in to comment.