github.com/netdata/go.d.plugin@v0.58.1/agent/jobmgr/manager_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package jobmgr 4 5 import ( 6 "bytes" 7 "context" 8 "sync" 9 "testing" 10 "time" 11 12 "github.com/netdata/go.d.plugin/agent/confgroup" 13 "github.com/netdata/go.d.plugin/agent/module" 14 "github.com/netdata/go.d.plugin/agent/safewriter" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 // TODO: tech dept 19 func TestNewManager(t *testing.T) { 20 21 } 22 23 // TODO: tech dept 24 func TestManager_Run(t *testing.T) { 25 groups := []*confgroup.Group{ 26 { 27 Source: "source", 28 Configs: []confgroup.Config{ 29 { 30 "name": "name", 31 "module": "success", 32 "update_every": module.UpdateEvery, 33 "autodetection_retry": module.AutoDetectionRetry, 34 "priority": module.Priority, 35 }, 36 { 37 "name": "name", 38 "module": "success", 39 "update_every": module.UpdateEvery + 1, 40 "autodetection_retry": module.AutoDetectionRetry, 41 "priority": module.Priority, 42 }, 43 { 44 "name": "name", 45 "module": "fail", 46 "update_every": module.UpdateEvery + 1, 47 "autodetection_retry": module.AutoDetectionRetry, 48 "priority": module.Priority, 49 }, 50 }, 51 }, 52 } 53 var buf bytes.Buffer 54 mgr := NewManager() 55 mgr.Modules = prepareMockRegistry() 56 mgr.Out = safewriter.New(&buf) 57 mgr.PluginName = "test.plugin" 58 59 ctx, cancel := context.WithCancel(context.Background()) 60 in := make(chan []*confgroup.Group) 61 var wg sync.WaitGroup 62 63 wg.Add(1) 64 go func() { defer wg.Done(); mgr.Run(ctx, in) }() 65 66 select { 67 case in <- groups: 68 case <-time.After(time.Second * 2): 69 } 70 71 time.Sleep(time.Second * 5) 72 cancel() 73 wg.Wait() 74 75 assert.True(t, buf.String() != "") 76 } 77 78 func prepareMockRegistry() module.Registry { 79 reg := module.Registry{} 80 reg.Register("success", module.Creator{ 81 Create: func() module.Module { 82 return &module.MockModule{ 83 InitFunc: func() bool { return true }, 84 CheckFunc: func() bool { return true }, 85 ChartsFunc: func() *module.Charts { 86 return &module.Charts{ 87 &module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}}, 88 } 89 }, 90 CollectFunc: func() map[string]int64 { 91 return map[string]int64{"id1": 1} 92 }, 93 } 94 }, 95 }) 96 reg.Register("fail", module.Creator{ 97 Create: func() module.Module { 98 return &module.MockModule{ 99 InitFunc: func() bool { return false }, 100 } 101 }, 102 }) 103 return reg 104 }