github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/logging/loggers/channel_logger_test.go (about) 1 // Copyright Monax Industries Limited 2 // SPDX-License-Identifier: Apache-2.0 3 4 package loggers 5 6 import ( 7 "testing" 8 9 "time" 10 11 "fmt" 12 13 "github.com/eapache/channels" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestChannelLogger(t *testing.T) { 18 loggingRingBufferCap := channels.BufferCap(5) 19 cl := NewChannelLogger(loggingRingBufferCap) 20 21 // Push a larger number of log messages than will fit into ring buffer 22 for i := 0; i < int(loggingRingBufferCap)+10; i++ { 23 cl.Log("log line", i) 24 } 25 26 // Observe that oldest 10 messages are overwritten (so first message is 10) 27 for i := 0; i < int(loggingRingBufferCap); i++ { 28 ll := cl.WaitReadLogLine() 29 assert.Equal(t, 10+i, ll[1]) 30 } 31 32 assert.Nil(t, cl.ReadLogLine(), "Since we have drained the buffer there "+ 33 "should be no more log lines.") 34 } 35 36 func TestChannelLogger_Reset(t *testing.T) { 37 loggingRingBufferCap := channels.BufferCap(5) 38 cl := NewChannelLogger(loggingRingBufferCap) 39 for i := 0; i < int(loggingRingBufferCap); i++ { 40 cl.Log("log line", i) 41 } 42 cl.Reset() 43 for i := 0; i < int(loggingRingBufferCap); i++ { 44 cl.Log("log line", i) 45 } 46 for i := 0; i < int(loggingRingBufferCap); i++ { 47 ll := cl.WaitReadLogLine() 48 assert.Equal(t, i, ll[1]) 49 } 50 assert.Nil(t, cl.ReadLogLine(), "Since we have drained the buffer there "+ 51 "should be no more log lines.") 52 } 53 54 func TestNonBlockingLogger(t *testing.T) { 55 tl := newTestLogger() 56 nbl, _ := NonBlockingLogger(tl) 57 nbl.Log("Foo", "Bar") 58 nbl.Log("Baz", "Bur") 59 nbl.Log("Badger", "Romeo") 60 time.Sleep(time.Second) 61 62 lls, err := tl.logLines(3) 63 assert.NoError(t, err) 64 assert.Equal(t, logLines("Foo", "Bar", "", 65 "Baz", "Bur", "", 66 "Badger", "Romeo"), lls) 67 } 68 69 func TestNonBlockingLoggerErrors(t *testing.T) { 70 el := newErrorLogger("Should surface") 71 nbl, errCh := NonBlockingLogger(el) 72 nbl.Log("failure", "true") 73 assert.Equal(t, "Should surface", 74 fmt.Sprintf("%s", <-errCh.Out())) 75 }