Skip to content

Commit 6da020a

Browse files
committed
Add goleak in unit tests
1 parent 689bcb0 commit 6da020a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+371
-8
lines changed

backend/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/rotisserie/eris v0.5.4
2222
github.com/stretchr/testify v1.8.4
2323
github.com/vrischmann/envconfig v1.3.0
24+
go.uber.org/goleak v1.3.0
2425
golang.org/x/crypto v0.11.0
2526
gorm.io/datatypes v1.2.0
2627
gorm.io/driver/mysql v1.5.1

backend/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
145145
github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 h1:qXafrlZL1WsJW5OokjraLLRURHiw0OzKHD/RNdspp4w=
146146
gitlab.com/jc21com/sqlite v1.22.2-0.20230527022643-b56cedb3bc85 h1:NPHauobrOymc80Euu+e0tsMyXcdtLCX5bQPKX5zsI38=
147147
gitlab.com/jc21com/sqlite v1.22.2-0.20230527022643-b56cedb3bc85/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk=
148+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
149+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
148150
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
149151
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
150152
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=

backend/internal/acme/acmesh_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import (
99
"npm/internal/entity/dnsprovider"
1010

1111
"github.com/stretchr/testify/assert"
12+
"go.uber.org/goleak"
1213
)
1314

1415
// TODO configurable
1516
const acmeLogFile = "/data/logs/acme.sh.log"
1617

1718
func TestBuildCertRequestArgs(t *testing.T) {
19+
// goleak is used to detect goroutine leaks
20+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
21+
1822
type want struct {
1923
args []string
2024
err error
@@ -195,6 +199,9 @@ func TestBuildCertRequestArgs(t *testing.T) {
195199
}
196200

197201
func TestGetAcmeShFilePath(t *testing.T) {
202+
// goleak is used to detect goroutine leaks
203+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
204+
198205
t.Run("basic test", func(t *testing.T) {
199206
path, err := getAcmeShFilePath()
200207
if err != nil {
@@ -207,6 +214,9 @@ func TestGetAcmeShFilePath(t *testing.T) {
207214
}
208215

209216
func TestGetCommonEnvVars(t *testing.T) {
217+
// goleak is used to detect goroutine leaks
218+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
219+
210220
t.Run("basic test", func(t *testing.T) {
211221
t.Setenv("ACMESH_CONFIG_HOME", "/data/.acme.sh/config")
212222
t.Setenv("ACMESH_HOME", "/data/.acme.sh")
@@ -227,6 +237,9 @@ func TestGetCommonEnvVars(t *testing.T) {
227237
}
228238

229239
func TestGetAcmeShVersion(t *testing.T) {
240+
// goleak is used to detect goroutine leaks
241+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
242+
230243
t.Run("basic test", func(t *testing.T) {
231244
resp := GetAcmeShVersion()
232245
// Seems like a pointless test, however when this is run in CI

backend/internal/api/context/context_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"go.uber.org/goleak"
78
)
89

910
func TestGetString(t *testing.T) {
11+
// goleak is used to detect goroutine leaks
12+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
13+
1014
t.Run("basic test", func(t *testing.T) {
1115
assert.Equal(t, "context value: Body", BodyCtxKey.String())
1216
})

backend/internal/api/http/responses_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import (
1010

1111
"github.com/qri-io/jsonschema"
1212
"github.com/stretchr/testify/assert"
13+
"go.uber.org/goleak"
1314
)
1415

1516
func TestResultResponseJSON(t *testing.T) {
17+
// goleak is used to detect goroutine leaks
18+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
19+
1620
tests := []struct {
1721
name string
1822
status int
@@ -67,6 +71,9 @@ func TestResultResponseJSON(t *testing.T) {
6771
}
6872

6973
func TestResultSchemaErrorJSON(t *testing.T) {
74+
// goleak is used to detect goroutine leaks
75+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
76+
7077
tests := []struct {
7178
name string
7279
given []jsonschema.KeyError
@@ -104,6 +111,9 @@ func TestResultSchemaErrorJSON(t *testing.T) {
104111
}
105112

106113
func TestResultErrorJSON(t *testing.T) {
114+
// goleak is used to detect goroutine leaks
115+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
116+
107117
tests := []struct {
108118
name string
109119
status int
@@ -146,6 +156,9 @@ func TestResultErrorJSON(t *testing.T) {
146156
}
147157

148158
func TestNotFound(t *testing.T) {
159+
// goleak is used to detect goroutine leaks
160+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
161+
149162
t.Run("basic test", func(t *testing.T) {
150163
r := httptest.NewRequest(http.MethodGet, "/anything", nil)
151164
w := httptest.NewRecorder()
@@ -163,6 +176,9 @@ func TestNotFound(t *testing.T) {
163176
}
164177

165178
func TestResultResponseText(t *testing.T) {
179+
// goleak is used to detect goroutine leaks
180+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
181+
166182
t.Run("basic test", func(t *testing.T) {
167183
r := httptest.NewRequest(http.MethodGet, "/anything", nil)
168184
w := httptest.NewRecorder()

backend/internal/api/middleware/access_control_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import (
88
"npm/internal/api/middleware"
99

1010
"github.com/stretchr/testify/assert"
11+
"go.uber.org/goleak"
1112
)
1213

1314
func TestAccessControl(t *testing.T) {
15+
// goleak is used to detect goroutine leaks
16+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
17+
1418
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1519
w.WriteHeader(http.StatusOK)
1620
})

backend/internal/api/middleware/body_context_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import (
1010
"npm/internal/api/middleware"
1111

1212
"github.com/stretchr/testify/assert"
13+
"go.uber.org/goleak"
1314
)
1415

1516
func TestBodyContext(t *testing.T) {
17+
// goleak is used to detect goroutine leaks
18+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
19+
1620
// Create a test request with a body
1721
body := []byte(`{"name": "John", "age": 30}`)
1822
req, err := http.NewRequest("POST", "/test", bytes.NewBuffer(body))

backend/internal/api/middleware/enforce_setup_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9+
"go.uber.org/goleak"
910

1011
"npm/internal/api/middleware"
1112
"npm/internal/config"
1213
)
1314

1415
func TestEnforceSetup(t *testing.T) {
16+
// goleak is used to detect goroutine leaks
17+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
18+
1519
tests := []struct {
1620
name string
1721
shouldBeSetup bool

backend/internal/api/middleware/expansion_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import (
1010
"npm/internal/api/middleware"
1111

1212
"github.com/stretchr/testify/assert"
13+
"go.uber.org/goleak"
1314
)
1415

1516
func TestExpansion(t *testing.T) {
17+
// goleak is used to detect goroutine leaks
18+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
19+
1620
t.Run("with expand query param", func(t *testing.T) {
1721
req, err := http.NewRequest("GET", "/path?expand=item1,item2", nil)
1822
assert.NoError(t, err)
@@ -47,6 +51,9 @@ func TestExpansion(t *testing.T) {
4751
}
4852

4953
func TestGetExpandFromContext(t *testing.T) {
54+
// goleak is used to detect goroutine leaks
55+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
56+
5057
t.Run("with context value", func(t *testing.T) {
5158
req, err := http.NewRequest("GET", "/path", nil)
5259
assert.NoError(t, err)

backend/internal/api/middleware/list_query_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ import (
1313
"npm/internal/tags"
1414

1515
"github.com/stretchr/testify/assert"
16+
"go.uber.org/goleak"
1617
)
1718

1819
func TestListQuery(t *testing.T) {
20+
// goleak is used to detect goroutine leaks
21+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
22+
1923
tests := []struct {
2024
name string
2125
queryParams string
@@ -61,6 +65,9 @@ func TestListQuery(t *testing.T) {
6165
}
6266

6367
func TestGetFiltersFromContext(t *testing.T) {
68+
// goleak is used to detect goroutine leaks
69+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
70+
6471
req, err := http.NewRequest("GET", "/test", nil)
6572
assert.NoError(t, err)
6673

@@ -75,6 +82,9 @@ func TestGetFiltersFromContext(t *testing.T) {
7582
}
7683

7784
func TestGetSortFromContext(t *testing.T) {
85+
// goleak is used to detect goroutine leaks
86+
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
87+
7888
req, err := http.NewRequest("GET", "/test", nil)
7989
assert.NoError(t, err)
8090

0 commit comments

Comments
 (0)