github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/util/recover_test.go (about)

     1  package util
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/mongodb/grip"
     9  	"github.com/mongodb/grip/send"
    10  	. "github.com/smartystreets/goconvey/convey"
    11  )
    12  
    13  func TestRecoverHandler(t *testing.T) {
    14  	if runtime.Compiler == "gccgo" && runtime.GOARCH == "s390x" {
    15  		t.Skip("test encounters runtime bug on gccgo on zLinux; see EVG-1689")
    16  	}
    17  	grip.Error(os.Setenv("EVERGREEN_TEST", "true"))
    18  
    19  	Convey("Recover handler should handle panics", t, func() {
    20  		Convey("without a panic there should be no log messages", func() {
    21  			sender := send.MakeInternalLogger()
    22  			So(grip.SetSender(sender), ShouldBeNil)
    23  			RecoverAndLogStackTrace()
    24  			So(grip.SetSender(send.MakeNative()), ShouldBeNil)
    25  			msg := sender.GetMessage()
    26  			So(msg, ShouldBeNil)
    27  
    28  		})
    29  
    30  		Convey("with a panic there should be log messages", func() {
    31  			sender := send.MakeInternalLogger()
    32  			So(grip.SetSender(sender), ShouldBeNil)
    33  
    34  			func() {
    35  				defer RecoverAndLogStackTrace()
    36  				panic("sorry")
    37  			}()
    38  
    39  			msgs := []interface{}{}
    40  			So(grip.SetSender(send.MakeNative()), ShouldBeNil)
    41  
    42  			for {
    43  				m := sender.GetMessage()
    44  				if m == nil {
    45  					break
    46  				}
    47  
    48  				msgs = append(msgs, m)
    49  			}
    50  
    51  			So(len(msgs), ShouldBeGreaterThan, 2)
    52  		})
    53  
    54  	})
    55  }