github.com/lingyao2333/mo-zero@v1.4.1/core/logx/fields_test.go (about)

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