Skip to content

Commit df8b6f5

Browse files
committed
Add unit test to check stopping gatherers on source config update
1 parent 41f8b72 commit df8b6f5

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

internal/reaper/reaper_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,57 @@ func TestReaper_LoadSources(t *testing.T) {
9494
assert.NoError(t, r.LoadSources())
9595
assert.Equal(t, 4, len(r.monitoredSources), "Expected four monitored sources after resetting groups")
9696
})
97+
98+
t.Run("Test source config update stops gatherers", func(t *testing.T) {
99+
// Track if cancel functions were called
100+
cancelCalled := make(map[string]bool)
101+
102+
source1 := sources.Source{
103+
Name: "Source1",
104+
IsEnabled: true,
105+
Kind: sources.SourcePostgres,
106+
ConnStr: "postgres://localhost:5432/db1",
107+
Metrics: map[string]float64{"metric1": 60, "metric2": 120},
108+
}
109+
110+
reader := &testutil.MockSourcesReaderWriter{
111+
GetSourcesFunc: func() (sources.Sources, error) {
112+
return sources.Sources{source1}, nil
113+
},
114+
}
115+
116+
r := NewReaper(ctx, &cmdopts.Options{SourcesReaderWriter: reader})
117+
assert.NoError(t, r.LoadSources())
118+
assert.Equal(t, 1, len(r.monitoredSources), "Expected one monitored source after initial load")
119+
120+
for i := range 2 {
121+
dbMetric1 := "Source1" + dbMetricJoinStr + "metric1"
122+
dbMetric2 := "Source1" + dbMetricJoinStr + "metric2"
123+
124+
cancelCalled[dbMetric1] = false
125+
cancelCalled[dbMetric2] = false
126+
127+
r.cancelFuncs[dbMetric1] = func() { cancelCalled[dbMetric1] = true }
128+
r.cancelFuncs[dbMetric2] = func() { cancelCalled[dbMetric2] = true }
129+
130+
if i == 0 {
131+
// Change conn str to trigger config change
132+
source1.ConnStr = "postgres://localhost:5432/db1_updated"
133+
} else {
134+
// Change metric1 interval to trigger config change
135+
source1.Metrics = map[string]float64{"metric1": 30, "metric2": 120}
136+
}
137+
138+
// Load sources again with updated config
139+
assert.NoError(t, r.LoadSources())
140+
141+
// Verify cancel functions were called
142+
assert.True(t, cancelCalled[dbMetric1], "Expected cancel function for metric1 to be called")
143+
assert.True(t, cancelCalled[dbMetric2], "Expected cancel function for metric2 to be called")
144+
145+
// Verify cancel funcs were removed from the map
146+
assert.Nil(t, r.cancelFuncs[dbMetric1], "Expected cancel function for metric1 to be removed")
147+
assert.Nil(t, r.cancelFuncs[dbMetric2], "Expected cancel function for metric2 to be removed")
148+
}
149+
})
97150
}

0 commit comments

Comments
 (0)