stacktrace_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "strings"
  23. "testing"
  24. "github.com/stretchr/testify/assert"
  25. "github.com/stretchr/testify/require"
  26. )
  27. func TestTakeStacktrace(t *testing.T) {
  28. trace := takeStacktrace()
  29. lines := strings.Split(trace, "\n")
  30. require.True(t, len(lines) > 0, "Expected stacktrace to have at least one frame.")
  31. assert.Contains(
  32. t,
  33. lines[0],
  34. "testing.",
  35. "Expected stacktrace to start with the test runner (zap frames are filtered out) %s.", lines[0],
  36. )
  37. }
  38. func TestIsZapFrame(t *testing.T) {
  39. zapFrames := []string{
  40. "go.uber.org/zap.Stack",
  41. "go.uber.org/zap.(*SugaredLogger).log",
  42. "go.uber.org/zap/zapcore.(ArrayMarshalerFunc).MarshalLogArray",
  43. "github.com/uber/tchannel-go/vendor/go.uber.org/zap.Stack",
  44. "github.com/uber/tchannel-go/vendor/go.uber.org/zap.(*SugaredLogger).log",
  45. "github.com/uber/tchannel-go/vendor/go.uber.org/zap/zapcore.(ArrayMarshalerFunc).MarshalLogArray",
  46. }
  47. nonZapFrames := []string{
  48. "github.com/uber/tchannel-go.NewChannel",
  49. "go.uber.org/not-zap.New",
  50. "go.uber.org/zapext.ctx",
  51. "go.uber.org/zap_ext/ctx.New",
  52. }
  53. t.Run("zap frames", func(t *testing.T) {
  54. for _, f := range zapFrames {
  55. require.True(t, isZapFrame(f), f)
  56. }
  57. })
  58. t.Run("non-zap frames", func(t *testing.T) {
  59. for _, f := range nonZapFrames {
  60. require.False(t, isZapFrame(f), f)
  61. }
  62. })
  63. }
  64. func BenchmarkTakeStacktrace(b *testing.B) {
  65. for i := 0; i < b.N; i++ {
  66. takeStacktrace()
  67. }
  68. }