Skip to content

Commit

Permalink
refactor: apply staticchecks
Browse files Browse the repository at this point in the history
Signed-off-by: Rueian <rueiancsie@gmail.com>
  • Loading branch information
rueian committed May 2, 2024
1 parent 4dd52dd commit cf567f7
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func test(t *testing.T, storeFn func() CacheStore) {
t.Fatal("CachePXAT should return a desired pttl")
}

v2, e2 = store.Flight("key", "cmd", time.Millisecond*100, now)
v2, _ = store.Flight("key", "cmd", time.Millisecond*100, now)
if v2.typ != v.typ || v2.string != v.string {
t.Fatal("flights after Update should return updated RedisMessage")
}
Expand Down
5 changes: 1 addition & 4 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2696,10 +2696,7 @@ func TestClusterClient_SendReadOperationToReplicaNodesWriteOperationToPrimaryNod
&ClientOption{
InitAddress: []string{"127.0.0.1:0"},
SendToReplicas: func(cmd Completed) bool {
if cmd.IsReadOnly() {
return true
}
return false
return cmd.IsReadOnly()
},
},
func(dst string, opt *ClientOption) conn {
Expand Down
6 changes: 2 additions & 4 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func TestMuxDelegation(t *testing.T) {
result := make([]RedisResult, len(multi))
for j, cmd := range multi {
if s := cmd.Cmd.Slot() & uint16(len(wires)-1); s != idx {
result[j] = newErrResult(errors.New(fmt.Sprintf("wrong slot %v %v", s, idx)))
result[j] = newErrResult(fmt.Errorf("wrong slot %v %v", s, idx))
} else {
result[j] = newResult(RedisMessage{typ: '+', string: cmd.Cmd.Commands()[1]}, nil)
}
Expand Down Expand Up @@ -497,7 +497,7 @@ func TestMuxDelegation(t *testing.T) {
DoMultiCacheFn: func(multi ...CacheableTTL) *redisresults {
for _, cmd := range multi {
if s := cmd.Cmd.Slot() & uint16(len(wires)-1); s != idx {
return &redisresults{s: []RedisResult{newErrResult(errors.New(fmt.Sprintf("wrong slot %v %v", s, idx)))}}
return &redisresults{s: []RedisResult{newErrResult(fmt.Errorf("wrong slot %v %v", s, idx))}}
}
}
return &redisresults{s: []RedisResult{newErrResult(context.DeadlineExceeded)}}
Expand Down Expand Up @@ -896,7 +896,6 @@ func (m *mockWire) CleanSubscriptions() {
if m.CleanSubscriptionsFn != nil {
m.CleanSubscriptionsFn()
}
return
}

func (m *mockWire) SetPubSubHooks(hooks PubSubHooks) <-chan error {
Expand All @@ -910,7 +909,6 @@ func (m *mockWire) SetOnCloseHook(fn func(error)) {
if m.SetOnCloseHookFn != nil {
m.SetOnCloseHookFn(fn)
}
return
}

func (m *mockWire) Info() map[string]RedisMessage {
Expand Down
5 changes: 2 additions & 3 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,8 +1164,8 @@ func (p *pipe) syncDo(dl time.Time, dlOk bool, cmd Completed) (resp RedisResult)
}

var msg RedisMessage
err := writeCmd(p.w, cmd.Commands())
if err = p.w.Flush(); err == nil {
err := flushCmd(p.w, cmd.Commands())
if err == nil {
msg, err = syncRead(p.r)
}
if err != nil {
Expand Down Expand Up @@ -1225,7 +1225,6 @@ abort:
for i := 0; i < len(resp); i++ {
resp[i] = newErrResult(err)
}
return
}

func syncRead(r *bufio.Reader) (m RedisMessage, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func write(o io.Writer, m RedisMessage) (err error) {
_, err = o.Write([]byte{m.typ})
switch m.typ {
case '$':
_, err = o.Write(append([]byte(strconv.Itoa(len(m.string))), '\r', '\n'))
_, _ = o.Write(append([]byte(strconv.Itoa(len(m.string))), '\r', '\n'))
_, err = o.Write(append([]byte(m.string), '\r', '\n'))
case '+', '-', '_':
_, err = o.Write(append([]byte(m.string), '\r', '\n'))
Expand Down
5 changes: 5 additions & 0 deletions resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ func writeCmd(o *bufio.Writer, cmd []string) (err error) {
return err
}

func flushCmd(o *bufio.Writer, cmd []string) (err error) {
_ = writeCmd(o, cmd)
return o.Flush()
}

const (
unexpectedNoCRLF = "received unexpected simple string message ending without CRLF"
unexpectedNumByte = "received unexpected number byte: "
Expand Down
6 changes: 2 additions & 4 deletions resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package rueidis
import (
"bufio"
"bytes"
crand "crypto/rand"
"io"
"math/rand"
"reflect"
"strconv"
"strings"
"testing"
"time"
)

const iteration = 100
Expand All @@ -18,8 +18,6 @@ var generators = map[byte]func(i int64, f float64, str string) string{}

//gocyclo:ignore
func init() {
rand.Seed(time.Now().UnixNano())

generators['$'] = func(i int64, f float64, str string) string {
return strconv.Itoa(len(str)) + "\r\n" + str + "\r\n"
}
Expand Down Expand Up @@ -683,7 +681,7 @@ func source(str string) *bufio.Reader {
func random(trim bool) string {
retry:
bs := make([]byte, randN(5000))
if _, err := rand.Read(bs); err != nil {
if _, err := crand.Read(bs); err != nil {
panic(err)
}
if trim {
Expand Down
2 changes: 1 addition & 1 deletion ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newRing(factor int) *ring {
m := &sync.Mutex{}
r.store[i].c1 = sync.NewCond(m)
r.store[i].c2 = sync.NewCond(m)
r.store[i].ch = make(chan RedisResult, 0) // this channel can't be buffered
r.store[i].ch = make(chan RedisResult) // this channel can't be buffered
}
return r
}
Expand Down
4 changes: 2 additions & 2 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ func TestRing(t *testing.T) {

t.Run("PutMulti Wakeup WaitForWrite", func(t *testing.T) {
ring := newRing(DefaultRingScale)
if _, multi, ch := ring.NextWriteCmd(); ch == nil {
if _, _, ch := ring.NextWriteCmd(); ch == nil {
go func() {
time.Sleep(time.Millisecond * 100)
ring.PutMulti([]Completed{cmds.PingCmd}, nil)
}()
if _, multi, ch = ring.WaitForWrite(); ch != nil && multi[0].Commands()[0] == cmds.PingCmd.Commands()[0] {
if _, multi, ch := ring.WaitForWrite(); ch != nil && multi[0].Commands()[0] == cmds.PingCmd.Commands()[0] {
return
}
}
Expand Down
8 changes: 4 additions & 4 deletions rueidislock/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func newClient(t *testing.T) rueidis.Client {
}

func TestNewLocker(t *testing.T) {
l, err := NewLocker(LockerOption{ClientOption: rueidis.ClientOption{InitAddress: nil}})
_, err := NewLocker(LockerOption{ClientOption: rueidis.ClientOption{InitAddress: nil}})
if err == nil {
t.Fatal(err)
}
l, err = NewLocker(LockerOption{ClientOption: rueidis.ClientOption{InitAddress: address}})
l, err := NewLocker(LockerOption{ClientOption: rueidis.ClientOption{InitAddress: address}})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestLocker_WithContext_DeadContext(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
cancel()
ctx, cancel, err := locker.WithContext(ctx, strconv.Itoa(rand.Int()))
_, _, err := locker.WithContext(ctx, strconv.Itoa(rand.Int()))
if !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
Expand Down Expand Up @@ -589,7 +589,7 @@ func TestLocker_Flush(t *testing.T) {
t.Fatal(err)
}

ctx, cancel, err := locker.WithContext(context.Background(), strconv.Itoa(rand.Int()))
_, cancel, err := locker.WithContext(context.Background(), strconv.Itoa(rand.Int()))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion singleflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *call) LazyDo(threshold time.Duration, fn func() error) {
c.cn++
ts := c.ts
c.mu.Unlock()
time.Sleep(ts.Add(threshold).Sub(time.Now()))
time.Sleep(time.Until(ts.Add(threshold)))
go c.do(ch, fn)
}

Expand Down

0 comments on commit cf567f7

Please sign in to comment.