github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/logger/loggerutils/cache/log_cache_test.go (about) 1 package cache 2 3 import ( 4 "context" 5 "testing" 6 7 "time" 8 9 "bytes" 10 11 "github.com/demonoid81/moby/daemon/logger" 12 "gotest.tools/v3/assert" 13 "gotest.tools/v3/assert/cmp" 14 ) 15 16 type fakeLogger struct { 17 messages chan logger.Message 18 close chan struct{} 19 } 20 21 func (l *fakeLogger) Log(msg *logger.Message) error { 22 select { 23 case l.messages <- *msg: 24 case <-l.close: 25 } 26 logger.PutMessage(msg) 27 return nil 28 } 29 30 func (l *fakeLogger) Name() string { 31 return "fake" 32 } 33 34 func (l *fakeLogger) Close() error { 35 close(l.close) 36 return nil 37 } 38 39 func TestLog(t *testing.T) { 40 cacher := &fakeLogger{make(chan logger.Message), make(chan struct{})} 41 l := &loggerWithCache{ 42 l: &fakeLogger{make(chan logger.Message, 100), make(chan struct{})}, 43 cache: cacher, 44 } 45 defer l.Close() 46 47 var messages []logger.Message 48 for i := 0; i < 100; i++ { 49 messages = append(messages, logger.Message{ 50 Timestamp: time.Now(), 51 Line: append(bytes.Repeat([]byte("a"), 100), '\n'), 52 }) 53 } 54 55 ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) 56 defer cancel() 57 58 go func() { 59 for _, msg := range messages { 60 select { 61 case <-ctx.Done(): 62 return 63 default: 64 } 65 66 m := logger.NewMessage() 67 dumbCopyMessage(m, &msg) 68 l.Log(m) 69 } 70 }() 71 72 for _, m := range messages { 73 var msg logger.Message 74 select { 75 case <-ctx.Done(): 76 t.Fatal("timed out waiting for messages... this is probably a test implementation error") 77 case msg = <-cacher.messages: 78 assert.Assert(t, cmp.DeepEqual(msg, m)) 79 } 80 } 81 }