2019-07-11 12:51:55 +08:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2020-03-12 13:41:30 +00:00
|
|
|
"fmt"
|
2019-07-11 12:51:55 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
"github.com/kr/pretty"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-07-11 12:51:55 +08:00
|
|
|
)
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func TestMemoryReInit(t *testing.T) {
|
2020-04-06 16:45:55 +01:00
|
|
|
s := NewStore(store.Table("aaa"))
|
|
|
|
s.Init(store.Table(""))
|
|
|
|
if len(s.Options().Table) > 0 {
|
2020-03-12 13:41:30 +00:00
|
|
|
t.Error("Init didn't reinitialise the store")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryBasic(t *testing.T) {
|
2019-07-11 12:51:55 +08:00
|
|
|
s := NewStore()
|
2020-03-12 13:41:30 +00:00
|
|
|
s.Init()
|
|
|
|
basictest(s, t)
|
|
|
|
}
|
2019-07-11 12:51:55 +08:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func TestMemoryPrefix(t *testing.T) {
|
|
|
|
s := NewStore()
|
2020-04-06 16:45:55 +01:00
|
|
|
s.Init(store.Table("some-prefix"))
|
2020-03-12 13:41:30 +00:00
|
|
|
basictest(s, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryNamespace(t *testing.T) {
|
|
|
|
s := NewStore()
|
2020-04-06 16:45:55 +01:00
|
|
|
s.Init(store.Database("some-namespace"))
|
2020-03-12 13:41:30 +00:00
|
|
|
basictest(s, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryNamespacePrefix(t *testing.T) {
|
|
|
|
s := NewStore()
|
2020-04-06 16:45:55 +01:00
|
|
|
s.Init(store.Table("some-prefix"), store.Database("some-namespace"))
|
2020-03-12 13:41:30 +00:00
|
|
|
basictest(s, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func basictest(s store.Store, t *testing.T) {
|
|
|
|
t.Logf("Testing store %s, with options %# v\n", s.String(), pretty.Formatter(s.Options()))
|
|
|
|
// Read and Write an expiring Record
|
|
|
|
if err := s.Write(&store.Record{
|
|
|
|
Key: "Hello",
|
|
|
|
Value: []byte("World"),
|
|
|
|
Expiry: time.Millisecond * 100,
|
|
|
|
}); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if r, err := s.Read("Hello"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
if len(r) != 1 {
|
|
|
|
t.Error("Read returned multiple records")
|
|
|
|
}
|
|
|
|
if r[0].Key != "Hello" {
|
|
|
|
t.Errorf("Expected %s, got %s", "Hello", r[0].Key)
|
|
|
|
}
|
|
|
|
if string(r[0].Value) != "World" {
|
|
|
|
t.Errorf("Expected %s, got %s", "World", r[0].Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
if _, err := s.Read("Hello"); err != store.ErrNotFound {
|
|
|
|
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
|
2019-07-11 12:51:55 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// Write 3 records with various expiry and get with prefix
|
|
|
|
records := []*store.Record{
|
|
|
|
&store.Record{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("foofoo"),
|
|
|
|
},
|
|
|
|
&store.Record{
|
|
|
|
Key: "foobar",
|
|
|
|
Value: []byte("foobarfoobar"),
|
|
|
|
Expiry: time.Millisecond * 100,
|
|
|
|
},
|
|
|
|
&store.Record{
|
|
|
|
Key: "foobarbaz",
|
|
|
|
Value: []byte("foobarbazfoobarbaz"),
|
|
|
|
Expiry: 2 * time.Millisecond * 100,
|
|
|
|
},
|
2019-07-11 12:51:55 +08:00
|
|
|
}
|
2020-03-12 13:41:30 +00:00
|
|
|
for _, r := range records {
|
|
|
|
if err := s.Write(r); err != nil {
|
|
|
|
t.Errorf("Couldn't write k: %s, v: %# v (%s)", r.Key, pretty.Formatter(r.Value), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if results, err := s.Read("foo", store.ReadPrefix()); err != nil {
|
|
|
|
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", pretty.Formatter(results), err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 3 {
|
|
|
|
t.Errorf("Expected 3 items, got %d", len(results))
|
|
|
|
}
|
|
|
|
t.Logf("Prefix test: %v\n", pretty.Formatter(results))
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
if results, err := s.Read("foo", store.ReadPrefix()); err != nil {
|
|
|
|
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", pretty.Formatter(results), err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 2 {
|
|
|
|
t.Errorf("Expected 2 items, got %d", len(results))
|
|
|
|
}
|
|
|
|
t.Logf("Prefix test: %v\n", pretty.Formatter(results))
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
if results, err := s.Read("foo", store.ReadPrefix()); err != nil {
|
|
|
|
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", pretty.Formatter(results), err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 1 {
|
|
|
|
t.Errorf("Expected 1 item, got %d", len(results))
|
|
|
|
}
|
|
|
|
t.Logf("Prefix test: %# v\n", pretty.Formatter(results))
|
|
|
|
}
|
|
|
|
if err := s.Delete("foo", func(d *store.DeleteOptions) {}); err != nil {
|
|
|
|
t.Errorf("Delete failed (%v)", err)
|
|
|
|
}
|
|
|
|
if results, err := s.Read("foo", store.ReadPrefix()); err != nil {
|
|
|
|
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", pretty.Formatter(results), err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 0 {
|
|
|
|
t.Errorf("Expected 0 items, got %d (%# v)", len(results), pretty.Formatter(results))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write 3 records with various expiry and get with Suffix
|
|
|
|
records = []*store.Record{
|
|
|
|
&store.Record{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("foofoo"),
|
|
|
|
},
|
|
|
|
&store.Record{
|
|
|
|
Key: "barfoo",
|
|
|
|
Value: []byte("barfoobarfoo"),
|
|
|
|
Expiry: time.Millisecond * 100,
|
|
|
|
},
|
|
|
|
&store.Record{
|
|
|
|
Key: "bazbarfoo",
|
|
|
|
Value: []byte("bazbarfoobazbarfoo"),
|
|
|
|
Expiry: 2 * time.Millisecond * 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, r := range records {
|
|
|
|
if err := s.Write(r); err != nil {
|
|
|
|
t.Errorf("Couldn't write k: %s, v: %# v (%s)", r.Key, pretty.Formatter(r.Value), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if results, err := s.Read("foo", store.ReadSuffix()); err != nil {
|
|
|
|
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", pretty.Formatter(results), err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 3 {
|
|
|
|
t.Errorf("Expected 3 items, got %d", len(results))
|
|
|
|
}
|
|
|
|
t.Logf("Prefix test: %v\n", pretty.Formatter(results))
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
if results, err := s.Read("foo", store.ReadSuffix()); err != nil {
|
|
|
|
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", pretty.Formatter(results), err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 2 {
|
|
|
|
t.Errorf("Expected 2 items, got %d", len(results))
|
|
|
|
}
|
|
|
|
t.Logf("Prefix test: %v\n", pretty.Formatter(results))
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
if results, err := s.Read("foo", store.ReadSuffix()); err != nil {
|
|
|
|
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", pretty.Formatter(results), err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 1 {
|
|
|
|
t.Errorf("Expected 1 item, got %d", len(results))
|
|
|
|
}
|
|
|
|
t.Logf("Prefix test: %# v\n", pretty.Formatter(results))
|
|
|
|
}
|
|
|
|
if err := s.Delete("foo"); err != nil {
|
|
|
|
t.Errorf("Delete failed (%v)", err)
|
|
|
|
}
|
|
|
|
if results, err := s.Read("foo", store.ReadSuffix()); err != nil {
|
|
|
|
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", pretty.Formatter(results), err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 0 {
|
|
|
|
t.Errorf("Expected 0 items, got %d (%# v)", len(results), pretty.Formatter(results))
|
|
|
|
}
|
2019-07-11 12:51:55 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
// Test Prefix, Suffix and WriteOptions
|
|
|
|
if err := s.Write(&store.Record{
|
|
|
|
Key: "foofoobarbar",
|
|
|
|
Value: []byte("something"),
|
|
|
|
}, store.WriteTTL(time.Millisecond*100)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err := s.Write(&store.Record{
|
|
|
|
Key: "foofoo",
|
|
|
|
Value: []byte("something"),
|
|
|
|
}, store.WriteExpiry(time.Now().Add(time.Millisecond*100))); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err := s.Write(&store.Record{
|
|
|
|
Key: "barbar",
|
|
|
|
Value: []byte("something"),
|
|
|
|
// TTL has higher precedence than expiry
|
|
|
|
}, store.WriteExpiry(time.Now().Add(time.Hour)), store.WriteTTL(time.Millisecond*100)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if results, err := s.Read("foo", store.ReadPrefix(), store.ReadSuffix()); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 1 {
|
|
|
|
t.Errorf("Expected 1 results, got %d: %# v", len(results), pretty.Formatter(results))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
if results, err := s.List(); err != nil {
|
|
|
|
t.Errorf("List failed: %s", err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 0 {
|
|
|
|
t.Error("Expiry options were not effective")
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 16:15:23 +00:00
|
|
|
s.Write(&store.Record{Key: "a", Value: []byte("a")})
|
|
|
|
s.Write(&store.Record{Key: "aa", Value: []byte("aa")})
|
|
|
|
s.Write(&store.Record{Key: "aaa", Value: []byte("aaa")})
|
|
|
|
if results, err := s.Read("b", store.ReadPrefix()); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 0 {
|
|
|
|
t.Errorf("Expected 0 results, got %d", len(results))
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 12:51:55 +08:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
s.Init()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
s.Write(&store.Record{
|
|
|
|
Key: fmt.Sprintf("a%d", i),
|
|
|
|
Value: []byte{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if results, err := s.Read("a", store.ReadLimit(5), store.ReadPrefix()); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 5 {
|
|
|
|
t.Error("Expected 5 results, got ", len(results))
|
|
|
|
}
|
|
|
|
if results[0].Key != "a0" {
|
|
|
|
t.Errorf("Expected a0, got %s", results[0].Key)
|
|
|
|
}
|
|
|
|
if results[4].Key != "a4" {
|
|
|
|
t.Errorf("Expected a4, got %s", results[4].Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if results, err := s.Read("a", store.ReadLimit(30), store.ReadOffset(5), store.ReadPrefix()); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
if len(results) != 5 {
|
|
|
|
t.Error("Expected 5 results, got ", len(results))
|
|
|
|
}
|
2019-07-11 12:51:55 +08:00
|
|
|
}
|
|
|
|
}
|