From 236c97d279d0dc8a1d927688b64b5357275ab838 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 24 Oct 2016 17:07:55 +0100 Subject: [PATCH] Tests and comments --- .../matrix-org/go-neb/polling/polling.go | 2 ++ .../matrix-org/go-neb/server/server_test.go | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/github.com/matrix-org/go-neb/server/server_test.go diff --git a/src/github.com/matrix-org/go-neb/polling/polling.go b/src/github.com/matrix-org/go-neb/polling/polling.go index d4b6bbc..0a84a92 100644 --- a/src/github.com/matrix-org/go-neb/polling/polling.go +++ b/src/github.com/matrix-org/go-neb/polling/polling.go @@ -74,6 +74,8 @@ func pollLoop(service types.Service, ts int64) { }) defer func() { + // Kill the poll loop entirely as it is likely that whatever made us panic will + // make us panic again. We can whine bitterly about it though. if r := recover(); r != nil { logger.WithField("panic", r).Errorf( "pollLoop panicked!\n%s", debug.Stack(), diff --git a/src/github.com/matrix-org/go-neb/server/server_test.go b/src/github.com/matrix-org/go-neb/server/server_test.go new file mode 100644 index 0000000..920d0ae --- /dev/null +++ b/src/github.com/matrix-org/go-neb/server/server_test.go @@ -0,0 +1,29 @@ +package server + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestProtect(t *testing.T) { + mockWriter := httptest.NewRecorder() + mockReq, _ := http.NewRequest("GET", "http://example.com/foo", nil) + h := Protect(func(w http.ResponseWriter, req *http.Request) { + var array []string + w.Write([]byte(array[5])) // NPE + }) + + h(mockWriter, mockReq) + + expectCode := 500 + if mockWriter.Code != expectCode { + t.Errorf("TestProtect wanted HTTP status %d, got %d", expectCode, mockWriter.Code) + } + + expectBody := `{"message":"Internal Server Error"}` + actualBody := mockWriter.Body.String() + if actualBody != expectBody { + t.Errorf("TestProtect wanted body %s, got %s", expectBody, actualBody) + } +}