Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaqiuxy committed Jun 7, 2021
1 parent 1c3a8e1 commit fb616ef
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dev:
docker compose up --abort-on-container-exit

test: fmt
go test
go test ./...

fmt:
go fmt ./...
Expand Down
39 changes: 1 addition & 38 deletions cache_test.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,21 @@
package wfcache_test

import (
"context"
"encoding/json"
"fmt"
"reflect"
"sync"
"testing"
"time"

"github.com/go-redis/redis/v8"
"github.com/juliaqiuxy/wfcache"
basicAdapter "github.com/juliaqiuxy/wfcache/basic"
bigCacheAdapter "github.com/juliaqiuxy/wfcache/bigcache"
redisAdapter "github.com/juliaqiuxy/wfcache/redis"
)

var redisOnce sync.Once
var redisInstance *redis.Client

func RedisClient() *redis.Client {
redisOnce.Do(func() {
var err error
redisInstance, err = redisDb()

if err != nil {
panic(err)
}
})

return redisInstance
}

func redisDb() (*redis.Client, error) {
db := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
})

err := db.Ping(context.Background()).Err()
if err != nil {
return nil, err
}

return db, nil
}

func TestSanityCheck(t *testing.T) {
r := RedisClient()

func TestWfCache(t *testing.T) {
c, _ := wfcache.Create(
basicAdapter.Create(5*time.Minute),
bigCacheAdapter.Create(30*time.Minute),
redisAdapter.Create(r, 6*time.Hour),
)

key := "my_key"
Expand Down
73 changes: 73 additions & 0 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package redis_test

import (
"context"
"encoding/json"
"fmt"
"reflect"
"sync"
"testing"
"time"

"github.com/go-redis/redis/v8"
"github.com/juliaqiuxy/wfcache"
redisAdapter "github.com/juliaqiuxy/wfcache/redis"
)

var redisOnce sync.Once
var redisInstance *redis.Client

func RedisClient() *redis.Client {
var redisDb = func() (*redis.Client, error) {
db := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
})

err := db.Ping(context.Background()).Err()
if err != nil {
return nil, err
}

return db, nil
}

redisOnce.Do(func() {
var err error
redisInstance, err = redisDb()

if err != nil {
panic(err)
}
})

return redisInstance
}

func TestRedis(t *testing.T) {
r := RedisClient()

c, _ := wfcache.Create(
redisAdapter.Create(r, 6*time.Hour),
)

key := "my_key"
val := "my_value"

c.Set(key, val)

items, err := c.BatchGet([]string{key})

if len(items) != 1 {
t.Errorf("Received %v items, expected 1", len(items))
}

var str string
json.Unmarshal(items[0].Value, &str)

if str != val {
t.Errorf("Received %v (type %v), expected %v (type %v)", str, reflect.TypeOf(str), val, reflect.TypeOf(val))
}

fmt.Println(items, str, err)
}

0 comments on commit fb616ef

Please sign in to comment.