Skip to content

Commit

Permalink
test: rueidislock in case of flush
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Sep 22, 2023
1 parent 4e74bce commit dbcc67f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
8 changes: 0 additions & 8 deletions rueidislock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,6 @@ func (m *locker) WithContext(ctx context.Context, name string) (context.Context,
}
}
if cancel(); err != nil {
if err == ErrLockerClosed {
m.mu.RLock()
closed := m.gates == nil
m.mu.RUnlock()
if !closed {
continue
}
}
return ctx, cancel, err
}
}
Expand Down
47 changes: 46 additions & 1 deletion rueidislock/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/redis/rueidis"
)

var address = []string{"127.0.0.1:6376"}
var address = []string{"127.0.0.1:6379"}

func newLocker(t *testing.T, noLoop, setpx, nocsc bool) *locker {
impl, err := NewLocker(LockerOption{
Expand Down Expand Up @@ -512,3 +512,48 @@ func TestLocker_RetryErrLockerClosed(t *testing.T) {
})
}
}

func TestLocker_Flush(t *testing.T) {
test := func(t *testing.T, noLoop, setpx, nocsc bool) {
client, err := rueidis.NewClient(rueidis.ClientOption{InitAddress: address})
if err != nil {
t.Fatal(err)
}
defer client.Close()

locker := newLocker(t, noLoop, setpx, nocsc)

lck := strconv.Itoa(rand.Int())
ctx, _, err := locker.WithContext(context.Background(), lck)
if err != nil {
t.Fatal(err)
}

if err := client.Do(context.Background(), client.B().Flushall().Build()).Error(); err != nil {
t.Fatal(err)
}

<-ctx.Done()

if err := ctx.Err(); !errors.Is(err, context.Canceled) {
t.Fatal(err)
}

ctx, cancel, err := locker.WithContext(context.Background(), strconv.Itoa(rand.Int()))
if err != nil {
t.Fatal(err)
}
cancel()
}
for _, nocsc := range []bool{false, true} {
t.Run("Tracking Loop", func(t *testing.T) {
test(t, false, false, nocsc)
})
t.Run("Tracking NoLoop", func(t *testing.T) {
test(t, true, false, nocsc)
})
t.Run("SET PX", func(t *testing.T) {
test(t, true, true, nocsc)
})
}
}

0 comments on commit dbcc67f

Please sign in to comment.