2020-07-06 07:59:32 -05:00
package redis
import (
2022-10-26 13:46:48 +03:00
"bytes"
2021-01-16 03:00:25 +03:00
"context"
2020-07-06 07:59:32 -05:00
"os"
2024-11-27 01:57:39 +03:00
"sync/atomic"
2020-07-06 07:59:32 -05:00
"testing"
"time"
2021-01-16 03:00:25 +03:00
2024-10-05 16:11:46 +03:00
goredis "github.com/redis/go-redis/v9"
2021-10-27 23:29:16 +03:00
"go.unistack.org/micro/v3/store"
2024-07-04 15:13:19 +03:00
"go.unistack.org/micro/v3/tracer"
2020-07-06 07:59:32 -05:00
)
2024-11-22 16:06:59 +03:00
func TestLazyConnect ( t * testing . T ) {
ctx := context . Background ( )
var err error
r := NewStore ( )
if err = r . Init ( ) ; err != nil {
t . Fatal ( err )
}
if err = r . Connect ( ctx ) ; err != nil {
t . Logf ( "connect failed %v" , err )
}
for {
if err = r . Write ( ctx , "mykey" , "myval" ) ; err != nil {
t . Logf ( "failed to write %v" , err )
}
}
}
2024-07-04 15:13:19 +03:00
func TestKeepTTL ( t * testing . T ) {
ctx := context . Background ( )
if tr := os . Getenv ( "INTEGRATION_TESTS" ) ; len ( tr ) > 0 {
t . Skip ( )
}
r := NewStore ( store . Addrs ( os . Getenv ( "STORE_NODES" ) ) )
2024-11-27 01:57:39 +03:00
if err := r . Init ( store . LazyConnect ( true ) ) ; err != nil {
2024-07-04 15:13:19 +03:00
t . Fatal ( err )
}
if err := r . Connect ( ctx ) ; err != nil {
t . Fatal ( err )
}
key := "key"
err := r . Write ( ctx , key , "val1" , store . WriteTTL ( 15 * time . Second ) )
if err != nil {
t . Fatalf ( "Write error: %v" , err )
}
time . Sleep ( 3 * time . Second )
err = r . Write ( ctx , key , "val2" , store . WriteTTL ( - 1 ) )
if err != nil {
t . Fatalf ( "Write error: %v" , err )
}
}
2020-07-06 07:59:32 -05:00
func Test_rkv_configure ( t * testing . T ) {
type fields struct {
options store . Options
2024-10-05 16:11:46 +03:00
Client goredis . UniversalClient
2020-07-06 07:59:32 -05:00
}
type wantValues struct {
username string
password string
address string
}
tests := [ ] struct {
name string
fields fields
wantErr bool
want wantValues
} {
2021-11-24 15:23:47 +03:00
{
name : "No Url" , fields : fields { options : store . Options { } , Client : nil } ,
2020-07-06 07:59:32 -05:00
wantErr : false , want : wantValues {
username : "" ,
password : "" ,
address : "127.0.0.1:6379" ,
2021-11-24 15:23:47 +03:00
} ,
} ,
{
2024-07-04 15:13:19 +03:00
name : "legacy Url" , fields : fields { options : store . Options { Tracer : tracer . DefaultTracer , Addrs : [ ] string { "127.0.0.1:6379" } } , Client : nil } ,
2020-07-06 07:59:32 -05:00
wantErr : false , want : wantValues {
username : "" ,
password : "" ,
address : "127.0.0.1:6379" ,
2021-11-24 15:23:47 +03:00
} ,
} ,
{
2024-07-04 15:13:19 +03:00
name : "New Url" , fields : fields { options : store . Options { Tracer : tracer . DefaultTracer , Addrs : [ ] string { "redis://127.0.0.1:6379" } } , Client : nil } ,
2020-07-06 07:59:32 -05:00
wantErr : false , want : wantValues {
username : "" ,
password : "" ,
address : "127.0.0.1:6379" ,
2021-11-24 15:23:47 +03:00
} ,
} ,
{
2024-07-04 15:13:19 +03:00
name : "Url with Pwd" , fields : fields { options : store . Options { Tracer : tracer . DefaultTracer , Addrs : [ ] string { "redis://:password@redis:6379" } } , Client : nil } ,
2020-07-06 07:59:32 -05:00
wantErr : false , want : wantValues {
username : "" ,
password : "password" ,
address : "redis:6379" ,
2021-11-24 15:23:47 +03:00
} ,
} ,
{
2024-07-04 15:13:19 +03:00
name : "Url with username and Pwd" , fields : fields { options : store . Options { Tracer : tracer . DefaultTracer , Addrs : [ ] string { "redis://username:password@redis:6379" } } , Client : nil } ,
2020-07-06 07:59:32 -05:00
wantErr : false , want : wantValues {
username : "username" ,
password : "password" ,
address : "redis:6379" ,
2021-11-24 15:23:47 +03:00
} ,
} ,
2020-07-06 07:59:32 -05:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2024-11-27 01:57:39 +03:00
b := atomic . Bool { }
2023-12-12 13:48:47 +03:00
rc := & Store {
2024-11-27 01:57:39 +03:00
opts : tt . fields . options ,
cli : tt . fields . Client ,
connected : & b ,
2020-07-06 07:59:32 -05:00
}
2023-12-12 13:48:47 +03:00
err := rc . configure ( )
2020-07-06 07:59:32 -05:00
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 ( )
}
2022-10-26 13:46:48 +03:00
r := NewStore ( store . Addrs ( os . Getenv ( "STORE_NODES" ) ) )
2020-07-06 07:59:32 -05:00
2022-10-26 13:46:48 +03:00
if err := r . Init ( ) ; err != nil {
t . Fatal ( err )
}
if err := r . Connect ( ctx ) ; err != nil {
2021-01-16 03:00:25 +03:00
t . Fatal ( err )
2020-07-06 07:59:32 -05:00
}
key := "myTest"
2022-10-26 13:46:48 +03:00
tval := [ ] byte ( "myValue" )
2021-01-16 03:00:25 +03:00
var val [ ] byte
2022-10-26 13:46:48 +03:00
err := r . Write ( ctx , key , tval , 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
}
2022-10-26 13:46:48 +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 )
2022-10-26 13:46:48 +03:00
} else if ! bytes . Equal ( val , tval ) {
t . Fatalf ( "read err: data not eq" )
2020-07-06 07:59:32 -05:00
}
2022-10-26 13:46:48 +03:00
keys , err := r . List ( ctx )
if err != nil {
t . Fatalf ( "List error: %v\n" , err )
}
_ = keys
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
}
2022-10-26 13:46:48 +03:00
// t.Logf("%v", keys)
}
func Test_MRead ( t * testing . T ) {
ctx := context . Background ( )
var err error
if tr := os . Getenv ( "INTEGRATION_TESTS" ) ; len ( tr ) > 0 {
t . Skip ( )
}
r := NewStore ( store . Addrs ( os . Getenv ( "STORE_NODES" ) ) )
if err = r . Init ( ) ; err != nil {
t . Fatal ( err )
}
if err = r . Connect ( ctx ) ; err != nil {
t . Fatal ( err )
}
key1 := "myTest1"
key2 := "myTest2"
tval1 := [ ] byte ( "myValue1" )
tval2 := [ ] byte ( "myValue2" )
var vals [ ] [ ] byte
err = r . Write ( ctx , key1 , tval1 , store . WriteTTL ( 2 * time . Minute ) )
if err != nil {
t . Fatalf ( "Write error: %v" , err )
}
err = r . Write ( ctx , key2 , tval2 , store . WriteTTL ( 2 * time . Minute ) )
if err != nil {
t . Fatalf ( "Write error: %v" , err )
}
err = r . MRead ( ctx , [ ] string { key1 , key2 } , & vals )
if err != nil {
t . Fatalf ( "Read error: %v\n" , err )
}
// t.Logf("%s", vals)
_ = vals
keys , err := r . List ( ctx )
if err != nil {
t . Fatalf ( "List error: %v\n" , err )
}
_ = keys
// t.Logf("%v", keys)
err = r . Delete ( ctx , key1 )
if err != nil {
t . Fatalf ( "Delete error: %v\n" , err )
}
err = r . Delete ( ctx , key2 )
if err != nil {
t . Fatalf ( "Delete error: %v\n" , err )
}
}
/ *
func Test_MReadCodec ( t * testing . T ) {
type mytype struct {
Key string ` json:"name" `
Val string ` json:"val" `
}
ctx := context . Background ( )
var err error
if tr := os . Getenv ( "INTEGRATION_TESTS" ) ; len ( tr ) > 0 {
t . Skip ( )
}
r := NewStore ( store . Nodes ( os . Getenv ( "STORE_NODES" ) ) , store . Codec ( jsoncodec . NewCodec ( ) ) )
if err = r . Init ( ) ; err != nil {
t . Fatal ( err )
}
if err = r . Connect ( ctx ) ; err != nil {
t . Fatal ( err )
}
key1 := "myTest1"
key2 := "myTest2"
key3 := "myTest3"
tval1 := & mytype { Key : "key1" , Val : "val1" }
tval2 := & mytype { Key : "key2" , Val : "val2" }
var vals [ ] * mytype
err = r . Write ( ctx , key1 , tval1 , store . WriteTTL ( 2 * time . Minute ) )
if err != nil {
t . Fatalf ( "Write error: %v" , err )
}
err = r . Write ( ctx , key2 , tval2 , store . WriteTTL ( 2 * time . Minute ) )
if err != nil {
t . Fatalf ( "Write error: %v" , err )
}
err = r . MRead ( ctx , [ ] string { key1 , key3 , key2 } , & vals )
if err != nil {
t . Fatalf ( "Read error: %v\n" , err )
}
if vals [ 0 ] . Key != "key1" || vals [ 1 ] != nil || vals [ 2 ] . Key != "key2" {
t . Fatalf ( "read err: struct not filled" )
}
keys , 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
}
2022-10-26 13:46:48 +03:00
_ = keys
err = r . Delete ( ctx , key1 )
if err != nil {
t . Fatalf ( "Delete error: %v\n" , err )
}
err = r . Delete ( ctx , key2 )
if err != nil {
t . Fatalf ( "Delete error: %v\n" , err )
}
2020-07-06 07:59:32 -05:00
}
2022-10-26 13:46:48 +03:00
* /