summaryrefslogtreecommitdiff
path: root/plugins/kv/memory
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-01-07 01:06:50 +0300
committerValery Piashchynski <[email protected]>2021-01-07 01:06:50 +0300
commitc1465d3bcdf24a78440300aa51e7cfc92ce874a8 (patch)
tree6e0f5107eba90df73724b6611ca6adfa148d2a3f /plugins/kv/memory
parentc9f670ee734355cbc5d504186946b7db67cf62b5 (diff)
KV, updated, bug fixed, with intergration tests via plugins
Diffstat (limited to 'plugins/kv/memory')
-rw-r--r--plugins/kv/memory/plugin.go (renamed from plugins/kv/memory/storage.go)31
-rw-r--r--plugins/kv/memory/storage_test.go114
2 files changed, 71 insertions, 74 deletions
diff --git a/plugins/kv/memory/storage.go b/plugins/kv/memory/plugin.go
index f4bdacea..2c65f14c 100644
--- a/plugins/kv/memory/storage.go
+++ b/plugins/kv/memory/plugin.go
@@ -1,7 +1,6 @@
package memory
import (
- "context"
"strings"
"sync"
"time"
@@ -49,7 +48,7 @@ func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
return nil
}
-func (s Plugin) Serve() chan error {
+func (s *Plugin) Serve() chan error {
errCh := make(chan error, 1)
// start in-memory gc for kv
go s.gc()
@@ -57,7 +56,7 @@ func (s Plugin) Serve() chan error {
return errCh
}
-func (s Plugin) Stop() error {
+func (s *Plugin) Stop() error {
const op = errors.Op("in-memory storage stop")
err := s.Close()
if err != nil {
@@ -66,7 +65,7 @@ func (s Plugin) Stop() error {
return nil
}
-func (s Plugin) Has(ctx context.Context, keys ...string) (map[string]bool, error) {
+func (s *Plugin) Has(keys ...string) (map[string]bool, error) {
const op = errors.Op("in-memory storage Has")
if keys == nil {
return nil, errors.E(op, errors.NoKeys)
@@ -86,7 +85,7 @@ func (s Plugin) Has(ctx context.Context, keys ...string) (map[string]bool, error
return m, nil
}
-func (s Plugin) Get(ctx context.Context, key string) ([]byte, error) {
+func (s *Plugin) Get(key string) ([]byte, error) {
const op = errors.Op("in-memory storage Get")
// to get cases like " "
keyTrimmed := strings.TrimSpace(key)
@@ -102,7 +101,7 @@ func (s Plugin) Get(ctx context.Context, key string) ([]byte, error) {
return nil, nil
}
-func (s Plugin) MGet(ctx context.Context, keys ...string) (map[string]interface{}, error) {
+func (s *Plugin) MGet(keys ...string) (map[string]interface{}, error) {
const op = errors.Op("in-memory storage MGet")
if keys == nil {
return nil, errors.E(op, errors.NoKeys)
@@ -127,7 +126,7 @@ func (s Plugin) MGet(ctx context.Context, keys ...string) (map[string]interface{
return m, nil
}
-func (s Plugin) Set(ctx context.Context, items ...kv.Item) error {
+func (s *Plugin) Set(items ...kv.Item) error {
const op = errors.Op("in-memory storage Set")
if items == nil {
return errors.E(op, errors.NoKeys)
@@ -150,7 +149,7 @@ func (s Plugin) Set(ctx context.Context, items ...kv.Item) error {
// MExpire sets the expiration time to the key
// If key already has the expiration time, it will be overwritten
-func (s Plugin) MExpire(ctx context.Context, items ...kv.Item) error {
+func (s *Plugin) MExpire(items ...kv.Item) error {
const op = errors.Op("in-memory storage MExpire")
for i := range items {
if items[i].TTL == "" || strings.TrimSpace(items[i].Key) == "" {
@@ -179,7 +178,7 @@ func (s Plugin) MExpire(ctx context.Context, items ...kv.Item) error {
return nil
}
-func (s Plugin) TTL(ctx context.Context, keys ...string) (map[string]interface{}, error) {
+func (s *Plugin) TTL(keys ...string) (map[string]interface{}, error) {
const op = errors.Op("in-memory storage TTL")
if keys == nil {
return nil, errors.E(op, errors.NoKeys)
@@ -203,7 +202,7 @@ func (s Plugin) TTL(ctx context.Context, keys ...string) (map[string]interface{}
return m, nil
}
-func (s Plugin) Delete(ctx context.Context, keys ...string) error {
+func (s *Plugin) Delete(keys ...string) error {
const op = errors.Op("in-memory storage Delete")
if keys == nil {
return errors.E(op, errors.NoKeys)
@@ -224,11 +223,21 @@ func (s Plugin) Delete(ctx context.Context, keys ...string) error {
}
// Close clears the in-memory storage
-func (s Plugin) Close() error {
+func (s *Plugin) Close() error {
s.stop <- struct{}{}
return nil
}
+// RPCService returns associated rpc service.
+func (s *Plugin) RPC() interface{} {
+ return kv.NewRPCServer(s, s.log)
+}
+
+// Name returns plugin user-friendly name
+func (s *Plugin) Name() string {
+ return PluginName
+}
+
// ================================== PRIVATE ======================================
func (s *Plugin) gc() {
diff --git a/plugins/kv/memory/storage_test.go b/plugins/kv/memory/storage_test.go
index b7b46637..4b30460d 100644
--- a/plugins/kv/memory/storage_test.go
+++ b/plugins/kv/memory/storage_test.go
@@ -1,7 +1,6 @@
package memory
import (
- "context"
"strconv"
"sync"
"testing"
@@ -16,7 +15,7 @@ func initStorage() kv.Storage {
}
func cleanup(t *testing.T, s kv.Storage, keys ...string) {
- err := s.Delete(context.Background(), keys...)
+ err := s.Delete(keys...)
if err != nil {
t.Fatalf("error during cleanup: %s", err.Error())
}
@@ -25,9 +24,7 @@ func cleanup(t *testing.T, s kv.Storage, keys ...string) {
func TestStorage_Has(t *testing.T) {
s := initStorage()
- ctx := context.Background()
-
- v, err := s.Has(ctx, "key")
+ v, err := s.Has("key")
assert.NoError(t, err)
assert.False(t, v["key"])
}
@@ -41,13 +38,12 @@ func TestStorage_Has_Set_Has(t *testing.T) {
}
}()
- ctx := context.Background()
- v, err := s.Has(ctx, "key")
+ v, err := s.Has("key")
assert.NoError(t, err)
// no such key
assert.False(t, v["key"])
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "value",
TTL: "",
@@ -58,7 +54,7 @@ func TestStorage_Has_Set_Has(t *testing.T) {
TTL: "",
}))
- v, err = s.Has(ctx, "key", "key2")
+ v, err = s.Has("key", "key2")
assert.NoError(t, err)
// no such key
assert.True(t, v["key"])
@@ -74,13 +70,12 @@ func TestStorage_Has_Set_MGet(t *testing.T) {
}
}()
- ctx := context.Background()
- v, err := s.Has(ctx, "key")
+ v, err := s.Has("key")
assert.NoError(t, err)
// no such key
assert.False(t, v["key"])
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "value",
TTL: "",
@@ -91,13 +86,13 @@ func TestStorage_Has_Set_MGet(t *testing.T) {
TTL: "",
}))
- v, err = s.Has(ctx, "key", "key2")
+ v, err = s.Has("key", "key2")
assert.NoError(t, err)
// no such key
assert.True(t, v["key"])
assert.True(t, v["key2"])
- res, err := s.MGet(ctx, "key", "key2")
+ res, err := s.MGet("key", "key2")
assert.NoError(t, err)
assert.Len(t, res, 2)
}
@@ -111,13 +106,12 @@ func TestStorage_Has_Set_Get(t *testing.T) {
}
}()
- ctx := context.Background()
- v, err := s.Has(ctx, "key")
+ v, err := s.Has("key")
assert.NoError(t, err)
// no such key
assert.False(t, v["key"])
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "value",
TTL: "",
@@ -128,13 +122,13 @@ func TestStorage_Has_Set_Get(t *testing.T) {
TTL: "",
}))
- v, err = s.Has(ctx, "key", "key2")
+ v, err = s.Has("key", "key2")
assert.NoError(t, err)
// no such key
assert.True(t, v["key"])
assert.True(t, v["key2"])
- res, err := s.Get(ctx, "key")
+ res, err := s.Get("key")
assert.NoError(t, err)
if string(res) != "value" {
@@ -151,13 +145,12 @@ func TestStorage_Set_Del_Get(t *testing.T) {
}
}()
- ctx := context.Background()
- v, err := s.Has(ctx, "key")
+ v, err := s.Has("key")
assert.NoError(t, err)
// no such key
assert.False(t, v["key"])
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "value",
TTL: "",
@@ -168,27 +161,26 @@ func TestStorage_Set_Del_Get(t *testing.T) {
TTL: "",
}))
- v, err = s.Has(ctx, "key", "key2")
+ v, err = s.Has("key", "key2")
assert.NoError(t, err)
// no such key
assert.True(t, v["key"])
assert.True(t, v["key2"])
// check that keys are present
- res, err := s.MGet(ctx, "key", "key2")
+ res, err := s.MGet("key", "key2")
assert.NoError(t, err)
assert.Len(t, res, 2)
- assert.NoError(t, s.Delete(ctx, "key", "key2"))
- // check that keys are not presentps -eo state,uid,pid,ppid,rtprio,time,comm
- res, err = s.MGet(ctx, "key", "key2")
+ assert.NoError(t, s.Delete("key", "key2"))
+ // check that keys are not presents -eo state,uid,pid,ppid,rtprio,time,comm
+ res, err = s.MGet("key", "key2")
assert.NoError(t, err)
assert.Len(t, res, 0)
}
func TestStorage_Set_GetM(t *testing.T) {
s := initStorage()
- ctx := context.Background()
defer func() {
cleanup(t, s, "key", "key2")
@@ -198,11 +190,11 @@ func TestStorage_Set_GetM(t *testing.T) {
}
}()
- v, err := s.Has(ctx, "key")
+ v, err := s.Has("key")
assert.NoError(t, err)
assert.False(t, v["key"])
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "value",
TTL: "",
@@ -213,14 +205,13 @@ func TestStorage_Set_GetM(t *testing.T) {
TTL: "",
}))
- res, err := s.MGet(ctx, "key", "key2")
+ res, err := s.MGet("key", "key2")
assert.NoError(t, err)
assert.Len(t, res, 2)
}
func TestStorage_MExpire_TTL(t *testing.T) {
s := initStorage()
- ctx := context.Background()
defer func() {
cleanup(t, s, "key", "key2")
@@ -230,12 +221,12 @@ func TestStorage_MExpire_TTL(t *testing.T) {
}()
// ensure that storage is clean
- v, err := s.Has(ctx, "key", "key2")
+ v, err := s.Has("key", "key2")
assert.NoError(t, err)
assert.False(t, v["key"])
assert.False(t, v["key2"])
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "hello world",
TTL: "",
@@ -258,12 +249,12 @@ func TestStorage_MExpire_TTL(t *testing.T) {
Value: "",
TTL: nowPlusFive,
}
- assert.NoError(t, s.MExpire(ctx, i1, i2))
+ assert.NoError(t, s.MExpire(i1, i2))
time.Sleep(time.Second * 6)
// ensure that storage is clean
- v, err = s.Has(ctx, "key", "key2")
+ v, err = s.Has("key", "key2")
assert.NoError(t, err)
assert.False(t, v["key"])
assert.False(t, v["key2"])
@@ -271,7 +262,6 @@ func TestStorage_MExpire_TTL(t *testing.T) {
func TestNilAndWrongArgs(t *testing.T) {
s := initStorage()
- ctx := context.Background()
defer func() {
if err := s.Close(); err != nil {
panic(err)
@@ -279,48 +269,47 @@ func TestNilAndWrongArgs(t *testing.T) {
}()
// check
- v, err := s.Has(ctx, "key")
+ v, err := s.Has("key")
assert.NoError(t, err)
assert.False(t, v["key"])
- _, err = s.Has(ctx, "")
+ _, err = s.Has("")
assert.Error(t, err)
- _, err = s.Get(ctx, "")
+ _, err = s.Get("")
assert.Error(t, err)
- _, err = s.Get(ctx, " ")
+ _, err = s.Get(" ")
assert.Error(t, err)
- _, err = s.Get(ctx, " ")
+ _, err = s.Get(" ")
assert.Error(t, err)
- _, err = s.MGet(ctx, "key", "key2", "")
+ _, err = s.MGet("key", "key2", "")
assert.Error(t, err)
- _, err = s.MGet(ctx, "key", "key2", " ")
+ _, err = s.MGet("key", "key2", " ")
assert.Error(t, err)
- assert.NoError(t, s.Set(ctx, kv.Item{}))
- _, err = s.Has(ctx, "key")
+ assert.NoError(t, s.Set(kv.Item{}))
+ _, err = s.Has("key")
assert.NoError(t, err)
- err = s.Delete(ctx, "")
+ err = s.Delete("")
assert.Error(t, err)
- err = s.Delete(ctx, "key", "")
+ err = s.Delete("key", "")
assert.Error(t, err)
- err = s.Delete(ctx, "key", " ")
+ err = s.Delete("key", " ")
assert.Error(t, err)
- err = s.Delete(ctx, "key")
+ err = s.Delete("key")
assert.NoError(t, err)
}
func TestStorage_SetExpire_TTL(t *testing.T) {
s := initStorage()
- ctx := context.Background()
defer func() {
cleanup(t, s, "key", "key2")
if err := s.Close(); err != nil {
@@ -329,12 +318,12 @@ func TestStorage_SetExpire_TTL(t *testing.T) {
}()
// ensure that storage is clean
- v, err := s.Has(ctx, "key", "key2")
+ v, err := s.Has("key", "key2")
assert.NoError(t, err)
assert.False(t, v["key"])
assert.False(t, v["key2"])
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "hello world",
TTL: "",
@@ -348,7 +337,7 @@ func TestStorage_SetExpire_TTL(t *testing.T) {
nowPlusFive := time.Now().Add(time.Second * 5).Format(time.RFC3339)
// set timeout to 5 sec
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "value",
TTL: nowPlusFive,
@@ -360,7 +349,7 @@ func TestStorage_SetExpire_TTL(t *testing.T) {
}))
time.Sleep(time.Second * 2)
- m, err := s.TTL(ctx, "key", "key2")
+ m, err := s.TTL("key", "key2")
assert.NoError(t, err)
// remove a precision 4.02342342 -> 4
@@ -381,7 +370,7 @@ func TestStorage_SetExpire_TTL(t *testing.T) {
time.Sleep(time.Second * 4)
// ensure that storage is clean
- v, err = s.Has(ctx, "key", "key2")
+ v, err = s.Has("key", "key2")
assert.NoError(t, err)
assert.False(t, v["key"])
assert.False(t, v["key2"])
@@ -396,13 +385,12 @@ func TestConcurrentReadWriteTransactions(t *testing.T) {
}
}()
- ctx := context.Background()
- v, err := s.Has(ctx, "key")
+ v, err := s.Has("key")
assert.NoError(t, err)
// no such key
assert.False(t, v["key"])
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key",
Value: "hello world",
TTL: "",
@@ -412,7 +400,7 @@ func TestConcurrentReadWriteTransactions(t *testing.T) {
TTL: "",
}))
- v, err = s.Has(ctx, "key", "key2")
+ v, err = s.Has("key", "key2")
assert.NoError(t, err)
// no such key
assert.True(t, v["key"])
@@ -429,7 +417,7 @@ func TestConcurrentReadWriteTransactions(t *testing.T) {
m.Lock()
// set is writable transaction
// it should stop readable
- assert.NoError(t, s.Set(ctx, kv.Item{
+ assert.NoError(t, s.Set(kv.Item{
Key: "key" + strconv.Itoa(i),
Value: "hello world" + strconv.Itoa(i),
TTL: "",
@@ -447,7 +435,7 @@ func TestConcurrentReadWriteTransactions(t *testing.T) {
defer wg.Done()
for i := 0; i <= 1000; i++ {
m.RLock()
- v, err = s.Has(ctx, "key")
+ v, err = s.Has("key")
assert.NoError(t, err)
// no such key
assert.True(t, v["key"])
@@ -460,7 +448,7 @@ func TestConcurrentReadWriteTransactions(t *testing.T) {
defer wg.Done()
for i := 0; i <= 1000; i++ {
m.Lock()
- err = s.Delete(ctx, "key"+strconv.Itoa(i))
+ err = s.Delete("key" + strconv.Itoa(i))
assert.NoError(t, err)
m.Unlock()
}