Skip to content

Commit 6f9f1a1

Browse files
committed
fix up tests; improved error handling throughout
1 parent 12d316a commit 6f9f1a1

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ linters:
66
enable:
77
- errcheck
88
- goimports
9-
- golint
109
- govet
1110
- staticcheck
1211

profile.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929

3030
// FinalizerFunc is a function that is invokved during the teardown period
3131
// of the profiling instance.
32-
type FinalizerFunc func()
32+
type FinalizerFunc func() error
3333

3434
// CallbackFunc is a function that can be supplied with the
3535
// WithCallback option to be executed when the profiling instance
@@ -90,7 +90,9 @@ func (p *Profiler) Stop() {
9090
if !atomic.CompareAndSwapUint32(&profilingActive, 1, 0) {
9191
die("profiler instance was not started")
9292
}
93-
p.finalizer()
93+
if err := p.finalizer(); err != nil {
94+
die(err.Error())
95+
}
9496
if p.callback != nil {
9597
p.callback(p)
9698
}

profile_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ func TestProfilesEnabledExpectedOutput(t *testing.T) {
2121
if err != nil {
2222
t.Fatal(err)
2323
}
24-
defer os.RemoveAll(storage)
24+
defer func() {
25+
if err := os.RemoveAll(storage); err != nil {
26+
t.Error("problem removing file", err)
27+
}
28+
}()
2529
tests := map[string]struct {
2630
source string
2731
checks []CheckFunc
@@ -102,25 +106,25 @@ func createTempTestFile(t *testing.T, source string) (string, func()) {
102106
go 1.23.2
103107
require github.com/symonk/profiler v0.0.0-20241021143805-788e1dbe92a9
104108
`
105-
os.WriteFile(mod, []byte(contents), 0644)
109+
if err := os.WriteFile(mod, []byte(contents), 0644); err != nil {
110+
t.Error("failed to write file", err)
111+
}
106112

107113
// create the appropriate mod file etc
108-
return main, func() { defer os.RemoveAll(dir) }
114+
return main, func() {
115+
defer func() {
116+
if err := os.RemoveAll(dir); err != nil {
117+
t.Error("failed to remove dir", err)
118+
}
119+
}()
120+
}
109121
}
110122

111123
// Check function implementations for asserting against the responses
112124
func exitedZero(t *testing.T, _, _ string, code int) {
113125
assert.Zero(t, code)
114126
}
115127

116-
// patternMatchLines checks that the lines in stdout matched
117-
func stdOutOutMatchLines(patterns ...string) CheckFunc {
118-
return func(t *testing.T, stdout, stderr string, exit int) {
119-
assert.NotEmpty(t, stdout)
120-
patternMatchLines(t, stdout, patterns...)
121-
}
122-
}
123-
124128
// patternMatchLines checks that the lines in stderr matched
125129
func stdErrOutMatchLines(patterns ...string) CheckFunc {
126130
return func(t *testing.T, stdout, stderr string, exit int) {
@@ -156,10 +160,6 @@ func patternMatchLines(t *testing.T, input string, patterns ...string) bool {
156160
return false
157161
}
158162

159-
func emptyStdErr(t *testing.T, _, stderr string, _ int) {
160-
assert.Empty(t, stderr)
161-
}
162-
163163
func emptyStdOut(t *testing.T, stdout, _ string, _ int) {
164164
assert.Empty(t, stdout)
165165
}

strategy.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,85 +33,91 @@ func cpuStrategyFn(p *Profiler) (FinalizerFunc, error) {
3333
if err := pprof.StartCPUProfile(p.profileFile); err != nil {
3434
return nil, err
3535
}
36-
return func() {
36+
return func() (err error) {
37+
defer func() { err = p.profileFile.Close() }()
3738
pprof.StopCPUProfile()
38-
p.profileFile.Close()
39+
return nil
3940
}, nil
4041
}
4142

4243
func heapStrategyFn(p *Profiler) (FinalizerFunc, error) {
4344
rate := runtime.MemProfileRate
4445
p.SetProfileFile(MemoryFileName)
4546
runtime.MemProfileRate = p.memoryProfileRate
46-
return func() {
47+
return func() (err error) {
48+
defer func() { runtime.MemProfileRate = rate }()
49+
defer func() { err = p.profileFile.Close() }()
4750
_ = pprof.Lookup(heapProfileName).WriteTo(p.profileFile, 0)
4851
runtime.GC()
49-
p.profileFile.Close()
50-
runtime.MemProfileRate = rate
52+
return nil
5153
}, nil
5254
}
5355

5456
func allocStrategyFn(p *Profiler) (FinalizerFunc, error) {
5557
rate := runtime.MemProfileRate
5658
p.SetProfileFile(MemoryFileName)
5759
runtime.MemProfileRate = p.memoryProfileRate
58-
return func() {
60+
return func() (err error) {
61+
defer func() { runtime.MemProfileRate = rate }()
62+
defer func() { err = p.profileFile.Close() }()
5963
_ = pprof.Lookup(allocProfileName).WriteTo(p.profileFile, 0)
6064
runtime.GC()
61-
p.profileFile.Close()
62-
runtime.MemProfileRate = rate
65+
return nil
6366
}, nil
6467
}
6568

6669
func mutexStrategyFn(p *Profiler) (FinalizerFunc, error) {
6770
p.SetProfileFile(MutexFileName)
6871
_ = pprof.Lookup("mutex").WriteTo(p.profileFile, 0)
69-
return func() {
70-
p.profileFile.Close()
72+
return func() error {
73+
return p.profileFile.Close()
7174
}, nil
7275
}
7376

7477
func blockStrategyFn(p *Profiler) (FinalizerFunc, error) {
7578
p.SetProfileFile(BlockFileName)
7679
// for now, we do not allow customising the runtime.SetBlockProfileRate
7780
// if it is useful in future, change is welcome here.
78-
return func() {
81+
return func() error {
82+
defer runtime.SetBlockProfileRate(0)
7983
_ = pprof.Lookup("block").WriteTo(p.profileFile, 0)
80-
p.profileFile.Close()
81-
runtime.SetBlockProfileRate(0)
84+
return p.profileFile.Close()
8285
}, nil
8386
}
8487

8588
func goroutineStrategyFn(p *Profiler) (FinalizerFunc, error) {
8689
p.SetProfileFile(GoroutineFileName)
8790
_ = pprof.Lookup("goroutine").WriteTo(p.profileFile, 0)
88-
return func() {
89-
p.profileFile.Close()
91+
return func() error {
92+
return p.profileFile.Close()
9093
}, nil
9194
}
9295

9396
func threadCreateStrategyFn(p *Profiler) (FinalizerFunc, error) {
9497
p.SetProfileFile(ThreadCreateFileName)
95-
return func() {
98+
return func() (err error) {
99+
defer func() { err = p.profileFile.Close() }()
96100
_ = pprof.Lookup("threadcreate").WriteTo(p.profileFile, 0)
97-
p.profileFile.Close()
101+
return nil
98102
}, nil
99103
}
100104

101105
func traceStrategyFn(p *Profiler) (FinalizerFunc, error) {
102106
p.SetProfileFile(TraceFileName)
103-
trace.Start(p.profileFile)
104-
return func() {
105-
p.profileFile.Close()
107+
if err := trace.Start(p.profileFile); err != nil {
108+
return nil, err
109+
}
110+
return func() error {
106111
trace.Stop()
112+
return nil
107113
}, nil
108114
}
109115

110116
func clockStrategyFn(p *Profiler) (FinalizerFunc, error) {
111117
p.SetProfileFile(ClockFileName)
112118
teardown := fgprof.Start(p.profileFile, fgprof.FormatPprof)
113-
return func() {
114-
teardown()
115-
p.profileFile.Close()
119+
return func() (err error) {
120+
defer func() { err = p.profileFile.Close() }()
121+
return teardown()
116122
}, nil
117123
}

0 commit comments

Comments
 (0)