forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
850 lines (818 loc) · 21.8 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
package trpc
import (
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"trpc.group/trpc-go/trpc-go/errs"
"trpc.group/trpc-go/trpc-go/rpcz"
)
// TestLoadGlobalConfigNICError tests LoadGlobalConfig error.
func TestLoadGlobalConfigNICError(t *testing.T) {
err := LoadGlobalConfig("testdata/trpc_go_error.yaml")
assert.NotNil(t, err)
}
// TestRepairServiceIPWithNicAdminError tests repairServiceIPWithNic for admin error.
func TestRepairServiceIPWithNicAdminError(t *testing.T) {
conf := &Config{}
conf.Server.Admin.IP = ""
conf.Server.Admin.Nic = "ethNoneExist"
err := repairServiceIPWithNic(conf)
assert.Contains(t, err.Error(), "can't find admin IP by the NIC")
}
func TestParseEnv(t *testing.T) {
filePath := "./trpc_go.yaml"
content := `
global:
namespace: development
env_name: ${test}
container_name: ${container_name}
local_ip: $local_ip
server:
app: ${}
server: ${server
client:
timeout: ${client_timeout}
`
err := os.WriteFile(filePath, []byte(content), 0644)
if err != nil {
t.Errorf("write file err: %v", err)
return
}
defer func() {
_ = os.Remove(filePath)
}()
containerName := `这是容器名称,this is container name.`
_ = os.Setenv("container_name", containerName)
localIP := `127.1.2.3`
_ = os.Setenv("local_ip", localIP)
timeout := 456
_ = os.Setenv("client_timeout", strconv.FormatInt(int64(timeout), 10))
c, err := parseConfigFromFile(filePath)
if err != nil {
t.Errorf("parse err: %v", err)
return
}
assert.Equal(t, c.Global.Namespace, "development")
assert.Equal(t, c.Global.EnvName, "") // env name not set, should be replaced with empty value
assert.Equal(t, c.Global.ContainerName, containerName)
assert.Equal(t, c.Global.LocalIP, "$local_ip") // only ${var} instead of $var is valid
assert.Equal(t, c.Server.App, "") // empty ${} should be deleted
assert.Equal(t, c.Server.Server, "${server") // only ${var} is valid
assert.Equal(t, c.Client.Timeout, timeout)
}
func TestParseEnvSpecialChar(t *testing.T) {
filePath := "./trpc_go.yaml"
content := `
global:
namespace: development
env_name: test
container_name: ${container_name}
local_ip: ${local_ip}
`
err := os.WriteFile(filePath, []byte(content), 0644)
if err != nil {
t.Errorf("write file err: %v", err)
return
}
defer func() {
_ = os.Remove(filePath)
}()
containerName := `'这是"容器名称": this is container name. !@#$%^&*()_+-='`
_ = os.Setenv("container_name", containerName)
actualContainerName := strings.Trim(containerName, `'`) // single quotes
localIP := `"'127.1.2.3'"`
_ = os.Setenv("local_ip", localIP)
actualLocalIP := strings.Trim(localIP, `"`) // double quotes
c, err := parseConfigFromFile(filePath)
if err != nil {
t.Errorf("parse err: %v", err)
return
}
assert.Equal(t, c.Global.Namespace, "development")
assert.Equal(t, c.Global.EnvName, "test")
assert.Equal(t, c.Global.ContainerName, actualContainerName)
assert.Equal(t, c.Global.LocalIP, actualLocalIP)
}
func Test_serverConfigPath(t *testing.T) {
oldServerConfigPath := ServerConfigPath
ServerConfigPath = "./testdata/trpc_go.yaml"
path := serverConfigPath()
assert.Equal(t, "./testdata/trpc_go.yaml", path)
ServerConfigPath = "./trpc_go.yaml"
path = serverConfigPath()
assert.Equal(t, defaultConfigPath, path)
ServerConfigPath = oldServerConfigPath
}
func Test_setDefault(t *testing.T) {
var (
dstEmpty string
dstNotEmpty = "not-empty"
def = "default"
)
setDefault(&dstEmpty, def)
setDefault(&dstNotEmpty, def)
assert.Equal(t, dstEmpty, def)
assert.NotEqual(t, dstNotEmpty, def)
}
func TestConfigTransport(t *testing.T) {
t.Run("Server Config", func(t *testing.T) {
var cfg Config
require.Nil(t, yaml.Unmarshal([]byte(`
server:
transport: test-transport
`), &cfg))
require.Equal(t, "test-transport", cfg.Server.Transport)
})
t.Run("Service Config", func(t *testing.T) {
var cfg ServiceConfig
require.Nil(t, yaml.Unmarshal([]byte(`
transport: test-transport
`), &cfg))
require.Equal(t, "test-transport", cfg.Transport)
})
t.Run("Client Config", func(t *testing.T) {
var cfg ClientConfig
require.Nil(t, yaml.Unmarshal([]byte(`
transport: test-transport
`), &cfg))
require.Equal(t, "test-transport", cfg.Transport)
})
}
func TestConfigStreamFilter(t *testing.T) {
filterName := "sf1"
t.Run("server config", func(t *testing.T) {
var cfg Config
require.Nil(t, yaml.Unmarshal([]byte(`
server:
stream_filter:
- sf1
`), &cfg))
require.Equal(t, filterName, cfg.Server.StreamFilter[0])
})
t.Run("service config", func(t *testing.T) {
var cfg ServiceConfig
require.Nil(t, yaml.Unmarshal([]byte(`
stream_filter:
- sf1
`), &cfg))
require.Equal(t, filterName, cfg.StreamFilter[0])
})
t.Run("client config", func(t *testing.T) {
var cfg ClientConfig
require.Nil(t, yaml.Unmarshal([]byte(`
stream_filter:
- sf1
`), &cfg))
require.Equal(t, filterName, cfg.StreamFilter[0])
})
}
func TestRecordWhen(t *testing.T) {
t.Run("empty record-when", func(t *testing.T) {
config := &RPCZConfig{}
require.Nil(t, yaml.Unmarshal(
[]byte(`
fraction: 1.0
capacity: 10`),
config,
))
r := rpcz.NewRPCZ(config.generate())
var ids []rpcz.SpanID
for i := 0; i < 10; i++ {
s, ender := r.NewChild("")
ids = append(ids, s.ID())
ender.End()
}
for _, id := range ids {
_, ok := r.Query(id)
require.True(t, ok)
}
})
t.Run("unknown node", func(t *testing.T) {
config := &RecordWhenConfig{}
err := yaml.Unmarshal(
[]byte(`
- XOR:
- __min_request_size: 30
- __min_response_size: 40
`),
config,
)
require.Contains(t, errs.Msg(err), "unknown node: XOR")
})
t.Run("AND node is map type", func(t *testing.T) {
config := &RecordWhenConfig{}
err := yaml.Unmarshal(
[]byte(`
- AND: {__rpc_name: "/trpc.app.server.service/method"}
`),
config,
)
require.Contains(t, errs.Msg(err), "cannot unmarshal !!map into []map[trpc.nodeKind]yaml.Node")
})
t.Run("OR node is map type", func(t *testing.T) {
config := &RecordWhenConfig{}
err := yaml.Unmarshal(
[]byte(`
- OR: {__rpc_name: "/trpc.app.server.service/method"}
`),
config,
)
require.Contains(t, errs.Msg(err), "cannot unmarshal !!map into []map[trpc.nodeKind]yaml.Node")
})
}
func TestRecordWhen_NotNode(t *testing.T) {
t.Run("NOT node is empty", func(t *testing.T) {
config := &RPCZConfig{}
err := yaml.Unmarshal([]byte(`
record_when:
- NOT:
`),
config,
)
require.ErrorContains(t, err, "value is empty")
})
t.Run("NOT node has two children", func(t *testing.T) {
config := &RecordWhenConfig{}
err := yaml.Unmarshal(
[]byte(`
- NOT: {__rpc_name: "/trpc.app.server.service/method", __min_duration: 1000ms}
`),
config,
)
require.Contains(t, errs.Msg(err), "the valid number of child node can only be 1")
})
t.Run("NOT has a leaf child", func(t *testing.T) {
config := &RecordWhenConfig{}
require.Nil(t, yaml.Unmarshal(
[]byte(`
- NOT:
__rpc_name: "/trpc.app.server.service/method"
`),
config,
))
})
t.Run("NOT has a internal child", func(t *testing.T) {
config := &RecordWhenConfig{}
require.Nil(t, yaml.Unmarshal(
[]byte(`
- NOT:
OR:
- __min_duration: 1000ms
- __rpc_name: "/trpc.app.server.service/method"
`),
config,
))
})
t.Run("NOT node is slice type", func(t *testing.T) {
config := &RecordWhenConfig{}
err := yaml.Unmarshal(
[]byte(`
- NOT:
- __rpc_name: "/trpc.app.server.service/method"
`),
config,
)
require.Contains(t, errs.Msg(err), "cannot unmarshal !!seq into map[trpc.nodeKind]yaml.Node")
})
}
func TestRecordWhen_ANDNode(t *testing.T) {
t.Run("AND node is empty", func(t *testing.T) {
config := &RPCZConfig{}
err := yaml.Unmarshal([]byte(`
record_when:
- AND:
`),
config,
)
require.ErrorContains(t, err, "value is empty")
})
t.Run("AND node has two children", func(t *testing.T) {
config := &RecordWhenConfig{}
require.Nil(t, yaml.Unmarshal(
[]byte(`
- AND:
- __rpc_name: "/trpc.app.server.service/method"
- __min_duration: 1000ms
`),
config,
))
})
t.Run("AND has a leaf child", func(t *testing.T) {
config := &RecordWhenConfig{}
require.Nil(t, yaml.Unmarshal(
[]byte(`
- AND:
- __rpc_name: "/trpc.app.server.service/method"
`),
config,
))
})
t.Run("AND has a internal child", func(t *testing.T) {
config := &RecordWhenConfig{}
require.Nil(t, yaml.Unmarshal(
[]byte(`
- AND:
- OR:
- __min_duration: 1000ms
- __rpc_name: "/trpc.app.server.service/method"
`),
config,
))
})
t.Run("AND node is map type", func(t *testing.T) {
config := &RecordWhenConfig{}
err := yaml.Unmarshal(
[]byte(`
- AND:
__rpc_name: "/trpc.app.server.service/method"
`),
config,
)
require.Contains(t, errs.Msg(err), "cannot unmarshal !!map into []map[trpc.nodeKind]yaml.Node")
})
}
func TestRPCZ_RecordWhen_ErrorCode(t *testing.T) {
config := &RPCZConfig{}
mustYamlUnmarshal(t, []byte(`
fraction: 1.0
capacity: 10
record_when:
- __sampling_fraction: 1
- OR:
- __error_code: 1 # RetServerDecodeFail = 1
- __error_code: 2 # RetServerEncodeFail = 2
- __error_message: "service codec"
- __error_message: "client codec"
- NOT:
OR:
- __error_code: 1
- __error_message: "service codec"
`), config)
r := rpcz.NewRPCZ(config.generate())
var (
expectedIDs []rpcz.SpanID
unexpectedIDs []rpcz.SpanID
)
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetServerDecodeFail, "service codec"))
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetServerEncodeFail, "service codec"))
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetServerDecodeFail, "client codec"))
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetServerEncodeFail, "client codec"))
expectedIDs = append(expectedIDs, s.ID())
ender.End()
}
for i, id := range expectedIDs {
_, ok := r.Query(id)
require.True(t, ok, i)
}
for i, id := range unexpectedIDs {
_, ok := r.Query(id)
require.False(t, ok, i)
}
}
func TestRPC_RecordWhen_CustomAttribute(t *testing.T) {
config := &RPCZConfig{}
mustYamlUnmarshal(t, []byte(`
fraction: 1.0
capacity: 10
record_when:
- __sampling_fraction: 1
- OR:
- __has_attribute: (race, elf)
- __has_attribute: (class, wizard)
- NOT:
OR:
- __has_attribute: (race, dwarf)
- __has_attribute: (class, warlock)
`), config)
r := rpcz.NewRPCZ(config.generate())
var (
expectedIDs []rpcz.SpanID
unexpectedIDs []rpcz.SpanID
)
{
s, ender := r.NewChild("")
s.SetAttribute("race", "elf")
s.SetAttribute("class", "wizard")
expectedIDs = append(expectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute("race", "elf")
s.SetAttribute("class", "wizard, warlock")
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute("race", "elf, dwarf")
s.SetAttribute("class", "wizard")
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute("race", "elf, dwarf")
s.SetAttribute("class", "wizard, warlock")
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
for i, id := range expectedIDs {
_, ok := r.Query(id)
require.True(t, ok, i)
}
for i, id := range unexpectedIDs {
_, ok := r.Query(id)
require.False(t, ok, i)
}
}
func TestRPC_RecordWhen_InvalidCustomAttribute(t *testing.T) {
t.Run("miss left parenthesis", func(t *testing.T) {
config := &RPCZConfig{}
require.ErrorContains(t, yaml.Unmarshal([]byte(`
record_when:
- __has_attribute: race, elf)
`), config), "invalid attribute form")
})
t.Run("miss right parenthesis", func(t *testing.T) {
config := &RPCZConfig{}
require.ErrorContains(t, yaml.Unmarshal([]byte(`
record_when:
- __has_attribute: (race, elf
`), config), "invalid attribute form")
})
t.Run("middle delimiter space", func(t *testing.T) {
config := &RPCZConfig{}
require.ErrorContains(t, yaml.Unmarshal([]byte(`
record_when:
- __has_attribute: (race,elf)
`), config), "invalid attribute form")
})
t.Run("middle delimiter comma", func(t *testing.T) {
config := &RPCZConfig{}
require.ErrorContains(t, yaml.Unmarshal([]byte(`
record_when:
- __has_attribute: (race elf)
`), config), "invalid attribute form")
})
}
func TestRPCZ_RecordWhen_MinDuration(t *testing.T) {
t.Run("not empty", func(t *testing.T) {
config := &RPCZConfig{}
mustYamlUnmarshal(t, []byte(`
fraction: 1.0
capacity: 10
record_when:
- __error_code: 999 # RetUnknown = 0
- __min_duration: 100ms
- __sampling_fraction: 1
`), config)
r := rpcz.NewRPCZ(config.generate())
var (
expectedIDs []rpcz.SpanID
unexpectedIDs []rpcz.SpanID
)
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetUnknown, ""))
// mimic some time-consuming operation.
time.Sleep(1 * time.Second)
expectedIDs = append(expectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetUnknown, ""))
// mimic some time-consuming operation.
time.Sleep(2 * time.Second)
expectedIDs = append(expectedIDs, s.ID())
ender.End()
}
{
// don't call ender.End()
s, _ := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetOK, ""))
unexpectedIDs = append(unexpectedIDs, s.ID())
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetUnknown, ""))
// mimic some time-consuming operation.
time.Sleep(1 * time.Millisecond)
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetUnknown, ""))
// mimic some time-consuming operation.
time.Sleep(2 * time.Millisecond)
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
for _, id := range expectedIDs {
_, ok := r.Query(id)
require.True(t, ok)
}
for _, id := range unexpectedIDs {
_, ok := r.Query(id)
require.False(t, ok)
}
})
t.Run("empty", func(t *testing.T) {
config := &RPCZConfig{}
mustYamlUnmarshal(t, []byte(`
fraction: 1.0
capacity: 10
record_when:
- __error_code: 0 # RetOK = 0
- __sampling_fraction: 1
`), config)
r := rpcz.NewRPCZ(config.generate())
var (
unexpectedID rpcz.SpanID
expectedID rpcz.SpanID
)
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetUnknown, ""))
// mimic some time-consuming operation.
time.Sleep(1 * time.Millisecond)
unexpectedID = s.ID()
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetOK, ""))
// mimic some time-consuming operation.
time.Sleep(1 * time.Millisecond)
expectedID = s.ID()
ender.End()
}
_, ok := r.Query(unexpectedID)
require.False(t, ok)
_, ok = r.Query(expectedID)
require.True(t, ok)
})
}
func TestRPCZ_RecordWhen_MinRequestSize(t *testing.T) {
config := &RPCZConfig{}
mustYamlUnmarshal(t, []byte(`
fraction: 1.0
capacity: 10
record_when:
- __sampling_fraction: 1
- __min_request_size: 30
`), config)
r := rpcz.NewRPCZ(config.generate())
t.Run("unset request size", func(t *testing.T) {
s, ender := r.NewChild("")
unexpectedID := s.ID()
ender.End()
_, ok := r.Query(unexpectedID)
require.False(t, ok)
})
t.Run("request size less than min_request_size", func(t *testing.T) {
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeRequestSize, 29)
unexpectedID := s.ID()
ender.End()
_, ok := r.Query(unexpectedID)
require.False(t, ok)
})
t.Run("request size equals to min_request_size", func(t *testing.T) {
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeRequestSize, 30)
expectedID := s.ID()
ender.End()
_, ok := r.Query(expectedID)
require.True(t, ok)
})
t.Run("request size greater than min_request_size", func(t *testing.T) {
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeRequestSize, 31)
expectedID := s.ID()
ender.End()
_, ok := r.Query(expectedID)
require.True(t, ok)
})
}
func TestRPCZ_RecordWhen_MinResponseSize(t *testing.T) {
config := &RPCZConfig{}
mustYamlUnmarshal(t, []byte(`
fraction: 1.0
capacity: 10
record_when:
- __sampling_fraction: 1
- __min_response_size: 40
`), config)
r := rpcz.NewRPCZ(config.generate())
t.Run("unset response size", func(t *testing.T) {
s, ender := r.NewChild("")
unexpectedID := s.ID()
ender.End()
_, ok := r.Query(unexpectedID)
require.False(t, ok)
})
t.Run("request size less than min_response_size", func(t *testing.T) {
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeResponseSize, 39)
unexpectedID := s.ID()
ender.End()
_, ok := r.Query(unexpectedID)
require.False(t, ok)
})
t.Run("request size equals to min_response_size", func(t *testing.T) {
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeResponseSize, 40)
expectedID := s.ID()
ender.End()
_, ok := r.Query(expectedID)
require.True(t, ok)
})
t.Run("request size greater than min_response_size", func(t *testing.T) {
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeResponseSize, 41)
expectedID := s.ID()
ender.End()
_, ok := r.Query(expectedID)
require.True(t, ok)
})
}
func TestRPCZ_RecordWhen_RPCName(t *testing.T) {
config := &RPCZConfig{}
mustYamlUnmarshal(t, []byte(`
fraction: 1.0
capacity: 10
record_when:
- __sampling_fraction: 1
- __rpc_name: trpc.app.server.service
`), config)
r := rpcz.NewRPCZ(config.generate())
t.Run("unset RPCName", func(t *testing.T) {
s, ender := r.NewChild("")
unexpectedID := s.ID()
ender.End()
_, ok := r.Query(unexpectedID)
require.False(t, ok)
})
t.Run("RPCName does not contain rpc_name", func(t *testing.T) {
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeRPCName, "/xxx.app.server.service/method")
unexpectedID := s.ID()
ender.End()
_, ok := r.Query(unexpectedID)
require.False(t, ok)
})
t.Run("RPCName contains rpc_name", func(t *testing.T) {
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeRPCName, "/trpc.app.server.service/method")
expectedID := s.ID()
ender.End()
_, ok := r.Query(expectedID)
require.True(t, ok)
})
}
func TestRPCZ_RecordWhen_ErrorCodeAndMinDuration(t *testing.T) {
config := &RPCZConfig{}
mustYamlUnmarshal(t, []byte(`
fraction: 1.0
capacity: 10
record_when:
- AND:
- __error_code: 0 # RetOK = 0
- __min_duration: 100ms
- __sampling_fraction: 1
`), config)
r := rpcz.NewRPCZ(config.generate())
var (
expectedIDs []rpcz.SpanID
unexpectedIDs []rpcz.SpanID
)
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetOK, ""))
// mimic some time-consuming operation.
time.Sleep(1 * time.Second)
expectedIDs = append(expectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetUnknown, ""))
// mimic some time-consuming operation.
time.Sleep(2 * time.Second)
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetOK, ""))
// mimic some time-consuming operation.
time.Sleep(1 * time.Millisecond)
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
s, ender := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetUnknown, ""))
// mimic some time-consuming operation.
time.Sleep(2 * time.Millisecond)
unexpectedIDs = append(unexpectedIDs, s.ID())
ender.End()
}
{
// don't call ender.End()
s, _ := r.NewChild("")
s.SetAttribute(rpcz.TRPCAttributeError, errs.NewFrameError(errs.RetOK, ""))
unexpectedIDs = append(unexpectedIDs, s.ID())
}
for i, id := range expectedIDs {
_, ok := r.Query(id)
require.True(t, ok, i)
}
for i, id := range unexpectedIDs {
_, ok := r.Query(id)
require.False(t, ok, i)
}
}
func mustYamlUnmarshal(t *testing.T, in []byte, out interface{}) {
t.Helper()
if err := yaml.Unmarshal(in, out); err != nil {
t.Fatal(err)
}
}
func TestRepairServiceIdleTime(t *testing.T) {
t.Run("set by service timeout", func(t *testing.T) {
var cfg Config
require.Nil(t, yaml.Unmarshal([]byte(`
server:
service:
- name: trpc.test.helloworld.Greeter
ip: 127.0.0.1
port: 8000
network: tcp
protocol: trpc
timeout: 120000
`), &cfg))
require.Nil(t, RepairConfig(&cfg))
require.Equal(t, 120000, cfg.Server.Service[0].Idletime)
})
t.Run("set by default", func(t *testing.T) {
var cfg Config
require.Nil(t, yaml.Unmarshal([]byte(`
server:
service:
- name: trpc.test.helloworld.Greeter
ip: 127.0.0.1
port: 8000
network: tcp
protocol: trpc
timeout: 500
`), &cfg))
require.Nil(t, RepairConfig(&cfg))
require.Equal(t, defaultIdleTimeout, cfg.Server.Service[0].Idletime)
})
t.Run("set by config", func(t *testing.T) {
var cfg Config
require.Nil(t, yaml.Unmarshal([]byte(`
server:
service:
- name: trpc.test.helloworld.Greeter
ip: 127.0.0.1
port: 8000
network: tcp
protocol: trpc
timeout: 500
idletime: 1500
`), &cfg))
require.Nil(t, RepairConfig(&cfg))
require.Equal(t, 1500, cfg.Server.Service[0].Idletime)
})
}