2024-04-22 08:47:50 +03:00
|
|
|
package memory
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/store"
|
2021-02-12 16:33:16 +03:00
|
|
|
)
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
type testHook struct {
|
|
|
|
f bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testHook) Exists(fn store.FuncExists) store.FuncExists {
|
|
|
|
return func(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
|
|
|
t.f = true
|
|
|
|
return fn(ctx, key, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHook(t *testing.T) {
|
|
|
|
h := &testHook{}
|
|
|
|
|
|
|
|
s := NewStore(store.Hooks(store.HookExists(h.Exists)))
|
|
|
|
|
|
|
|
if err := s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Write(context.TODO(), "test", nil); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Exists(context.TODO(), "test"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !h.f {
|
|
|
|
t.Fatal("hook not works")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
func TestMemoryReInit(t *testing.T) {
|
2024-04-22 08:47:50 +03:00
|
|
|
s := NewStore(store.Namespace("aaa"))
|
2021-07-14 17:11:37 +03:00
|
|
|
if err := s.Init(store.Namespace("")); err != nil {
|
2021-02-13 01:46:16 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-07-14 17:11:37 +03:00
|
|
|
if len(s.Options().Namespace) > 0 {
|
2021-02-12 16:33:16 +03:00
|
|
|
t.Error("Init didn't reinitialise the store")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryBasic(t *testing.T) {
|
2024-04-22 08:47:50 +03:00
|
|
|
s := NewStore()
|
2021-02-13 01:46:16 +03:00
|
|
|
if err := s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
basictest(s, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryPrefix(t *testing.T) {
|
2024-04-22 08:47:50 +03:00
|
|
|
s := NewStore()
|
2021-07-14 17:11:37 +03:00
|
|
|
if err := s.Init(store.Namespace("some-prefix")); err != nil {
|
2021-02-13 01:46:16 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
basictest(s, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryNamespace(t *testing.T) {
|
2024-04-22 08:47:50 +03:00
|
|
|
s := NewStore()
|
2021-07-14 17:11:37 +03:00
|
|
|
if err := s.Init(store.Namespace("some-namespace")); err != nil {
|
2021-02-13 01:46:16 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
basictest(s, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryNamespacePrefix(t *testing.T) {
|
2024-04-22 08:47:50 +03:00
|
|
|
s := NewStore()
|
2021-07-14 17:11:37 +03:00
|
|
|
if err := s.Init(store.Namespace("some-namespace")); err != nil {
|
2021-02-13 01:46:16 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
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)
|
2021-02-13 01:46:16 +03:00
|
|
|
} else if string(val) != "World" {
|
|
|
|
t.Errorf("Expected %s, got %s", "World", val)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
if err := s.Read(ctx, "Hello", &val); err != store.ErrNotFound {
|
|
|
|
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
|
|
|
|
}
|
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
if err := s.Disconnect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|