From d47fd328898d60ecd4f29d58031abc0d08e6259e Mon Sep 17 00:00:00 2001 From: Fabian Holler Date: Tue, 19 Apr 2022 13:07:28 +0200 Subject: [PATCH 1/2] tests: detect go-routine leaks in integration testcases The goleak detector from uber is now run on the end of an integration test run. If the package or a testcase does not terminate all go-routines it started, it will now cause testcase failures. This will help to detect go-routine leaks in the package. --- go.mod | 2 ++ go.sum | 44 ++++++++++++++++++++++++++++++++++++++++++++ integration_test.go | 6 ++++++ 3 files changed, 52 insertions(+) create mode 100644 go.sum diff --git a/go.mod b/go.mod index d26956a..e1bdee9 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/rabbitmq/amqp091-go go 1.16 + +require go.uber.org/goleak v1.1.12 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7feb0be --- /dev/null +++ b/go.sum @@ -0,0 +1,44 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/integration_test.go b/integration_test.go index 70ad414..08a395d 100644 --- a/integration_test.go +++ b/integration_test.go @@ -24,6 +24,8 @@ import ( "testing" "testing/quick" "time" + + "go.uber.org/goleak" ) func TestIntegrationOpenClose(t *testing.T) { @@ -1967,3 +1969,7 @@ func generateCrc32Random(size int) []byte { return msg } + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} From 6ee394e0cfe636772d3cb2ced263d009aab5397a Mon Sep 17 00:00:00 2001 From: Fabian Holler Date: Tue, 19 Apr 2022 13:02:30 +0200 Subject: [PATCH 2/2] tests: always close connections in integration testcases The goleak detector that runs for all integration tests found that multiple testcases were leaking go-routines because the did not close the connections they opened. Close the connection via t.Cleanup() in those testcases. --- connection_test.go | 3 +++ integration_test.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/connection_test.go b/connection_test.go index 167487d..d9a16b6 100644 --- a/connection_test.go +++ b/connection_test.go @@ -18,6 +18,7 @@ import ( func TestRequiredServerLocale(t *testing.T) { conn := integrationConnection(t, "AMQP 0-9-1 required server locale") + t.Cleanup(func() { conn.Close() }) requiredServerLocale := defaultLocale for _, locale := range conn.Locales { @@ -31,6 +32,7 @@ func TestRequiredServerLocale(t *testing.T) { func TestDefaultConnectionLocale(t *testing.T) { conn := integrationConnection(t, "client default locale") + t.Cleanup(func() { conn.Close() }) if conn.Config.Locale != defaultLocale { t.Fatalf("Expected default connection locale to be %s, is was: %s", defaultLocale, conn.Config.Locale) @@ -198,6 +200,7 @@ func TestIsClosed(t *testing.T) { // TestChannelIsClosed will test the public method IsClosed on a channel. func TestChannelIsClosed(t *testing.T) { conn := integrationConnection(t, "public channel.IsClosed()") + t.Cleanup(func() { conn.Close() }) ch, _ := conn.Channel() if ch.IsClosed() { diff --git a/integration_test.go b/integration_test.go index 08a395d..5aebf20 100644 --- a/integration_test.go +++ b/integration_test.go @@ -1424,6 +1424,8 @@ func TestDeadlockConsumerIssue48(t *testing.T) { func TestRepeatedChannelExceptionWithPublishAndMaxProcsIssue46(t *testing.T) { conn := integrationConnection(t, "issue46") if conn != nil { + t.Cleanup(func() { conn.Close() }) + for i := 0; i < 100; i++ { ch, err := conn.Channel() if err != nil { @@ -1446,6 +1448,8 @@ func TestRepeatedChannelExceptionWithPublishAndMaxProcsIssue46(t *testing.T) { func TestChannelExceptionWithCloseIssue43(t *testing.T) { conn := integrationConnection(t, "issue43") if conn != nil { + t.Cleanup(func() { conn.Close() }) + go func() { for err := range conn.NotifyClose(make(chan *Error)) { t.Log(err.Error())