clog_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2017 Unknwon
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package clog
  15. import (
  16. "bytes"
  17. "sync"
  18. "testing"
  19. . "github.com/smartystreets/goconvey/convey"
  20. )
  21. func Test_Version(t *testing.T) {
  22. Convey("Get version", t, func() {
  23. So(Version(), ShouldEqual, _VERSION)
  24. })
  25. }
  26. func Test_isValidLevel(t *testing.T) {
  27. Convey("Validate log level", t, func() {
  28. So(isValidLevel(LEVEL(-1)), ShouldBeFalse)
  29. So(isValidLevel(LEVEL(5)), ShouldBeFalse)
  30. So(isValidLevel(TRACE), ShouldBeTrue)
  31. So(isValidLevel(FATAL), ShouldBeTrue)
  32. })
  33. }
  34. const _MEMORY MODE = "memory"
  35. type memoryConfig struct {
  36. // Minimum level of messages to be processed.
  37. Level LEVEL
  38. // Buffer size defines how many messages can be queued before hangs.
  39. BufferSize int64
  40. }
  41. var (
  42. buf bytes.Buffer
  43. wg sync.WaitGroup
  44. )
  45. type memory struct {
  46. Adapter
  47. }
  48. func newMemory() Logger {
  49. return &memory{
  50. Adapter: Adapter{
  51. quitChan: make(chan struct{}),
  52. },
  53. }
  54. }
  55. func (m *memory) Level() LEVEL { return m.level }
  56. func (m *memory) Init(v interface{}) error {
  57. cfg, ok := v.(memoryConfig)
  58. if !ok {
  59. return ErrConfigObject{"memoryConfig", v}
  60. }
  61. if !isValidLevel(cfg.Level) {
  62. return ErrInvalidLevel{}
  63. }
  64. m.level = cfg.Level
  65. m.msgChan = make(chan *Message, cfg.BufferSize)
  66. return nil
  67. }
  68. func (m *memory) ExchangeChans(errorChan chan<- error) chan *Message {
  69. m.errorChan = errorChan
  70. return m.msgChan
  71. }
  72. func (m *memory) write(msg *Message) {
  73. buf.WriteString(msg.Body)
  74. wg.Done()
  75. }
  76. func (m *memory) Start() {
  77. LOOP:
  78. for {
  79. select {
  80. case msg := <-m.msgChan:
  81. m.write(msg)
  82. case <-m.quitChan:
  83. break LOOP
  84. }
  85. }
  86. for {
  87. if len(m.msgChan) == 0 {
  88. break
  89. }
  90. m.write(<-m.msgChan)
  91. }
  92. m.quitChan <- struct{}{} // Notify the cleanup is done.
  93. }
  94. func (m *memory) Destroy() {
  95. m.quitChan <- struct{}{}
  96. <-m.quitChan
  97. close(m.msgChan)
  98. close(m.quitChan)
  99. }
  100. func init() {
  101. Register(_MEMORY, newMemory)
  102. }
  103. func Test_Clog(t *testing.T) {
  104. Convey("In-memory logging", t, func() {
  105. So(New(_MEMORY, memoryConfig{}), ShouldBeNil)
  106. Convey("Basic logging", func() {
  107. buf.Reset()
  108. wg.Add(1)
  109. Trace("Level: %v", TRACE)
  110. wg.Wait()
  111. So(buf.String(), ShouldEqual, "[TRACE] Level: 0")
  112. buf.Reset()
  113. wg.Add(1)
  114. Info("Level: %v", INFO)
  115. wg.Wait()
  116. So(buf.String(), ShouldEqual, "[ INFO] Level: 1")
  117. buf.Reset()
  118. wg.Add(1)
  119. Warn("Level: %v", WARN)
  120. wg.Wait()
  121. So(buf.String(), ShouldEqual, "[ WARN] Level: 2")
  122. buf.Reset()
  123. wg.Add(1)
  124. Error(0, "Level: %v", ERROR)
  125. wg.Wait()
  126. So(buf.String(), ShouldEqual, "[ERROR] Level: 3")
  127. buf.Reset()
  128. wg.Add(1)
  129. Error(2, "Level: %v", ERROR)
  130. wg.Wait()
  131. So(buf.String(), ShouldContainSubstring, "clog_test.go")
  132. })
  133. })
  134. Convey("Skip logs has lower level", t, func() {
  135. So(New(_MEMORY, memoryConfig{
  136. Level: ERROR,
  137. }), ShouldBeNil)
  138. buf.Reset()
  139. Trace("Level: %v", TRACE)
  140. Trace("Level: %v", INFO)
  141. So(buf.String(), ShouldEqual, "")
  142. })
  143. }