func (s *GripInternalSuite) TestConditionalSend() { // because sink is an internal type (implementation of // sender,) and "GetMessage" isn't in the interface, though it // is exported, we can't pass the sink between functions. sink, err := send.NewInternalLogger(s.grip.ThresholdLevel(), s.grip.DefaultLevel()) s.NoError(err) s.grip.SetSender(sink) msg := message.NewLinesMessage("foo") msgTwo := message.NewLinesMessage("bar") // when the conditional argument is true, it should work s.grip.conditionalSend(level.Emergency, true, msg) s.Equal(sink.GetMessage().Message, msg) // when the conditional argument is true, it should work, and the channel is fifo s.grip.conditionalSend(level.Emergency, false, msgTwo) s.grip.conditionalSend(level.Emergency, true, msg) s.Equal(sink.GetMessage().Message, msg) // change the order s.grip.conditionalSend(level.Emergency, true, msg) s.grip.conditionalSend(level.Emergency, false, msgTwo) s.Equal(sink.GetMessage().Message, msg) }
func (s *GripInternalSuite) TestConditionalSendFatalDoesNotExitIfNotLoggable() { msg := message.NewLinesMessage("foo") s.grip.conditionalSendFatal(s.grip.DefaultLevel(), false, msg) s.True(level.Debug > s.grip.DefaultLevel()) s.grip.conditionalSendFatal(level.Debug, true, msg) }
func (s *GripInternalSuite) TestPanicSenderRespectsTThreshold() { s.True(level.Debug > s.grip.DefaultLevel()) // test that there is a no panic if the message isn't "logabble" defer func() { s.Nil(recover()) }() s.grip.sendPanic(level.Debug, message.NewLinesMessage("foo")) }
func (s *GripInternalSuite) TestPanicSenderActuallyPanics() { // both of these are in anonymous functions so that the defers // cover the correct area. func() { // first make sure that the defualt send method doesn't panic defer func() { s.Nil(recover()) }() s.grip.Sender().Send(s.grip.DefaultLevel(), message.NewLinesMessage("foo")) }() func() { // call a panic function with a recoverer set. defer func() { s.NotNil(recover()) }() s.grip.sendPanic(s.grip.DefaultLevel(), message.NewLinesMessage("foo")) }() }
func TestConditionalSendFatalExits(t *testing.T) { std.UseNativeLogger() if os.Getenv("SHOULD_CRASH") == "1" { std.EmergencyFatalWhen(true, message.NewLinesMessage("foo")) return } cmd := exec.Command(os.Args[0], "-test.run=TestConditionalSendFatalExits") cmd.Env = append(os.Environ(), "SHOULD_CRASH=1") err := cmd.Run() if err == nil { t.Errorf("sendFatal should have exited 1, instead: %s", err.Error()) } }
// This testing method uses the technique outlined in: // http://stackoverflow.com/a/33404435 to test a function that exits // since it's impossible to "catch" an os.Exit func TestSendFatalExits(t *testing.T) { grip := NewGrip("test") if os.Getenv("SHOULD_CRASH") == "1" { grip.sendFatal(grip.DefaultLevel(), message.NewLinesMessage("foo")) return } cmd := exec.Command(os.Args[0], "-test.run=TestSendFatalExits") cmd.Env = append(os.Environ(), "SHOULD_CRASH=1") err := cmd.Run() if err == nil { t.Errorf("sendFatal should have exited 0, instead: %s", err.Error()) } }
func (s *GripInternalSuite) TestConditionalSendPanic() { sink, err := send.NewInternalLogger(s.grip.ThresholdLevel(), s.grip.DefaultLevel()) s.NoError(err) s.grip.SetSender(sink) msg := message.NewLinesMessage("foo") // first if the conditional is false, it can't panic. s.NotPanics(func() { s.grip.conditionalSendPanic(level.Emergency, false, msg) }) // next, if the conditional is true it should panic s.Panics(func() { s.grip.conditionalSendPanic(level.Emergency, true, msg) }) }
func (g *Grip) ErrorPanicln(a ...interface{}) { g.sendPanic(level.Error, message.NewLinesMessage(a...)) }
func (g *Grip) Warningln(a ...interface{}) { g.sender.Send(level.Warning, message.NewLinesMessage(a...)) }
func (g *Grip) Noticeln(a ...interface{}) { g.sender.Send(level.Notice, message.NewLinesMessage(a...)) }
func (g *Grip) ErrorFatalln(a ...interface{}) { g.sendFatal(level.Error, message.NewLinesMessage(a...)) }
func (g *Grip) EmergencyFatalWhenln(conditional bool, msg ...interface{}) { g.conditionalSendFatal(level.Emergency, conditional, message.NewLinesMessage(msg...)) }
func (g *Grip) Debugln(a ...interface{}) { g.sender.Send(level.Debug, message.NewLinesMessage(a...)) }
func (g *Grip) ErrorPanicWhenln(conditional bool, msg ...interface{}) { g.conditionalSendPanic(level.Error, conditional, message.NewLinesMessage(msg...)) }
func (g *Grip) CriticalWhenln(conditional bool, msg ...interface{}) { g.CriticalWhen(conditional, message.NewLinesMessage(msg...)) }
func (g *Grip) EmergencyPanicln(a ...interface{}) { g.sendPanic(level.Emergency, message.NewLinesMessage(a...)) }
func (g *Grip) Emergencyln(a ...interface{}) { g.sender.Send(level.Emergency, message.NewLinesMessage(a...)) }
func (g *Grip) Defaultln(a ...interface{}) { g.sender.Send(g.sender.DefaultLevel(), message.NewLinesMessage(a...)) }
func (g *Grip) Sendln(l level.Priority, a ...interface{}) { g.sender.Send(l, message.NewLinesMessage(a...)) }
func (g *Grip) SendWhenln(conditional bool, l level.Priority, msg ...interface{}) { g.SendWhen(conditional, l, message.NewLinesMessage(msg...)) }
func (g *Grip) EmergencyFatalln(a ...interface{}) { g.sendFatal(level.Emergency, message.NewLinesMessage(a...)) }
func (g *Grip) Infoln(a ...interface{}) { g.sender.Send(level.Info, message.NewLinesMessage(a...)) }
func (g *Grip) CriticalFatalWhenln(conditional bool, msg ...interface{}) { g.conditionalSendFatal(level.Critical, conditional, message.NewLinesMessage(msg...)) }
func (g *Grip) Alertln(a ...interface{}) { g.sender.Send(level.Alert, message.NewLinesMessage(a...)) }
func (g *Grip) WarningWhenln(conditional bool, msg ...interface{}) { g.WarningWhen(conditional, message.NewLinesMessage(msg...)) }
func (g *Grip) AlertPanicln(a ...interface{}) { g.sendPanic(level.Alert, message.NewLinesMessage(a...)) }
func (g *Grip) EmergencyWhenln(conditional bool, msg ...interface{}) { g.EmergencyWhen(conditional, message.NewLinesMessage(msg...)) }
func (g *Grip) AlertFatalln(a ...interface{}) { g.sendFatal(level.Alert, message.NewLinesMessage(a...)) }
func (g *Grip) AlertWhenln(conditional bool, msg ...interface{}) { g.AlertWhen(conditional, message.NewLinesMessage(msg...)) }
func (g *Grip) Errorln(a ...interface{}) { g.sender.Send(level.Error, message.NewLinesMessage(a...)) }