mirror of https://github.com/perkeep/perkeep.git
Add pkg/leak, to help find leaks.
Change-Id: I52b15f168f63c5ac40c40f258d1f3ff2ffd4965e
This commit is contained in:
parent
0375ee2d47
commit
45034814d8
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
Copyright 2013 Google Inc.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package leak
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Checker checks for leaks.
|
||||||
|
type Checker struct {
|
||||||
|
pc []uintptr // nil once closed
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewChecker returns a Checker, remembering the stack trace.
|
||||||
|
func NewChecker() *Checker {
|
||||||
|
pc := make([]uintptr, 50)
|
||||||
|
ch := &Checker{pc[:runtime.Callers(0, pc)]}
|
||||||
|
runtime.SetFinalizer(ch, (*Checker).finalize)
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Checker) Close() {
|
||||||
|
if c != nil {
|
||||||
|
c.pc = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Checker) finalize() {
|
||||||
|
if c == nil || c.pc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
nTestLeaks++ // for testing
|
||||||
|
var buf bytes.Buffer
|
||||||
|
buf.WriteString("Leak at:\n")
|
||||||
|
for _, pc := range c.pc {
|
||||||
|
f := runtime.FuncForPC(pc)
|
||||||
|
if f == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
file, line := f.FileLine(f.Entry())
|
||||||
|
fmt.Fprintf(&buf, " %s:%d\n", file, line)
|
||||||
|
}
|
||||||
|
log.Println(buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
var nTestLeaks int
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
Copyright 2013 Google Inc.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package leak
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLeak(t *testing.T) {
|
||||||
|
testLeak(t, false, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNoLeak(t *testing.T) {
|
||||||
|
testLeak(t, true, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testLeak(t *testing.T, close bool, want int) {
|
||||||
|
c := make(chan bool)
|
||||||
|
go func() {
|
||||||
|
ch := NewChecker()
|
||||||
|
if close {
|
||||||
|
ch.Close()
|
||||||
|
}
|
||||||
|
c <- true
|
||||||
|
}()
|
||||||
|
<-c
|
||||||
|
leak0 := nTestLeaks
|
||||||
|
runtime.GC()
|
||||||
|
leaks := nTestLeaks - leak0
|
||||||
|
if leaks != want {
|
||||||
|
t.Errorf("got %d leaks; want %d", want)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue