fields_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package logx
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "strconv"
  7. "sync"
  8. "sync/atomic"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestAddGlobalFields(t *testing.T) {
  13. var buf bytes.Buffer
  14. writer := NewWriter(&buf)
  15. old := Reset()
  16. SetWriter(writer)
  17. defer SetWriter(old)
  18. Info("hello")
  19. buf.Reset()
  20. AddGlobalFields(Field("a", "1"), Field("b", "2"))
  21. AddGlobalFields(Field("c", "3"))
  22. Info("world")
  23. var m map[string]any
  24. assert.NoError(t, json.Unmarshal(buf.Bytes(), &m))
  25. assert.Equal(t, "1", m["a"])
  26. assert.Equal(t, "2", m["b"])
  27. assert.Equal(t, "3", m["c"])
  28. }
  29. func TestContextWithFields(t *testing.T) {
  30. ctx := ContextWithFields(context.Background(), Field("a", 1), Field("b", 2))
  31. vals := ctx.Value(fieldsContextKey)
  32. assert.NotNil(t, vals)
  33. fields, ok := vals.([]LogField)
  34. assert.True(t, ok)
  35. assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
  36. }
  37. func TestWithFields(t *testing.T) {
  38. ctx := WithFields(context.Background(), Field("a", 1), Field("b", 2))
  39. vals := ctx.Value(fieldsContextKey)
  40. assert.NotNil(t, vals)
  41. fields, ok := vals.([]LogField)
  42. assert.True(t, ok)
  43. assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
  44. }
  45. func TestWithFieldsAppend(t *testing.T) {
  46. var dummyKey struct{}
  47. ctx := context.WithValue(context.Background(), dummyKey, "dummy")
  48. ctx = ContextWithFields(ctx, Field("a", 1), Field("b", 2))
  49. ctx = ContextWithFields(ctx, Field("c", 3), Field("d", 4))
  50. vals := ctx.Value(fieldsContextKey)
  51. assert.NotNil(t, vals)
  52. fields, ok := vals.([]LogField)
  53. assert.True(t, ok)
  54. assert.Equal(t, "dummy", ctx.Value(dummyKey))
  55. assert.EqualValues(t, []LogField{
  56. Field("a", 1),
  57. Field("b", 2),
  58. Field("c", 3),
  59. Field("d", 4),
  60. }, fields)
  61. }
  62. func TestWithFieldsAppendCopy(t *testing.T) {
  63. const count = 10
  64. ctx := context.Background()
  65. for i := 0; i < count; i++ {
  66. ctx = ContextWithFields(ctx, Field(strconv.Itoa(i), 1))
  67. }
  68. af := Field("foo", 1)
  69. bf := Field("bar", 2)
  70. ctxa := ContextWithFields(ctx, af)
  71. ctxb := ContextWithFields(ctx, bf)
  72. assert.EqualValues(t, af, ctxa.Value(fieldsContextKey).([]LogField)[count])
  73. assert.EqualValues(t, bf, ctxb.Value(fieldsContextKey).([]LogField)[count])
  74. }
  75. func BenchmarkAtomicValue(b *testing.B) {
  76. b.ReportAllocs()
  77. var container atomic.Value
  78. vals := []LogField{
  79. Field("a", "b"),
  80. Field("c", "d"),
  81. Field("e", "f"),
  82. }
  83. container.Store(&vals)
  84. for i := 0; i < b.N; i++ {
  85. val := container.Load()
  86. if val != nil {
  87. _ = *val.(*[]LogField)
  88. }
  89. }
  90. }
  91. func BenchmarkRWMutex(b *testing.B) {
  92. b.ReportAllocs()
  93. var lock sync.RWMutex
  94. vals := []LogField{
  95. Field("a", "b"),
  96. Field("c", "d"),
  97. Field("e", "f"),
  98. }
  99. for i := 0; i < b.N; i++ {
  100. lock.RLock()
  101. _ = vals
  102. lock.RUnlock()
  103. }
  104. }