Skip to content

Commit

Permalink
chore: enhance goroutine leak detection in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinB committed Aug 12, 2024
1 parent d21ed24 commit 15f75ef
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
49 changes: 48 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package warc

import (
"io"
"net"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -14,9 +15,12 @@ import (
"time"

"github.com/armon/go-socks5"
"go.uber.org/goleak"
)

func TestHTTPClient(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand Down Expand Up @@ -84,6 +88,8 @@ func TestHTTPClient(t *testing.T) {
}

func TestHTTPClientWithProxy(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand All @@ -97,12 +103,29 @@ func TestHTTPClientWithProxy(t *testing.T) {
panic(err)
}

// Create a channel to signal server stop
stopChan := make(chan struct{})

go func() {
if err := proxyServer.ListenAndServe("tcp", "127.0.0.1:8000"); err != nil {
listener, err := net.Listen("tcp", "127.0.0.1:8000")
if err != nil {
panic(err)
}
defer listener.Close()

go func() {
<-stopChan
listener.Close()
}()

if err := proxyServer.Serve(listener); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
panic(err)
}
}()

// Defer sending the stop signal
defer close(stopChan)

// init test HTTP endpoint
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fileBytes, err := os.ReadFile(path.Join("testdata", "image.svg"))
Expand Down Expand Up @@ -166,6 +189,8 @@ func TestHTTPClientWithProxy(t *testing.T) {
}

func TestHTTPClientConcurrent(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
concurrency = 256
Expand Down Expand Up @@ -248,6 +273,8 @@ func TestHTTPClientConcurrent(t *testing.T) {
}

func TestHTTPClientMultiWARCWriters(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
concurrency = 256
Expand Down Expand Up @@ -336,6 +363,8 @@ func TestHTTPClientMultiWARCWriters(t *testing.T) {
}

func TestHTTPClientLocalDedupe(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand Down Expand Up @@ -418,6 +447,8 @@ func TestHTTPClientLocalDedupe(t *testing.T) {
}

func TestHTTPClientRemoteDedupe(t *testing.T) {
defer goleak.VerifyNone(t)

var (
dedupePath = "/web/timemap/cdx"
dedupeResp = "org,wikimedia,upload)/wikipedia/commons/5/55/blason_ville_fr_sarlat-la-can%c3%a9da_(dordogne).svg 20220320002518 https://upload.wikimedia.org/wikipedia/commons/5/55/Blason_ville_fr_Sarlat-la-Can%C3%A9da_%28Dordogne%29.svg image/svg+xml 200 UIRWL5DFIPQ4MX3D3GFHM2HCVU3TZ6I3 13974"
Expand Down Expand Up @@ -512,6 +543,8 @@ func TestHTTPClientRemoteDedupe(t *testing.T) {
}

func TestHTTPClientDisallow429(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand Down Expand Up @@ -586,6 +619,8 @@ func TestHTTPClientDisallow429(t *testing.T) {
}

func TestHTTPClientPayloadLargerThan2MB(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand Down Expand Up @@ -654,6 +689,8 @@ func TestHTTPClientPayloadLargerThan2MB(t *testing.T) {
}

func TestConcurrentHTTPClientPayloadLargerThan2MB(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
err error
Expand Down Expand Up @@ -741,6 +778,8 @@ func TestConcurrentHTTPClientPayloadLargerThan2MB(t *testing.T) {
}

func TestHTTPClientWithSelfSignedCertificate(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand Down Expand Up @@ -809,6 +848,8 @@ func TestHTTPClientWithSelfSignedCertificate(t *testing.T) {
}

func TestWARCWritingWithDisallowedCertificate(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand Down Expand Up @@ -880,6 +921,8 @@ func TestWARCWritingWithDisallowedCertificate(t *testing.T) {
}

func TestHTTPClientFullOnDisk(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
err error
Expand Down Expand Up @@ -947,6 +990,8 @@ func TestHTTPClientFullOnDisk(t *testing.T) {
}

func TestHTTPClientWithoutIoCopy(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand Down Expand Up @@ -1021,6 +1066,8 @@ func TestHTTPClientWithoutIoCopy(t *testing.T) {
}

func TestHTTPClientWithoutChunkEncoding(t *testing.T) {
defer goleak.VerifyNone(t)

var (
rotatorSettings = NewRotatorSettings()
errWg sync.WaitGroup
Expand Down
8 changes: 7 additions & 1 deletion header_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package warc

import "testing"
import (
"testing"

"go.uber.org/goleak"
)

// Tests for the Header methods and NewHeader
func TestHeaderMethods(t *testing.T) {
defer goleak.VerifyNone(t)

rotatorSettings := NewRotatorSettings()

rotatorSettings.WarcinfoContent.Set("test-header", "test-value")
Expand Down
4 changes: 4 additions & 0 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strings"
"testing"

"go.uber.org/goleak"
)

func testFileHash(t *testing.T, path string) {
Expand Down Expand Up @@ -246,6 +248,8 @@ func testFileEarlyEOF(t *testing.T, path string) {
}

func TestReader(t *testing.T) {
defer goleak.VerifyNone(t)

var paths = []string{
"testdata/test.warc.gz",
}
Expand Down
6 changes: 6 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package warc
import (
"bytes"
"testing"

"go.uber.org/goleak"
)

// Tests for the GetSHA1 function
func TestGetSHA1(t *testing.T) {
defer goleak.VerifyNone(t)

helloWorldSHA1 := "FKXGYNOJJ7H3IFO35FPUBC445EPOQRXN"

if GetSHA1(bytes.NewReader([]byte("hello world"))) != helloWorldSHA1 {
Expand All @@ -16,6 +20,8 @@ func TestGetSHA1(t *testing.T) {

// Tests for the NewRotatorSettings function
func TestNewRotatorSettings(t *testing.T) {
defer goleak.VerifyNone(t)

rotatorSettings := NewRotatorSettings()

if rotatorSettings.Prefix != "WARC" {
Expand Down
14 changes: 0 additions & 14 deletions warc_test.go

This file was deleted.

0 comments on commit 15f75ef

Please sign in to comment.