Skip to content

Commit

Permalink
Merge pull request #61 from bancek/fix-extend-unlock
Browse files Browse the repository at this point in the history
Fix Extend and Unlock
  • Loading branch information
hjr265 authored Dec 2, 2020
2 parents eb773f5 + 5e3c32b commit ef3eedd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (m *Mutex) release(ctx context.Context, pool redis.Pool, value string) (boo
if err != nil {
return false, err
}
return status != 0, nil
return status != int64(0), nil
}

var touchScript = redis.NewScript(1, `
Expand All @@ -190,7 +190,7 @@ func (m *Mutex) touch(ctx context.Context, pool redis.Pool, value string, expiry
if err != nil {
return false, err
}
return status != "ERR", nil
return status != int64(0), nil
}

func (m *Mutex) actOnPoolsAsync(actFn func(redis.Pool) (bool, error)) (int, error) {
Expand Down
52 changes: 52 additions & 0 deletions mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,58 @@ func TestMutexExtend(t *testing.T) {
}
}

func TestMutexExtendExpired(t *testing.T) {
for k, v := range makeCases(8) {
t.Run(k, func(t *testing.T) {
mutexes := newTestMutexes(v.pools, "test-mutex-extend", 1)
mutex := mutexes[0]
mutex.expiry = 500 * time.Millisecond

err := mutex.Lock()
if err != nil {
t.Fatalf("mutex lock failed: %s", err)
}
defer mutex.Unlock()

time.Sleep(1 * time.Second)

ok, err := mutex.Extend()
if err != nil {
t.Fatalf("mutex extend failed: %s", err)
}
if ok {
t.Fatalf("Expected ok == false, got %v", ok)
}
})
}
}

func TestMutexUnlockExpired(t *testing.T) {
for k, v := range makeCases(8) {
t.Run(k, func(t *testing.T) {
mutexes := newTestMutexes(v.pools, "test-mutex-extend", 1)
mutex := mutexes[0]
mutex.expiry = 500 * time.Millisecond

err := mutex.Lock()
if err != nil {
t.Fatalf("mutex lock failed: %s", err)
}
defer mutex.Unlock()

time.Sleep(1 * time.Second)

ok, err := mutex.Unlock()
if err != nil {
t.Fatalf("mutex unlock failed: %s", err)
}
if ok {
t.Fatalf("Expected ok == false, got %v", ok)
}
})
}
}

func TestMutexQuorum(t *testing.T) {
for k, v := range makeCases(4) {
t.Run(k, func(t *testing.T) {
Expand Down

0 comments on commit ef3eedd

Please sign in to comment.