Skip to content

Commit

Permalink
fix: revert hiding os.ErrDeadlineExceeded with context.DeadlineExceeded
Browse files Browse the repository at this point in the history
Signed-off-by: Rueian <[email protected]>
  • Loading branch information
rueian committed Nov 14, 2024
1 parent e84fc9e commit 2f05d34
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
9 changes: 1 addition & 8 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rueidis
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -590,7 +589,7 @@ func (p *pipe) backgroundPing() {
go func() { ch <- p.Do(context.Background(), cmds.PingCmd).NonRedisError() }()
select {
case <-tm.C:
err = context.DeadlineExceeded
err = os.ErrDeadlineExceeded
case err = <-ch:
tm.Stop()
}
Expand Down Expand Up @@ -1165,9 +1164,6 @@ func (p *pipe) syncDo(dl time.Time, dlOk bool, cmd Completed) (resp RedisResult)
msg, err = syncRead(p.r)
}
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
err = context.DeadlineExceeded
}
p.error.CompareAndSwap(nil, &errs{error: err})
p.conn.Close()
p.background() // start the background worker to clean up goroutines
Expand Down Expand Up @@ -1218,9 +1214,6 @@ process:
}
return
abort:
if errors.Is(err, os.ErrDeadlineExceeded) {
err = context.DeadlineExceeded
}
p.error.CompareAndSwap(nil, &errs{error: err})
p.conn.Close()
p.background() // start the background worker to clean up goroutines
Expand Down
29 changes: 15 additions & 14 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net"
"os"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -3402,7 +3403,7 @@ func TestExitOnRingFullAndPingTimout(t *testing.T) {
// fill the ring
for i := 0; i < len(p.queue.(*ring).store); i++ {
go func() {
if err := p.Do(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).Error(); err != context.DeadlineExceeded {
if err := p.Do(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).Error(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Errorf("unexpected result, expected context.DeadlineExceeded, got %v", err)
}
}()
Expand All @@ -3412,7 +3413,7 @@ func TestExitOnRingFullAndPingTimout(t *testing.T) {
mock.Expect("GET", "a")
}

if err := p.Do(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).Error(); err != context.DeadlineExceeded {
if err := p.Do(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).Error(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Errorf("unexpected result, expected context.DeadlineExceeded, got %v", err)
}
}
Expand Down Expand Up @@ -3944,7 +3945,7 @@ func TestSyncModeSwitchingWithDeadlineExceed_Do(t *testing.T) {
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
if err := p.Do(ctx, cmds.NewCompleted([]string{"GET", "a"})).NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.Do(ctx, cmds.NewCompleted([]string{"GET", "a"})).NonRedisError(); !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, os.ErrDeadlineExceeded) {
t.Errorf("unexpected err %v", err)
}
wg.Done()
Expand All @@ -3970,7 +3971,7 @@ func TestSyncModeSwitchingWithDeadlineExceed_DoMulti(t *testing.T) {
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
if err := p.DoMulti(ctx, cmds.NewCompleted([]string{"GET", "a"})).s[0].NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.DoMulti(ctx, cmds.NewCompleted([]string{"GET", "a"})).s[0].NonRedisError(); !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, os.ErrDeadlineExceeded) {
t.Errorf("unexpected err %v", err)
}
wg.Done()
Expand All @@ -3992,7 +3993,7 @@ func TestOngoingDeadlineContextInSyncMode_Do(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Second/2))
defer cancel()

if err := p.Do(ctx, cmds.NewCompleted([]string{"GET", "a"})).NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.Do(ctx, cmds.NewCompleted([]string{"GET", "a"})).NonRedisError(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("unexpected err %v", err)
}
p.Close()
Expand All @@ -4003,7 +4004,7 @@ func TestWriteDeadlineInSyncMode_Do(t *testing.T) {
p, _, _, closeConn := setup(t, ClientOption{ConnWriteTimeout: 1 * time.Second / 2, Dialer: net.Dialer{KeepAlive: time.Second / 3}})
defer closeConn()

if err := p.Do(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.Do(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).NonRedisError(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("unexpected err %v", err)
}
p.Close()
Expand All @@ -4018,7 +4019,7 @@ func TestWriteDeadlineIsShorterThanContextDeadlineInSyncMode_Do(t *testing.T) {
defer cancel()

startTime := time.Now()
if err := p.Do(ctx, cmds.NewCompleted([]string{"GET", "a"})).NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.Do(ctx, cmds.NewCompleted([]string{"GET", "a"})).NonRedisError(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("unexpected err %v", err)
}

Expand All @@ -4038,7 +4039,7 @@ func TestWriteDeadlineIsNoShorterThanContextDeadlineInSyncMode_DoBlocked(t *test
defer cancel()

startTime := time.Now()
if err := p.Do(ctx, cmds.NewBlockingCompleted([]string{"BLPOP", "a"})).NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.Do(ctx, cmds.NewBlockingCompleted([]string{"BLPOP", "a"})).NonRedisError(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("unexpected err %v", err)
}

Expand All @@ -4057,7 +4058,7 @@ func TestOngoingDeadlineContextInSyncMode_DoMulti(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Second/2))
defer cancel()

if err := p.DoMulti(ctx, cmds.NewCompleted([]string{"GET", "a"})).s[0].NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.DoMulti(ctx, cmds.NewCompleted([]string{"GET", "a"})).s[0].NonRedisError(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("unexpected err %v", err)
}
p.Close()
Expand All @@ -4068,7 +4069,7 @@ func TestWriteDeadlineInSyncMode_DoMulti(t *testing.T) {
p, _, _, closeConn := setup(t, ClientOption{ConnWriteTimeout: time.Second / 2, Dialer: net.Dialer{KeepAlive: time.Second / 3}})
defer closeConn()

if err := p.DoMulti(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).s[0].NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.DoMulti(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).s[0].NonRedisError(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("unexpected err %v", err)
}
p.Close()
Expand All @@ -4083,7 +4084,7 @@ func TestWriteDeadlineIsShorterThanContextDeadlineInSyncMode_DoMulti(t *testing.
defer cancel()

startTime := time.Now()
if err := p.DoMulti(ctx, cmds.NewCompleted([]string{"GET", "a"})).s[0].NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.DoMulti(ctx, cmds.NewCompleted([]string{"GET", "a"})).s[0].NonRedisError(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("unexpected err %v", err)
}

Expand All @@ -4103,7 +4104,7 @@ func TestWriteDeadlineIsNoShorterThanContextDeadlineInSyncMode_DoMultiBlocked(t
defer cancel()

startTime := time.Now()
if err := p.DoMulti(ctx, cmds.NewBlockingCompleted([]string{"BLPOP", "a"})).s[0].NonRedisError(); !errors.Is(err, context.DeadlineExceeded) {
if err := p.DoMulti(ctx, cmds.NewBlockingCompleted([]string{"BLPOP", "a"})).s[0].NonRedisError(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatalf("unexpected err %v", err)
}

Expand Down Expand Up @@ -4170,7 +4171,7 @@ func TestOngoingWriteTimeoutInPipelineMode_Do(t *testing.T) {
for i := 0; i < 5; i++ {
go func() {
_, err := p.Do(ctx, cmds.NewCompleted([]string{"GET", "a"})).ToString()
if errors.Is(err, context.DeadlineExceeded) {
if errors.Is(err, os.ErrDeadlineExceeded) {
atomic.AddInt32(&timeout, 1)
} else {
t.Errorf("unexpected err %v", err)
Expand Down Expand Up @@ -4244,7 +4245,7 @@ func TestOngoingWriteTimeoutInPipelineMode_DoMulti(t *testing.T) {
for i := 0; i < 5; i++ {
go func() {
_, err := p.DoMulti(ctx, cmds.NewCompleted([]string{"GET", "a"})).s[0].ToString()
if errors.Is(err, context.DeadlineExceeded) {
if errors.Is(err, os.ErrDeadlineExceeded) {
atomic.AddInt32(&timeout, 1)
} else {
t.Errorf("unexpected err %v", err)
Expand Down
5 changes: 3 additions & 2 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rueidis
import (
"bytes"
"context"
"errors"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -167,7 +168,7 @@ func testSETGET(t *testing.T, client Client, csc bool) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
val, err := client.Do(ctx, client.B().Get().Key(key).Build()).ToString()
if err != context.DeadlineExceeded {
if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, os.ErrDeadlineExceeded) {
if v, ok := kvs[key]; !((ok && val == v) || (!ok && IsRedisNil(err))) {
t.Errorf("unexpected get response %v %v %v", val, err, ok)
}
Expand Down Expand Up @@ -318,7 +319,7 @@ func testMultiSETGET(t *testing.T, client Client, csc bool) {
defer cancel()
for j, resp := range client.DoMulti(ctx, commands...) {
val, err := resp.ToString()
if err != context.DeadlineExceeded {
if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, os.ErrDeadlineExceeded) {
if v, ok := kvs[cmdkeys[j]]; !((ok && val == v) || (!ok && IsRedisNil(err))) {
t.Fatalf("unexpected get response %v %v %v", val, err, ok)
}
Expand Down

0 comments on commit 2f05d34

Please sign in to comment.