Adds recovery test
This commit is contained in:
73
recovery_test.go
Normal file
73
recovery_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package narco
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
type RecovererSuite struct {
|
||||
*NarcoSuite
|
||||
|
||||
recov *Recoverer
|
||||
buffer bytes.Buffer
|
||||
rec *httptest.ResponseRecorder
|
||||
req *http.Request
|
||||
URL string
|
||||
}
|
||||
|
||||
var _ = Suite(&RecovererSuite{NarcoSuite: NewNarcoSuite()})
|
||||
|
||||
func (s *RecovererSuite) SetUpSuite(c *C) {
|
||||
|
||||
s.recov = NewRecoverer()
|
||||
s.recov.Logger = log.New(&s.buffer, "[narco] ", log.LstdFlags)
|
||||
s.chain = s.chain.Append(s.recov.Wrap())
|
||||
|
||||
c.Assert(s.chain, NotNil)
|
||||
}
|
||||
|
||||
func (s *RecovererSuite) SetUpTest(c *C) {
|
||||
s.buffer.Reset()
|
||||
s.rec = httptest.NewRecorder()
|
||||
var err error
|
||||
s.URL = "http://" + httptest.DefaultRemoteAddr + "/"
|
||||
s.req, err = http.NewRequest("GET", s.URL, nil)
|
||||
c.Assert(err, IsNil, Commentf("Unexpected error :%s", err))
|
||||
}
|
||||
|
||||
func (s *RecovererSuite) TestRecoversFromPanic(c *C) {
|
||||
// this should not panic
|
||||
defer func() {
|
||||
err := recover()
|
||||
c.Assert(err, IsNil, Commentf("It should not have panic: %s", err))
|
||||
}()
|
||||
|
||||
s.ServeHTTP(s.rec, s.req, func(rw http.ResponseWriter, req *http.Request) {
|
||||
panic("foo")
|
||||
})
|
||||
|
||||
lines := strings.Split(s.buffer.String(), "\n")
|
||||
|
||||
c.Assert(lines[0], Matches, `\[narco\] .* `+"PANIC: foo")
|
||||
for i, l := range lines {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
matchString := "foo"
|
||||
if i == 1 {
|
||||
matchString = `goroutine [0-9]+ (\[running\])?:`
|
||||
} else if i%2 == 0 {
|
||||
//either function call, empty or created by line
|
||||
matchString = `(.*\(.*\)|created by .*|)`
|
||||
} else {
|
||||
//function location, or <autogenerated>
|
||||
matchString = `\t(.*\.go|\<autogenerated\>):[0-9]+ .*`
|
||||
}
|
||||
c.Assert(l, Matches, matchString, Commentf("On line %d", i))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user