micro/store/file/file_test.go

268 lines
6.9 KiB
Go
Raw Normal View History

2020-04-07 13:53:22 +02:00
package file
import (
"fmt"
2020-04-07 19:45:27 +01:00
"os"
"path/filepath"
"strings"
2020-04-07 13:53:22 +02:00
"testing"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/kr/pretty"
"github.com/micro/go-micro/v2/store"
)
func cleanup(db string, s store.Store) {
s.Close()
2020-04-08 09:57:51 +01:00
dir := filepath.Join(DefaultDir, db+"/")
2020-04-07 19:45:27 +01:00
os.RemoveAll(dir)
}
func TestFileStoreReInit(t *testing.T) {
2020-04-07 13:53:22 +02:00
s := NewStore(store.Table("aaa"))
defer cleanup(DefaultDatabase, s)
2020-04-07 13:53:22 +02:00
s.Init(store.Table("bbb"))
if s.Options().Table != "bbb" {
t.Error("Init didn't reinitialise the store")
}
}
2020-04-07 19:45:27 +01:00
func TestFileStoreBasic(t *testing.T) {
2020-04-07 13:53:22 +02:00
s := NewStore()
defer cleanup(DefaultDatabase, s)
2020-04-07 19:45:27 +01:00
fileTest(s, t)
2020-04-07 13:53:22 +02:00
}
2020-04-07 19:45:27 +01:00
func TestFileStoreTable(t *testing.T) {
s := NewStore(store.Table("testTable"))
defer cleanup(DefaultDatabase, s)
2020-04-07 19:45:27 +01:00
fileTest(s, t)
2020-04-07 13:53:22 +02:00
}
2020-04-07 19:45:27 +01:00
func TestFileStoreDatabase(t *testing.T) {
s := NewStore(store.Database("testdb"))
defer cleanup("testdb", s)
2020-04-07 19:45:27 +01:00
fileTest(s, t)
2020-04-07 13:53:22 +02:00
}
2020-04-07 19:45:27 +01:00
func TestFileStoreDatabaseTable(t *testing.T) {
s := NewStore(store.Table("testTable"), store.Database("testdb"))
defer cleanup("testdb", s)
2020-04-07 19:45:27 +01:00
fileTest(s, t)
2020-04-07 13:53:22 +02:00
}
2020-04-07 19:45:27 +01:00
func fileTest(s store.Store, t *testing.T) {
t.Logf("Options %s %v\n", s.String(), s.Options())
2020-04-07 13:53:22 +02:00
// Read and Write an expiring Record
if err := s.Write(&store.Record{
Key: "Hello",
Value: []byte("World"),
2020-04-07 19:45:27 +01:00
Expiry: time.Millisecond * 150,
2020-04-07 13:53:22 +02:00
}); err != nil {
t.Error(err)
}
2020-04-07 19:45:27 +01:00
2020-04-07 13:53:22 +02:00
if r, err := s.Read("Hello"); err != nil {
2020-04-07 19:45:27 +01:00
t.Fatal(err)
2020-04-07 13:53:22 +02:00
} 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)
}
}
2020-04-07 19:45:27 +01:00
// wait for expiry
2020-04-07 13:53:22 +02:00
time.Sleep(time.Millisecond * 200)
2020-04-07 19:45:27 +01:00
2020-04-07 13:53:22 +02:00
if _, err := s.Read("Hello"); err != store.ErrNotFound {
t.Errorf("Expected %# v, got %# v", store.ErrNotFound, err)
}
// Write 3 records with various expiry and get with Table
records := []*store.Record{
&store.Record{
Key: "foo",
Value: []byte("foofoo"),
},
&store.Record{
Key: "foobar",
Value: []byte("foobarfoobar"),
Expiry: time.Millisecond * 100,
},
}
2020-04-07 19:45:27 +01:00
2020-04-07 13:53:22 +02: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)
}
}
2020-04-07 19:45:27 +01:00
2020-04-07 13:53:22 +02:00
if results, err := s.Read("foo", store.ReadPrefix()); err != nil {
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
} else {
if len(results) != 2 {
t.Errorf("Expected 2 items, got %d", len(results))
t.Logf("Table test: %v\n", spew.Sdump(results))
}
}
2020-04-07 19:45:27 +01:00
// wait for the expiry
time.Sleep(time.Millisecond * 200)
2020-04-07 13:53:22 +02:00
if results, err := s.Read("foo", store.ReadPrefix()); err != nil {
t.Errorf("Couldn't read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
2020-04-07 19:45:27 +01:00
} else if len(results) != 1 {
t.Errorf("Expected 1 item, got %d", len(results))
t.Logf("Table test: %v\n", spew.Sdump(results))
2020-04-07 13:53:22 +02:00
}
2020-04-07 19:45:27 +01:00
if err := s.Delete("foo"); err != nil {
2020-04-07 13:53:22 +02:00
t.Errorf("Delete failed (%v)", err)
}
2020-04-07 19:45:27 +01:00
if results, err := s.Read("foo"); err != store.ErrNotFound {
t.Errorf("Expected read failure read all \"foo\" keys, got %# v (%s)", spew.Sdump(results), err)
2020-04-07 13:53:22 +02:00
} else {
if len(results) != 0 {
t.Errorf("Expected 0 items, got %d (%# v)", len(results), spew.Sdump(results))
}
}
// Write 3 records with various expiry and get with Suffix
records = []*store.Record{
&store.Record{
Key: "foo",
Value: []byte("foofoo"),
},
&store.Record{
2020-04-07 19:45:27 +01:00
Key: "barfoo",
Value: []byte("barfoobarfoo"),
2020-04-07 13:53:22 +02:00
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)", spew.Sdump(results), err)
} else {
if len(results) != 3 {
t.Errorf("Expected 3 items, got %d", len(results))
t.Logf("Table test: %v\n", spew.Sdump(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)", spew.Sdump(results), err)
} else {
if len(results) != 2 {
t.Errorf("Expected 2 items, got %d", len(results))
t.Logf("Table test: %v\n", spew.Sdump(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)", spew.Sdump(results), err)
} else {
if len(results) != 1 {
t.Errorf("Expected 1 item, got %d", len(results))
t.Logf("Table test: %# v\n", spew.Sdump(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)", spew.Sdump(results), err)
} else {
if len(results) != 0 {
t.Errorf("Expected 0 items, got %d (%# v)", len(results), spew.Sdump(results))
}
}
// Test Table, 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)
}
2020-04-07 19:45:27 +01:00
2020-04-07 13:53:22 +02:00
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), spew.Sdump(results))
}
}
2020-04-07 19:45:27 +01:00
2020-04-07 13:53:22 +02:00
time.Sleep(time.Millisecond * 100)
2020-04-07 19:45:27 +01:00
2020-04-07 13:53:22 +02:00
if results, err := s.List(); err != nil {
t.Errorf("List failed: %s", err)
} else {
if len(results) != 0 {
t.Errorf("Expiry options were not effective, results :%v", spew.Sdump(results))
}
}
2020-04-07 19:45:27 +01:00
// write the following records
2020-04-07 13:53:22 +02:00
for i := 0; i < 10; i++ {
s.Write(&store.Record{
Key: fmt.Sprintf("a%d", i),
Value: []byte{},
})
}
2020-04-07 19:45:27 +01:00
// read back a few records
2020-04-07 13:53:22 +02:00
if results, err := s.Read("a", store.ReadLimit(5), store.ReadPrefix()); err != nil {
t.Error(err)
} else {
if len(results) != 5 {
2020-04-08 09:57:51 +01:00
t.Fatal("Expected 5 results, got ", len(results))
2020-04-07 13:53:22 +02:00
}
2020-04-07 19:45:27 +01:00
if !strings.HasPrefix(results[0].Key, "a") {
2020-04-08 09:57:51 +01:00
t.Fatalf("Expected a prefix, got %s", results[0].Key)
2020-04-07 13:53:22 +02:00
}
}
2020-04-07 19:45:27 +01:00
// read the rest back
2020-04-07 13:53:22 +02:00
if results, err := s.Read("a", store.ReadLimit(30), store.ReadOffset(5), store.ReadPrefix()); err != nil {
2020-04-08 09:57:51 +01:00
t.Fatal(err)
2020-04-07 13:53:22 +02:00
} else {
if len(results) != 5 {
2020-04-08 09:57:51 +01:00
t.Fatal("Expected 5 results, got ", len(results))
2020-04-07 13:53:22 +02:00
}
}
}