micro/store/memory_test.go
Vasiliy Tolstov c3def24bf4 store: add Wrappers support, create Namespace wrapper
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-07-14 17:11:37 +03:00

74 lines
1.6 KiB
Go

package store_test
import (
"context"
"testing"
"time"
"github.com/unistack-org/micro/v3/store"
)
func TestMemoryReInit(t *testing.T) {
s := store.NewStore(store.Namespace("aaa"))
if err := s.Init(store.Namespace("")); err != nil {
t.Fatal(err)
}
if len(s.Options().Namespace) > 0 {
t.Error("Init didn't reinitialise the store")
}
}
func TestMemoryBasic(t *testing.T) {
s := store.NewStore()
if err := s.Init(); err != nil {
t.Fatal(err)
}
basictest(s, t)
}
func TestMemoryPrefix(t *testing.T) {
s := store.NewStore()
if err := s.Init(store.Namespace("some-prefix")); err != nil {
t.Fatal(err)
}
basictest(s, t)
}
func TestMemoryNamespace(t *testing.T) {
s := store.NewStore()
if err := s.Init(store.Namespace("some-namespace")); err != nil {
t.Fatal(err)
}
basictest(s, t)
}
func TestMemoryNamespacePrefix(t *testing.T) {
s := store.NewStore()
if err := s.Init(store.Namespace("some-namespace")); err != nil {
t.Fatal(err)
}
basictest(s, t)
}
func basictest(s store.Store, t *testing.T) {
ctx := context.Background()
// Read and Write an expiring Record
if err := s.Write(ctx, "Hello", "World", store.WriteTTL(time.Millisecond*100)); err != nil {
t.Error(err)
}
var val []byte
if err := s.Read(ctx, "Hello", &val); err != nil {
t.Error(err)
} else if string(val) != "World" {
t.Errorf("Expected %s, got %s", "World", val)
}
time.Sleep(time.Millisecond * 200)
if err := s.Read(ctx, "Hello", &val); err != store.ErrNotFound {
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
}
if err := s.Disconnect(ctx); err != nil {
t.Fatal(err)
}
}