Skip to content

Commit

Permalink
chore(go.d): pass context to init/check/collect/cleanup (netdata#19180)
Browse files Browse the repository at this point in the history
pass ctx to init/check/collect/cleanup
  • Loading branch information
ilyam8 authored Dec 10, 2024
1 parent d37130d commit b723a59
Show file tree
Hide file tree
Showing 261 changed files with 2,232 additions and 1,973 deletions.
8 changes: 4 additions & 4 deletions src/go/plugin/go.d/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ func prepareRegistry(mux *sync.Mutex, stats map[string]int, names ...string) mod

func prepareMockModule(name string, mux *sync.Mutex, stats map[string]int) module.Module {
return &module.MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
mux.Lock()
defer mux.Unlock()
stats[name+"_init"]++
return nil
},
CheckFunc: func() error {
CheckFunc: func(context.Context) error {
mux.Lock()
defer mux.Unlock()
stats[name+"_check"]++
Expand All @@ -88,13 +88,13 @@ func prepareMockModule(name string, mux *sync.Mutex, stats map[string]int) modul
&module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}},
}
},
CollectFunc: func() map[string]int64 {
CollectFunc: func(context.Context) map[string]int64 {
mux.Lock()
defer mux.Unlock()
stats[name+"_collect"]++
return map[string]int64{"id1": 1}
},
CleanupFunc: func() {
CleanupFunc: func(context.Context) {
mux.Lock()
defer mux.Unlock()
stats[name+"_cleanup"]++
Expand Down
6 changes: 3 additions & 3 deletions src/go/plugin/go.d/agent/jobmgr/dyncfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,13 @@ func (m *Manager) dyncfgConfigTest(fn functions.Function) {
slog.String("job", cfg.Name()),
)

defer job.Cleanup()
defer job.Cleanup(context.Background())

if err := job.Init(); err != nil {
if err := job.Init(context.Background()); err != nil {
m.dyncfgRespf(fn, 422, "Job initialization failed: %v", err)
return
}
if err := job.Check(); err != nil {
if err := job.Check(context.Background()); err != nil {
m.dyncfgRespf(fn, 422, "Job check failed: %v", err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/go/plugin/go.d/agent/jobmgr/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func prepareMockRegistry() module.Registry {
ChartsFunc: func() *module.Charts {
return &module.Charts{&module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}}}
},
CollectFunc: func() map[string]int64 { return map[string]int64{"id1": 1} },
CollectFunc: func(context.Context) map[string]int64 { return map[string]int64{"id1": 1} },
}
},
Config: func() any {
Expand All @@ -144,7 +144,7 @@ func prepareMockRegistry() module.Registry {
reg.Register("fail", module.Creator{
Create: func() module.Module {
return &module.MockModule{
InitFunc: func() error { return errors.New("mock failed init") },
InitFunc: func(context.Context) error { return errors.New("mock failed init") },
}
},
})
Expand Down
11 changes: 6 additions & 5 deletions src/go/plugin/go.d/agent/module/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package module

import (
"bytes"
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -224,7 +225,7 @@ func (j *Job) AutoDetection() (err error) {
}
}
if err != nil {
j.module.Cleanup()
j.module.Cleanup(context.TODO())
}
}()

Expand Down Expand Up @@ -282,7 +283,7 @@ LOOP:
}
}
}
j.module.Cleanup()
j.module.Cleanup(context.TODO())
j.Cleanup()
j.stop <- struct{}{}
}
Expand Down Expand Up @@ -342,7 +343,7 @@ func (j *Job) init() error {
return nil
}

if err := j.module.Init(); err != nil {
if err := j.module.Init(context.TODO()); err != nil {
return err
}

Expand All @@ -352,7 +353,7 @@ func (j *Job) init() error {
}

func (j *Job) check() error {
if err := j.module.Check(); err != nil {
if err := j.module.Check(context.TODO()); err != nil {
if j.AutoDetectTries != infTries {
j.AutoDetectTries--
}
Expand Down Expand Up @@ -405,7 +406,7 @@ func (j *Job) collect() (result map[string]int64) {
}
}
}()
return j.module.Collect()
return j.module.Collect(context.TODO())
}

func (j *Job) processMetrics(metrics map[string]int64, startTime time.Time, sinceLastRun int) bool {
Expand Down
33 changes: 17 additions & 16 deletions src/go/plugin/go.d/agent/module/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package module

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -73,10 +74,10 @@ func TestJob_AutoDetectionEvery(t *testing.T) {
func TestJob_RetryAutoDetection(t *testing.T) {
job := newTestJob()
m := &MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
return nil
},
CheckFunc: func() error { return errors.New("check error") },
CheckFunc: func(context.Context) error { return errors.New("check error") },
ChartsFunc: func() *Charts {
return &Charts{}
},
Expand Down Expand Up @@ -104,11 +105,11 @@ func TestJob_AutoDetection(t *testing.T) {
job := newTestJob()
var v int
m := &MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
v++
return nil
},
CheckFunc: func() error {
CheckFunc: func(context.Context) error {
v++
return nil
},
Expand All @@ -126,7 +127,7 @@ func TestJob_AutoDetection(t *testing.T) {
func TestJob_AutoDetection_FailInit(t *testing.T) {
job := newTestJob()
m := &MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
return errors.New("init error")
},
}
Expand All @@ -139,10 +140,10 @@ func TestJob_AutoDetection_FailInit(t *testing.T) {
func TestJob_AutoDetection_FailCheck(t *testing.T) {
job := newTestJob()
m := &MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
return nil
},
CheckFunc: func() error {
CheckFunc: func(context.Context) error {
return errors.New("check error")
},
}
Expand All @@ -155,10 +156,10 @@ func TestJob_AutoDetection_FailCheck(t *testing.T) {
func TestJob_AutoDetection_FailPostCheck(t *testing.T) {
job := newTestJob()
m := &MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
return nil
},
CheckFunc: func() error {
CheckFunc: func(context.Context) error {
return nil
},
ChartsFunc: func() *Charts {
Expand All @@ -174,7 +175,7 @@ func TestJob_AutoDetection_FailPostCheck(t *testing.T) {
func TestJob_AutoDetection_PanicInit(t *testing.T) {
job := newTestJob()
m := &MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
panic("panic in Init")
},
}
Expand All @@ -187,10 +188,10 @@ func TestJob_AutoDetection_PanicInit(t *testing.T) {
func TestJob_AutoDetection_PanicCheck(t *testing.T) {
job := newTestJob()
m := &MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
return nil
},
CheckFunc: func() error {
CheckFunc: func(context.Context) error {
panic("panic in Check")
},
}
Expand All @@ -203,10 +204,10 @@ func TestJob_AutoDetection_PanicCheck(t *testing.T) {
func TestJob_AutoDetection_PanicPostCheck(t *testing.T) {
job := newTestJob()
m := &MockModule{
InitFunc: func() error {
InitFunc: func(context.Context) error {
return nil
},
CheckFunc: func() error {
CheckFunc: func(context.Context) error {
return nil
},
ChartsFunc: func() *Charts {
Expand Down Expand Up @@ -234,7 +235,7 @@ func TestJob_Start(t *testing.T) {
},
}
},
CollectFunc: func() map[string]int64 {
CollectFunc: func(context.Context) map[string]int64 {
return map[string]int64{
"id1": 1,
"id2": 2,
Expand All @@ -261,7 +262,7 @@ func TestJob_Start(t *testing.T) {

func TestJob_MainLoop_Panic(t *testing.T) {
m := &MockModule{
CollectFunc: func() map[string]int64 {
CollectFunc: func(context.Context) map[string]int64 {
panic("panic in Collect")
},
}
Expand Down
29 changes: 16 additions & 13 deletions src/go/plugin/go.d/agent/module/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

package module

import "errors"
import (
"context"
"errors"
)

const MockConfigSchema = `
{
Expand Down Expand Up @@ -38,31 +41,31 @@ type MockModule struct {

FailOnInit bool

InitFunc func() error
CheckFunc func() error
InitFunc func(context.Context) error
CheckFunc func(context.Context) error
ChartsFunc func() *Charts
CollectFunc func() map[string]int64
CleanupFunc func()
CollectFunc func(context.Context) map[string]int64
CleanupFunc func(context.Context)
CleanupDone bool
}

// Init invokes InitFunc.
func (m *MockModule) Init() error {
func (m *MockModule) Init(ctx context.Context) error {
if m.FailOnInit {
return errors.New("mock init error")
}
if m.InitFunc == nil {
return nil
}
return m.InitFunc()
return m.InitFunc(ctx)
}

// Check invokes CheckFunc.
func (m *MockModule) Check() error {
func (m *MockModule) Check(ctx context.Context) error {
if m.CheckFunc == nil {
return nil
}
return m.CheckFunc()
return m.CheckFunc(ctx)
}

// Charts invokes ChartsFunc.
Expand All @@ -74,17 +77,17 @@ func (m *MockModule) Charts() *Charts {
}

// Collect invokes CollectDunc.
func (m *MockModule) Collect() map[string]int64 {
func (m *MockModule) Collect(ctx context.Context) map[string]int64 {
if m.CollectFunc == nil {
return nil
}
return m.CollectFunc()
return m.CollectFunc(ctx)
}

// Cleanup sets CleanupDone to true.
func (m *MockModule) Cleanup() {
func (m *MockModule) Cleanup(ctx context.Context) {
if m.CleanupFunc != nil {
m.CleanupFunc()
m.CleanupFunc(ctx)
}
m.CleanupDone = true
}
Expand Down
26 changes: 16 additions & 10 deletions src/go/plugin/go.d/agent/module/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package module

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,18 +12,20 @@ import (

func TestMockModule_Init(t *testing.T) {
m := &MockModule{}
ctx := context.Background()

assert.NoError(t, m.Init())
m.InitFunc = func() error { return nil }
assert.NoError(t, m.Init())
assert.NoError(t, m.Init(ctx))
m.InitFunc = func(context.Context) error { return nil }
assert.NoError(t, m.Init(ctx))
}

func TestMockModule_Check(t *testing.T) {
m := &MockModule{}
ctx := context.Background()

assert.NoError(t, m.Check())
m.CheckFunc = func() error { return nil }
assert.NoError(t, m.Check())
assert.NoError(t, m.Check(ctx))
m.CheckFunc = func(context.Context) error { return nil }
assert.NoError(t, m.Check(ctx))
}

func TestMockModule_Charts(t *testing.T) {
Expand All @@ -39,16 +42,19 @@ func TestMockModule_Collect(t *testing.T) {
d := map[string]int64{
"1": 1,
}
ctx := context.Background()

assert.Nil(t, m.Collect())
m.CollectFunc = func() map[string]int64 { return d }
assert.Equal(t, d, m.Collect())
assert.Nil(t, m.Collect(ctx))
m.CollectFunc = func(ctx context.Context) map[string]int64 { return d }
assert.Equal(t, d, m.Collect(ctx))
}

func TestMockModule_Cleanup(t *testing.T) {
m := &MockModule{}
ctx := context.Background()

require.False(t, m.CleanupDone)

m.Cleanup()
m.Cleanup(ctx)
assert.True(t, m.CleanupDone)
}
Loading

0 comments on commit b723a59

Please sign in to comment.