2020-07-06 07:59:32 -05:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2021-01-16 03:00:25 +03:00
|
|
|
"context"
|
2020-07-06 07:59:32 -05:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-01-16 03:00:25 +03:00
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
"github.com/unistack-org/micro/v3/store"
|
2020-07-06 07:59:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_rkv_configure(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
options store.Options
|
|
|
|
Client *redis.Client
|
|
|
|
}
|
|
|
|
type wantValues struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
wantErr bool
|
|
|
|
want wantValues
|
|
|
|
}{
|
|
|
|
{name: "No Url", fields: fields{options: store.Options{}, Client: nil},
|
|
|
|
wantErr: false, want: wantValues{
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
address: "127.0.0.1:6379",
|
|
|
|
}},
|
|
|
|
{name: "legacy Url", fields: fields{options: store.Options{Nodes: []string{"127.0.0.1:6379"}}, Client: nil},
|
|
|
|
wantErr: false, want: wantValues{
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
address: "127.0.0.1:6379",
|
|
|
|
}},
|
|
|
|
{name: "New Url", fields: fields{options: store.Options{Nodes: []string{"redis://127.0.0.1:6379"}}, Client: nil},
|
|
|
|
wantErr: false, want: wantValues{
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
address: "127.0.0.1:6379",
|
|
|
|
}},
|
|
|
|
{name: "Url with Pwd", fields: fields{options: store.Options{Nodes: []string{"redis://:password@redis:6379"}}, Client: nil},
|
|
|
|
wantErr: false, want: wantValues{
|
|
|
|
username: "",
|
|
|
|
password: "password",
|
|
|
|
address: "redis:6379",
|
|
|
|
}},
|
|
|
|
{name: "Url with username and Pwd", fields: fields{options: store.Options{Nodes: []string{"redis://username:password@redis:6379"}}, Client: nil},
|
|
|
|
wantErr: false, want: wantValues{
|
|
|
|
username: "username",
|
|
|
|
password: "password",
|
|
|
|
address: "redis:6379",
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &rkv{
|
2021-01-16 03:00:25 +03:00
|
|
|
opts: tt.fields.options,
|
|
|
|
cli: tt.fields.Client,
|
2020-07-06 07:59:32 -05:00
|
|
|
}
|
|
|
|
err := r.configure()
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("configure() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Store(t *testing.T) {
|
2021-01-16 03:00:25 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
2020-07-06 07:59:32 -05:00
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
r := new(rkv)
|
|
|
|
|
|
|
|
//r.options = store.Options{Nodes: []string{"redis://:password@127.0.0.1:6379"}}
|
|
|
|
//r.options = store.Options{Nodes: []string{"127.0.0.1:6379"}}
|
2021-01-16 03:00:25 +03:00
|
|
|
|
|
|
|
r.opts = store.NewOptions(store.Nodes(os.Getenv("STORE_NODES")))
|
2020-07-06 07:59:32 -05:00
|
|
|
|
|
|
|
if err := r.configure(); err != nil {
|
2021-01-16 03:00:25 +03:00
|
|
|
t.Fatal(err)
|
2020-07-06 07:59:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
key := "myTest"
|
2021-01-16 03:00:25 +03:00
|
|
|
var val []byte
|
|
|
|
err := r.Write(ctx, key, []byte("myValue"), store.WriteTTL(2*time.Minute))
|
2020-07-06 07:59:32 -05:00
|
|
|
if err != nil {
|
2021-01-16 03:00:25 +03:00
|
|
|
t.Fatalf("Write error: %v", err)
|
2020-07-06 07:59:32 -05:00
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
err = r.Read(ctx, key, val)
|
2020-07-06 07:59:32 -05:00
|
|
|
if err != nil {
|
2021-01-16 03:00:25 +03:00
|
|
|
t.Fatalf("Read error: %v\n", err)
|
2020-07-06 07:59:32 -05:00
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
err = r.Delete(ctx, key)
|
2020-07-06 07:59:32 -05:00
|
|
|
if err != nil {
|
2021-01-16 03:00:25 +03:00
|
|
|
t.Fatalf("Delete error: %v\n", err)
|
2020-07-06 07:59:32 -05:00
|
|
|
}
|
2021-01-16 03:00:25 +03:00
|
|
|
_, err = r.List(ctx)
|
2020-07-06 07:59:32 -05:00
|
|
|
if err != nil {
|
2021-01-16 03:00:25 +03:00
|
|
|
t.Fatalf("List error: %v\n", err)
|
2020-07-06 07:59:32 -05:00
|
|
|
}
|
|
|
|
}
|