master
go 63 lines 1.33 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package snmp
4
5 import (
6 "context"
7 "sync"
8 "testing"
9
10 "github.com/netdata/netdata/go/plugins/pkg/funcapi"
11 "github.com/stretchr/testify/assert"
12 )
13
14 type testMethodHandler struct{}
15
16 func (testMethodHandler) MethodParams(context.Context, string) ([]funcapi.ParamConfig, error) {
17 return nil, nil
18 }
19
20 func (testMethodHandler) Handle(context.Context, string, funcapi.ResolvedParams) *funcapi.FunctionResponse {
21 return &funcapi.FunctionResponse{Status: 200}
22 }
23
24 func (testMethodHandler) Cleanup(context.Context) {}
25
26 func TestFuncRouter_ConcurrentRegisterAndHandle(t *testing.T) {
27 tests := map[string]struct {
28 iterations int
29 }{
30 "registering a late handler does not race with function calls": {
31 iterations: 200,
32 },
33 }
34
35 for name, tc := range tests {
36 t.Run(name, func(t *testing.T) {
37 router := newFuncRouter(newIfaceCache())
38 handler := testMethodHandler{}
39 ctx := context.Background()
40
41 var wg sync.WaitGroup
42 wg.Add(2)
43
44 go func() {
45 defer wg.Done()
46 for i := 0; i < tc.iterations; i++ {
47 router.registerHandler("dynamic", handler)
48 }
49 }()
50
51 go func() {
52 defer wg.Done()
53 for i := 0; i < tc.iterations; i++ {
54 _, _ = router.MethodParams(ctx, "dynamic")
55 resp := router.Handle(ctx, "dynamic", nil)
56 assert.NotNil(t, resp)
57 }
58 }()
59
60 wg.Wait()
61 })
62 }
63 }