Skip to content

Commit

Permalink
Merge pull request #2 from kiran94/refactor/apply-interface
Browse files Browse the repository at this point in the history
refactor(redlock): apply interface
  • Loading branch information
vvanglro authored Jun 10, 2022
2 parents f2a0902 + 2047380 commit a2df1d2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion redlock/redlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ type RedLock struct {
client []*RedClient
}

type RedLocker interface {
SetLock(ctx context.Context, key string, value string, ttl int) (float64, error)
UnSetLock(ctx context.Context, key string, value string) (float64, error)
GetLockTtl(ctx context.Context, key string, value string) (int64, error)
IsLocked(ctx context.Context, key string) bool
}

// NewRedisLock returns a RedisLock.
func NewRedisLock(ctx context.Context, options ...*red.Options) *RedLock {
func NewRedisLock(ctx context.Context, options ...*red.Options) RedLocker {
var clients []*RedClient
for _, opt := range options {
client := red.NewClient(opt)
Expand Down
9 changes: 7 additions & 2 deletions redlock/redlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package redlock
import (
"context"
"fmt"
red "github.com/go-redis/redis/v8"
"testing"

red "github.com/go-redis/redis/v8"
)

func CommonClient() []*RedClient {
Expand Down Expand Up @@ -48,7 +49,11 @@ func TestNewRedisLock(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewRedisLock(tt.args.ctx, tt.args.options...)
for _, client := range got.client {
gotOut, ok := got.(*RedLock)
if !ok {
t.Fatalf("new redis lock was not a redlock instance")
}
for _, client := range gotOut.client {
err := client.cli.Ping(ctx).Err()
if err != nil {
t.Fatalf("redis connect error")
Expand Down

0 comments on commit a2df1d2

Please sign in to comment.