Skip to content

Commit

Permalink
fix: ci lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-shin0 committed Apr 25, 2024
1 parent 5cecb1f commit 44920aa
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 50 deletions.
29 changes: 19 additions & 10 deletions autopprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"errors"
"github.com/daangn/autopprof/queryer"
"go.uber.org/mock/gomock"
"github.com/golang/mock/gomock"
"io"
"testing"
"time"
Expand All @@ -24,8 +24,9 @@ func TestStart(t *testing.T) {
{
name: "disable flags are all true",
opt: Option{
DisableCPUProf: true,
DisableMemProf: true,
DisableCPUProf: true,
DisableMemProf: true,
DisableGoroutineProf: true,
},
want: ErrDisableAllProfiling,
},
Expand Down Expand Up @@ -57,6 +58,13 @@ func TestStart(t *testing.T) {
},
want: ErrInvalidMemThreshold,
},
{
name: "invalid GoroutineThreshold value -1",
opt: Option{
GoroutineThreshold: -1,
},
want: ErrInvalidGoroutineThreshold,
},
{
name: "when given reporter is nil",
opt: Option{
Expand Down Expand Up @@ -180,7 +188,7 @@ func TestAutoPprof_loadCPUQuota(t *testing.T) {
mockQueryer := queryer.NewMockCgroupsQueryer(ctrl)
mockQueryer.EXPECT().
SetCPUQuota().
Return(ErrV2CPUQuotaUndefined)
Return(queryer.ErrV2CPUQuotaUndefined)

return &autoPprof{
cgroupQueryer: mockQueryer,
Expand All @@ -199,7 +207,7 @@ func TestAutoPprof_loadCPUQuota(t *testing.T) {
mockQueryer := queryer.NewMockCgroupsQueryer(ctrl)
mockQueryer.EXPECT().
SetCPUQuota().
Return(ErrV2CPUQuotaUndefined)
Return(queryer.ErrV2CPUQuotaUndefined)

return &autoPprof{
cgroupQueryer: mockQueryer,
Expand All @@ -208,7 +216,7 @@ func TestAutoPprof_loadCPUQuota(t *testing.T) {
}
},
wantDisableCPUProfFlag: false,
wantErr: ErrV2CPUQuotaUndefined,
wantErr: queryer.ErrV2CPUQuotaUndefined,
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -875,14 +883,15 @@ func TestAutoPprof_watchGoroutineCount(t *testing.T) {
mockReporter.EXPECT().
ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(
func(_ context.Context, _ io.Reader, _ report.MemInfo) error {
func(_ context.Context, _ io.Reader, _ report.GoroutineInfo) error {
reported = true
return nil
},
)

ap := &autoPprof{
disableCPUProf: true,
disableMemProf: true,
watchInterval: 1 * time.Second,
goroutineThreshold: 100,
runtimeQueryer: mockQueryer,
Expand Down Expand Up @@ -917,7 +926,7 @@ func TestAutoPprof_watchGoroutineCount_consecutive(t *testing.T) {
GoroutineCount().
AnyTimes().
DoAndReturn(
func() (float64, error) {
func() (int, error) {
return 200, nil
},
)
Expand All @@ -938,16 +947,16 @@ func TestAutoPprof_watchGoroutineCount_consecutive(t *testing.T) {
ReportGoroutineProfile(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().
DoAndReturn(
func(_ context.Context, _ io.Reader, _ report.MemInfo) error {
func(_ context.Context, _ io.Reader, _ report.GoroutineInfo) error {
reportedCnt++
return nil
},
)

ap := &autoPprof{
disableCPUProf: true,
disableMemProf: true,
watchInterval: 1 * time.Second,
memThreshold: 0.2, // 20%.
goroutineThreshold: 100,
minConsecutiveOverThreshold: 3,
runtimeQueryer: mockQueryer,
Expand Down
6 changes: 3 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var (
ErrInvalidMemThreshold = fmt.Errorf(
"autopprof: memory threshold value must be between 0 and 1",
)
ErrInvalidGoroutineThreshold = fmt.Errorf(
"autopprof: goroutine threshold value must be greater than to 0",
)
ErrNilReporter = fmt.Errorf("autopprof: Reporter can't be nil")
ErrDisableAllProfiling = fmt.Errorf("autopprof: all profiling is disabled")
ErrV2CPUQuotaUndefined = fmt.Errorf("autopprof: v2 cpu quota is undefined")
ErrV2CPUMaxEmpty = fmt.Errorf("autopprof: v2 cpu.max is empty")
ErrV1CPUSubsystemEmpty = fmt.Errorf("autopprof: v1 cpu subsystem is empty")
)
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.19

require (
github.com/containerd/cgroups v1.0.4
github.com/golang/mock v1.6.0
github.com/slack-go/slack v0.11.3
go.uber.org/mock v0.4.0
)

require (
Expand All @@ -17,5 +17,5 @@ require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/opencontainers/runtime-spec v1.0.2 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
)
16 changes: 12 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B
github.com/containerd/cgroups v1.0.4/go.mod h1:nLNQtsF7Sl2HxNebu77i1R0oDlhiTG+kO4JTrUzo6IA=
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
Expand All @@ -25,6 +26,7 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -38,21 +40,23 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/slack-go/slack v0.11.3 h1:GN7revxEMax4amCc3El9a+9SGnjmBvSUobs0QnO6ZO8=
github.com/slack-go/slack v0.11.3/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
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.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
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/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
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/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
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-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -69,20 +73,24 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/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 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/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.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
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-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
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=
7 changes: 6 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Option struct {
DisableCPUProf bool
// DisableMemProf disables the memory profiling.
DisableMemProf bool
// DisableGoroutineProf disables the goroutine profiling.
DisableGoroutineProf bool

// CPUThreshold is the cpu usage threshold (between 0 and 1)
// to trigger the cpu profiling.
Expand Down Expand Up @@ -51,7 +53,7 @@ type Option struct {

// NOTE(mingrammer): testing the validate() is done in autopprof_test.go.
func (o Option) validate() error {
if o.DisableCPUProf && o.DisableMemProf {
if o.DisableCPUProf && o.DisableMemProf && o.DisableGoroutineProf {
return ErrDisableAllProfiling
}
if o.CPUThreshold < 0 || o.CPUThreshold > 1 {
Expand All @@ -60,6 +62,9 @@ func (o Option) validate() error {
if o.MemThreshold < 0 || o.MemThreshold > 1 {
return ErrInvalidMemThreshold
}
if o.GoroutineThreshold < 0 {
return ErrInvalidGoroutineThreshold
}
if o.Reporter == nil {
return ErrNilReporter
}
Expand Down
8 changes: 2 additions & 6 deletions profile_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions queryer/cgroupv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package queryer

import (
"bufio"
"github.com/daangn/autopprof"
"os"
"path"
"strconv"
Expand Down Expand Up @@ -130,5 +129,5 @@ func (c *cgroupV1) parseCPU(filename string) (int, error) {
if err := scanner.Err(); err != nil {
return 0, err
}
return 0, autopprof.ErrV1CPUSubsystemEmpty
return 0, ErrV1CPUSubsystemEmpty
}
7 changes: 3 additions & 4 deletions queryer/cgroupv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package queryer
import (
"bufio"
"fmt"
"github.com/daangn/autopprof"
"os"
"path"
"strconv"
Expand Down Expand Up @@ -86,7 +85,7 @@ func (c *cgroupV2) SetCPUQuota() error {
path.Join(c.mountPoint, c.cpuMaxFile),
)
if os.IsNotExist(err) {
return autopprof.ErrV2CPUQuotaUndefined
return ErrV2CPUQuotaUndefined
}
if err != nil {
return err
Expand All @@ -101,7 +100,7 @@ func (c *cgroupV2) SetCPUQuota() error {
)
}
if fields[0] == cgroupV2CPUMaxQuotaMax {
return autopprof.ErrV2CPUQuotaUndefined
return ErrV2CPUQuotaUndefined
}

max, err := strconv.Atoi(fields[0])
Expand All @@ -122,7 +121,7 @@ func (c *cgroupV2) SetCPUQuota() error {
if err := scanner.Err(); err != nil {
return err
}
return autopprof.ErrV2CPUMaxEmpty
return ErrV2CPUMaxEmpty
}

func (c *cgroupV2) snapshotCPUUsage(usage uint64) {
Expand Down
5 changes: 4 additions & 1 deletion queryer/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ import "fmt"

// Errors.
var (
ErrCgroupsUnavailable = fmt.Errorf("autopprof: cgroups is unavailable")
ErrCgroupsUnavailable = fmt.Errorf("autopprof: cgroups is unavailable")
ErrV2CPUQuotaUndefined = fmt.Errorf("autopprof: v2 cpu quota is undefined")
ErrV2CPUMaxEmpty = fmt.Errorf("autopprof: v2 cpu.max is empty")
ErrV1CPUSubsystemEmpty = fmt.Errorf("autopprof: v1 cpu subsystem is empty")
)
10 changes: 3 additions & 7 deletions queryer/queryer_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions report/report_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 44920aa

Please sign in to comment.