entry_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 zapcore
  21. import (
  22. "sync"
  23. "testing"
  24. "go.uber.org/zap/internal/exit"
  25. "github.com/stretchr/testify/assert"
  26. )
  27. func TestPutNilEntry(t *testing.T) {
  28. // Pooling nil entries defeats the purpose.
  29. var wg sync.WaitGroup
  30. wg.Add(2)
  31. go func() {
  32. defer wg.Done()
  33. for i := 0; i < 1000; i++ {
  34. putCheckedEntry(nil)
  35. }
  36. }()
  37. go func() {
  38. defer wg.Done()
  39. for i := 0; i < 1000; i++ {
  40. ce := getCheckedEntry()
  41. assert.NotNil(t, ce, "Expected only non-nil CheckedEntries in pool.")
  42. assert.False(t, ce.dirty, "Unexpected dirty bit set.")
  43. assert.Nil(t, ce.ErrorOutput, "Non-nil ErrorOutput.")
  44. assert.Equal(t, WriteThenNoop, ce.should, "Unexpected terminal behavior.")
  45. assert.Equal(t, 0, len(ce.cores), "Expected empty slice of cores.")
  46. assert.True(t, cap(ce.cores) > 0, "Expected pooled CheckedEntries to pre-allocate slice of Cores.")
  47. }
  48. }()
  49. wg.Wait()
  50. }
  51. func TestEntryCaller(t *testing.T) {
  52. tests := []struct {
  53. caller EntryCaller
  54. full string
  55. short string
  56. }{
  57. {
  58. caller: NewEntryCaller(100, "/path/to/foo.go", 42, false),
  59. full: "undefined",
  60. short: "undefined",
  61. },
  62. {
  63. caller: NewEntryCaller(100, "/path/to/foo.go", 42, true),
  64. full: "/path/to/foo.go:42",
  65. short: "to/foo.go:42",
  66. },
  67. {
  68. caller: NewEntryCaller(100, "to/foo.go", 42, true),
  69. full: "to/foo.go:42",
  70. short: "to/foo.go:42",
  71. },
  72. }
  73. for _, tt := range tests {
  74. assert.Equal(t, tt.full, tt.caller.String(), "Unexpected string from EntryCaller.")
  75. assert.Equal(t, tt.full, tt.caller.FullPath(), "Unexpected FullPath from EntryCaller.")
  76. assert.Equal(t, tt.short, tt.caller.TrimmedPath(), "Unexpected TrimmedPath from EntryCaller.")
  77. }
  78. }
  79. func TestCheckedEntryWrite(t *testing.T) {
  80. // Nil checked entries are safe.
  81. var ce *CheckedEntry
  82. assert.NotPanics(t, func() { ce.Write() }, "Unexpected panic writing nil CheckedEntry.")
  83. // WriteThenPanic
  84. ce = ce.Should(Entry{}, WriteThenPanic)
  85. assert.Panics(t, func() { ce.Write() }, "Expected to panic when WriteThenPanic is set.")
  86. ce.reset()
  87. // WriteThenFatal
  88. ce = ce.Should(Entry{}, WriteThenFatal)
  89. stub := exit.WithStub(func() {
  90. ce.Write()
  91. })
  92. assert.True(t, stub.Exited, "Expected to exit when WriteThenFatal is set.")
  93. ce.reset()
  94. }