Skip to content

Commit

Permalink
fix: test goroutine leaks (#643)
Browse files Browse the repository at this point in the history
Ensure that goroutines started by tests are cleaned up on termination.

Also:
* make TestLatencyHistories compatible with -count=X.
* Update to the supported versions of go 1.19 and 1.20.
* Update golangci-lint to v1.15.2

Fixes #641
  • Loading branch information
stevenh committed May 11, 2023
1 parent d685447 commit 78e255f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
strategy:
matrix:
go-version:
- '1.17.x'
- '1.18.x'
- '1.19.x'
- '1.20.x'
os:
- 'ubuntu-latest'
redis:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.46.2
version: v1.51.2

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
34 changes: 31 additions & 3 deletions redis/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"math"
"net"
"sync"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -479,22 +480,49 @@ func TestError(t *testing.T) {
}

func TestReadTimeout(t *testing.T) {
done := make(chan struct{})
errs := make(chan error, 2)
defer func() {
close(done)
for err := range errs {
require.NoError(t, err)
}
}()

var wg sync.WaitGroup
defer func() {
wg.Wait()
close(errs)
}()

l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("net.Listen returned %v", err)
}
defer l.Close()

wg.Add(1)
go func() {
defer wg.Done()

for {
c, err := l.Accept()
if err != nil {
return
}
wg.Add(1)
go func() {
time.Sleep(time.Second)
defer wg.Done()

to := time.NewTimer(time.Second)
defer to.Stop()
select {
case <-to.C:
case <-done:
return
}
_, err := c.Write([]byte("+OK\r\n"))
require.NoError(t, err)
errs <- err
c.Close()
}()
}
Expand Down Expand Up @@ -719,7 +747,7 @@ type blockedReader struct {

func (b blockedReader) Read(p []byte) (n int, err error) {
<-b.ch
return 0, nil
return 0, io.EOF
}

func dialTestBlockedConn(ch chan struct{}, w io.Writer) redis.DialOption {
Expand Down
6 changes: 5 additions & 1 deletion redis/reply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ package redis_test

import (
"fmt"
"github.com/stretchr/testify/require"
"math"
"reflect"
"strconv"
"testing"
"time"

"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -288,6 +288,10 @@ func TestLatencyHistories(t *testing.T) {
latencyMonitorThresholdOldCfg, err := strconv.Atoi(res[1])
require.NoError(t, err)

// Reset so we're compatible with -count=X
_, err = c.Do("LATENCY", "RESET", "command")
require.NoError(t, err)

// Enable latency monitoring for events that take 1ms or longer
result, err := c.Do("CONFIG", "SET", "latency-monitor-threshold", "1")
// reset the old configuration after test.
Expand Down

0 comments on commit 78e255f

Please sign in to comment.