Skip to content

Commit

Permalink
test(crontab): redis mutex test case
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Dec 21, 2023
1 parent 381d912 commit fba23db
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions crontab/mutex/redis/mutex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package redis

import (
"context"
"testing"
"time"

"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"

"github.com/go-packagist/go-kratos-components/crontab"
)

var (
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})

ctx = context.Background()
)

func TestMutex(t *testing.T) {
rdb.Ping(context.Background())
m1 := New(rdb, WithExpired(time.Second*1))
m2 := New(rdb, WithExpired(time.Second*1))

assert.NoError(t, m1.Lock(ctx, "test"))
assert.NoError(t, m1.Lock(ctx, "test"))
assert.EqualError(t, m2.Lock(ctx, "test"), crontab.ErrAnotherServerRunning.Error())

time.Sleep(time.Second * 2)
assert.NoError(t, m2.Lock(ctx, "test"))
assert.EqualError(t, m1.Lock(ctx, "test"), crontab.ErrAnotherServerRunning.Error())

assert.NoError(t, m2.Unlock(ctx, "test"))
assert.NoError(t, m1.Lock(ctx, "test"))
assert.EqualError(t, m2.Lock(ctx, "test"), crontab.ErrAnotherServerRunning.Error())
}

0 comments on commit fba23db

Please sign in to comment.