level_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zap
  21. import (
  22. "sync"
  23. "testing"
  24. "go.uber.org/zap/zapcore"
  25. "github.com/stretchr/testify/assert"
  26. )
  27. func TestLevelEnablerFunc(t *testing.T) {
  28. enab := LevelEnablerFunc(func(l zapcore.Level) bool { return l == zapcore.InfoLevel })
  29. tests := []struct {
  30. level zapcore.Level
  31. enabled bool
  32. }{
  33. {DebugLevel, false},
  34. {InfoLevel, true},
  35. {WarnLevel, false},
  36. {ErrorLevel, false},
  37. {DPanicLevel, false},
  38. {PanicLevel, false},
  39. {FatalLevel, false},
  40. }
  41. for _, tt := range tests {
  42. assert.Equal(t, tt.enabled, enab.Enabled(tt.level), "Unexpected result applying LevelEnablerFunc to %s", tt.level)
  43. }
  44. }
  45. func TestNewAtomicLevel(t *testing.T) {
  46. lvl := NewAtomicLevel()
  47. assert.Equal(t, InfoLevel, lvl.Level(), "Unexpected initial level.")
  48. lvl.SetLevel(ErrorLevel)
  49. assert.Equal(t, ErrorLevel, lvl.Level(), "Unexpected level after SetLevel.")
  50. lvl = NewAtomicLevelAt(WarnLevel)
  51. assert.Equal(t, WarnLevel, lvl.Level(), "Unexpected level after SetLevel.")
  52. }
  53. func TestAtomicLevelMutation(t *testing.T) {
  54. lvl := NewAtomicLevel()
  55. lvl.SetLevel(WarnLevel)
  56. // Trigger races for non-atomic level mutations.
  57. proceed := make(chan struct{})
  58. wg := &sync.WaitGroup{}
  59. runConcurrently(10, 100, wg, func() {
  60. <-proceed
  61. assert.Equal(t, WarnLevel, lvl.Level())
  62. })
  63. runConcurrently(10, 100, wg, func() {
  64. <-proceed
  65. lvl.SetLevel(WarnLevel)
  66. })
  67. close(proceed)
  68. wg.Wait()
  69. }
  70. func TestAtomicLevelText(t *testing.T) {
  71. tests := []struct {
  72. text string
  73. expect zapcore.Level
  74. err bool
  75. }{
  76. {"debug", DebugLevel, false},
  77. {"info", InfoLevel, false},
  78. {"", InfoLevel, false},
  79. {"warn", WarnLevel, false},
  80. {"error", ErrorLevel, false},
  81. {"dpanic", DPanicLevel, false},
  82. {"panic", PanicLevel, false},
  83. {"fatal", FatalLevel, false},
  84. {"foobar", InfoLevel, true},
  85. }
  86. for _, tt := range tests {
  87. var lvl AtomicLevel
  88. // Test both initial unmarshaling and overwriting existing value.
  89. for i := 0; i < 2; i++ {
  90. if tt.err {
  91. assert.Error(t, lvl.UnmarshalText([]byte(tt.text)), "Expected unmarshaling %q to fail.", tt.text)
  92. } else {
  93. assert.NoError(t, lvl.UnmarshalText([]byte(tt.text)), "Expected unmarshaling %q to succeed.", tt.text)
  94. }
  95. assert.Equal(t, tt.expect, lvl.Level(), "Unexpected level after unmarshaling.")
  96. lvl.SetLevel(InfoLevel)
  97. }
  98. // Test marshalling
  99. if tt.text != "" && !tt.err {
  100. lvl.SetLevel(tt.expect)
  101. marshaled, err := lvl.MarshalText()
  102. assert.NoError(t, err, `Unexpected error marshalling level "%v" to text.`, tt.expect)
  103. assert.Equal(t, tt.text, string(marshaled), "Expected marshaled text to match")
  104. assert.Equal(t, tt.text, lvl.String(), "Expected Stringer call to match")
  105. }
  106. }
  107. }