Skip to content

Commit

Permalink
Fix Valid function
Browse files Browse the repository at this point in the history
  • Loading branch information
hjr265 committed Mar 29, 2020
1 parent 367f6e0 commit fb51a7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 8 additions & 5 deletions mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,21 @@ func (m *Mutex) Extend() (bool, error) {
return true, nil
}

func (m *Mutex) Valid() bool {
n := m.actOnPoolsAsync(func(pool Pool) bool {
func (m *Mutex) Valid() (bool, error) {
n, err := m.actOnPoolsAsync(func(pool Pool) (bool, error) {
return m.valid(pool)
})
return n >= m.quorum
return n >= m.quorum, err
}

func (m *Mutex) valid(pool Pool) bool {
func (m *Mutex) valid(pool Pool) (bool, error) {
conn := pool.Get()
defer conn.Close()
reply, err := redis.String(conn.Do("GET", m.name))
return err == nil && m.value == reply
if err != nil {
return false, err
}
return m.value == reply, nil
}

func genValue() (string, error) {
Expand Down
6 changes: 5 additions & 1 deletion mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func TestValid(t *testing.T) {
}
assertAcquired(t, pools, mutex1)

if !mutex1.Valid() {
ok, err := mutex1.Valid()
if err != nil {
t.Fatalf("Expected err != nil, got: %q", err)
}
if !ok {
t.Fatalf("Expected a valid mutex")
}

Expand Down

0 comments on commit fb51a7f

Please sign in to comment.