global_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package eventlog
  17. import (
  18. "testing"
  19. mock "github.com/stretchr/testify/mock"
  20. "github.com/stretchr/testify/suite"
  21. )
  22. type GlobalLoggerSuite struct {
  23. suite.Suite
  24. }
  25. func (s *GlobalLoggerSuite) TearDownTest() {
  26. global.Store(nil)
  27. }
  28. func (s *GlobalLoggerSuite) TestGetGlobalLogger() {
  29. l := getGlobalLogger()
  30. s.NotNil(l)
  31. s.Equal(Level_Info, l.level)
  32. s.Equal(global.Load(), l)
  33. la := getGlobalLogger()
  34. s.Equal(l, la)
  35. }
  36. func (s *GlobalLoggerSuite) TestRecord() {
  37. mock1 := NewMockLogger(s.T())
  38. mock2 := NewMockLogger(s.T())
  39. getGlobalLogger().Register("mock1", mock1)
  40. getGlobalLogger().Register("mock2", mock2)
  41. rawEvt := NewRawEvt(Level_Info, "test")
  42. mock1.EXPECT().Record(rawEvt)
  43. mock2.EXPECT().Record(rawEvt)
  44. getGlobalLogger().Record(rawEvt)
  45. mock3 := NewMockLogger(s.T())
  46. getGlobalLogger().Register("mock3", mock3) // register logger without expectations
  47. rawEvt = NewRawEvt(Level_Debug, "test")
  48. getGlobalLogger().Record(rawEvt)
  49. }
  50. func (s *GlobalLoggerSuite) TestRecordFunc() {
  51. mock1 := NewMockLogger(s.T())
  52. mock2 := NewMockLogger(s.T())
  53. getGlobalLogger().Register("mock1", mock1)
  54. getGlobalLogger().Register("mock2", mock2)
  55. rawEvt := NewRawEvt(Level_Info, "test")
  56. mock1.EXPECT().RecordFunc(mock.Anything, mock.Anything)
  57. mock2.EXPECT().RecordFunc(mock.Anything, mock.Anything)
  58. getGlobalLogger().RecordFunc(Level_Info, func() Evt { return rawEvt })
  59. mock3 := NewMockLogger(s.T())
  60. getGlobalLogger().Register("mock3", mock3) // register logger without expectations
  61. rawEvt = NewRawEvt(Level_Debug, "test")
  62. getGlobalLogger().RecordFunc(Level_Debug, func() Evt { return rawEvt })
  63. }
  64. func (s *GlobalLoggerSuite) TestFlush() {
  65. mock1 := NewMockLogger(s.T())
  66. mock2 := NewMockLogger(s.T())
  67. getGlobalLogger().Register("mock1", mock1)
  68. getGlobalLogger().Register("mock2", mock2)
  69. mock1.EXPECT().Flush().Return(nil)
  70. mock2.EXPECT().Flush().Return(nil)
  71. err := getGlobalLogger().Flush()
  72. s.NoError(err)
  73. }
  74. func TestGlobalLogger(t *testing.T) {
  75. suite.Run(t, new(GlobalLoggerSuite))
  76. }