Skip to content

Commit

Permalink
Merge pull request #532 from redis/feat-rueidishook-error-result
Browse files Browse the repository at this point in the history
feat: add NewErrorResult() to rueidishook
  • Loading branch information
rueian authored Apr 13, 2024
2 parents cb9d65c + 952881e commit cac87e5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
23 changes: 23 additions & 0 deletions rueidishook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rueidishook
import (
"context"
"time"
"unsafe"

"github.com/redis/rueidis"
)
Expand Down Expand Up @@ -148,3 +149,25 @@ func (e *extended) Dedicate() (client rueidis.DedicatedClient, cancel func()) {
func (e *extended) Nodes() map[string]rueidis.Client {
panic("Nodes() is not allowed with rueidis.DedicatedClient")
}

type result struct {
err error
val rueidis.RedisMessage
}

func NewErrorResult(err error) rueidis.RedisResult {
r := result{err: err}
return *(*rueidis.RedisResult)(unsafe.Pointer(&r))
}

type stream struct {
p *int
w *int
e error
n int
}

func NewErrorResultStream(err error) rueidis.RedisResultStream {
r := stream{e: err}
return *(*rueidis.RedisResultStream)(unsafe.Pointer(&r))
}
22 changes: 22 additions & 0 deletions rueidishook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,25 @@ func TestForbiddenMethodForDedicatedClient(t *testing.T) {
shouldpanic(c.fn, c.msg)
}
}

func TestNewErrorResult(t *testing.T) {
e := errors.New("err")
r := NewErrorResult(e)
if r.Error() != e {
t.Fatal("unexpected err")
}
}

func TestNewErrorResultStream(t *testing.T) {
e := errors.New("err")
r := NewErrorResultStream(e)
if r.Error() != e {
t.Fatal("unexpected err")
}
if r.HasNext() {
t.Fatal("unexpected err")
}
if n, err := r.WriteTo(io.Discard); err != e || n != 0 {
t.Fatal("unexpected err or n")
}
}

0 comments on commit cac87e5

Please sign in to comment.