trpc.group/trpc-go/trpc-go@v1.0.3/metrics/options_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package metrics_test 15 16 import ( 17 "reflect" 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 "trpc.group/trpc-go/trpc-go/metrics" 22 ) 23 24 func TestWithMeta(t *testing.T) { 25 want := metrics.Options{} 26 monitors := map[string]interface{}{"req.total": 10001, "req.fail": 10002, "req.succ": 10003} 27 opt := metrics.WithMeta(monitors) 28 opt(&want) 29 30 type args struct { 31 meta map[string]interface{} 32 } 33 tests := []struct { 34 name string 35 args args 36 want metrics.Options 37 }{ 38 {"monitor", args{meta: monitors}, want}, 39 } 40 for _, tt := range tests { 41 t.Run(tt.name, func(t *testing.T) { 42 got := metrics.Options{} 43 opt := metrics.WithMeta(tt.args.meta) 44 opt(&got) 45 46 if !reflect.DeepEqual(got, tt.want) { 47 t.Errorf("WithMeta() = %v, comp %v", got, tt.want) 48 } 49 }) 50 } 51 } 52 53 func TestGetOptions(t *testing.T) { 54 assert.Nil(t, metrics.GetOptions().Meta) 55 56 meta := map[string]interface{}{ 57 "req.total": 10000, 58 "req.fail": 10001, 59 } 60 opts := metrics.Options{} 61 o := metrics.WithMeta(meta) 62 o(&opts) 63 64 assert.Equal(t, opts.Meta, meta) 65 }