github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/agent/comm/logger_test.go (about) 1 package comm 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/evergreen-ci/evergreen/model" 8 "github.com/mongodb/grip/send" 9 "github.com/mongodb/grip/slogger" 10 . "github.com/smartystreets/goconvey/convey" 11 ) 12 13 func TestLogging(t *testing.T) { 14 var apiLogger *APILogger 15 var taskCommunicator *MockCommunicator 16 var testLogger slogger.Logger 17 Convey("With a remote logging appender", t, func() { 18 taskCommunicator = &MockCommunicator{ 19 LogChan: make(chan []model.LogMessage, 100), 20 } 21 apiLogger = NewAPILogger(taskCommunicator) 22 23 testLogger = slogger.Logger{ 24 Name: "", 25 Appenders: []send.Sender{slogger.WrapAppender(apiLogger)}, 26 } 27 28 Convey("Logging fewer msgs than threshold should not flush", func() { 29 for i := 0; i < apiLogger.SendAfterLines-1; i++ { 30 testLogger.Logf(slogger.INFO, "test %v", i) 31 } 32 33 select { 34 case _, ok := <-taskCommunicator.LogChan: 35 So(ok, ShouldBeFalse) 36 default: 37 So(true, ShouldBeTrue) 38 } 39 Convey("Logging beyond the threshold should trigger a flush", func() { 40 testLogger.Logf(slogger.INFO, "test %v", 10) 41 time.Sleep(10 * time.Millisecond) 42 receivedMsgs, ok := <-taskCommunicator.LogChan 43 So(ok, ShouldBeTrue) 44 So(len(receivedMsgs), ShouldEqual, apiLogger.SendAfterLines) 45 So(len(apiLogger.messages), ShouldEqual, 0) 46 }) 47 }) 48 49 Convey("Calling flush() directly should trigger a flush", func() { 50 testLogger.Logf(slogger.INFO, "test %v", 11) 51 time.Sleep(10 * time.Millisecond) 52 apiLogger.Flush() 53 54 receivedMsgs, ok := <-taskCommunicator.LogChan 55 So(ok, ShouldBeTrue) 56 So(len(receivedMsgs), ShouldEqual, 1) 57 }) 58 59 Convey("Calling flush() when empty should not send anything", func() { 60 apiLogger.Flush() 61 time.Sleep(10 * time.Millisecond) 62 select { 63 case _, ok := <-taskCommunicator.LogChan: 64 So(ok, ShouldBeFalse) 65 default: 66 So(true, ShouldBeTrue) 67 } 68 }) 69 70 }) 71 } 72 73 func TestCommandLogger(t *testing.T) { 74 Convey("With an CommandLogger", t, func() { 75 76 var logger *StreamLogger 77 var commandLogger *CommandLogger 78 79 Convey("logging via the CommandLogger should add the command"+ 80 " name to the front of the message", func() { 81 sender := send.MakeInternalLogger() 82 83 logger = &StreamLogger{ 84 Local: &slogger.Logger{ 85 Name: "test", 86 Appenders: []send.Sender{sender}, 87 }, 88 } 89 90 commandLogger = &CommandLogger{ 91 commandName: "test", 92 logger: logger, 93 } 94 95 commandLogger.LogLocal(slogger.INFO, "Test %v", 1) 96 commandLogger.LogLocal(slogger.INFO, "Test %v", "2") 97 So(sender.Len(), ShouldEqual, 2) 98 So(sender.GetMessage().Rendered, ShouldEndWith, "[test] Test 1") 99 So(sender.GetMessage().Rendered, ShouldEndWith, "[test] Test 2") 100 }) 101 102 }) 103 }