Fix not canceling monitoring events for removed environments
and runners.
This commit is contained in:

committed by
Sebastian Serth

parent
5d54b0f786
commit
7119f3e012
@ -75,14 +75,14 @@ func NewLocalStorage[T any]() *localStorage[T] {
|
||||
// Iff callback is set, it will be called on a write operation.
|
||||
// Iff additionalEvents not zero, the duration will be used to periodically send additional monitoring events.
|
||||
func NewMonitoredLocalStorage[T any](
|
||||
measurement string, callback WriteCallback[T], additionalEvents time.Duration) *localStorage[T] {
|
||||
measurement string, callback WriteCallback[T], additionalEvents time.Duration, ctx context.Context) *localStorage[T] {
|
||||
s := &localStorage[T]{
|
||||
objects: make(map[string]T),
|
||||
measurement: measurement,
|
||||
callback: callback,
|
||||
}
|
||||
if additionalEvents != 0 {
|
||||
go s.periodicallySendMonitoringData(additionalEvents)
|
||||
go s.periodicallySendMonitoringData(additionalEvents, ctx)
|
||||
}
|
||||
return s
|
||||
}
|
||||
@ -172,8 +172,7 @@ func (s *localStorage[T]) sendMonitoringData(id string, o T, eventType EventType
|
||||
}
|
||||
}
|
||||
|
||||
func (s *localStorage[T]) periodicallySendMonitoringData(d time.Duration) {
|
||||
ctx := context.Background()
|
||||
func (s *localStorage[T]) periodicallySendMonitoringData(d time.Duration, ctx context.Context) {
|
||||
for ctx.Err() == nil {
|
||||
stub := new(T)
|
||||
s.sendMonitoringData("", *stub, Periodically, s.Length())
|
||||
|
@ -1,6 +1,7 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/influxdata/influxdb-client-go/v2/api/write"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -123,7 +124,7 @@ func TestNewMonitoredLocalStorage_Callback(t *testing.T) {
|
||||
} else if eventType == Creation {
|
||||
callbackAdditions++
|
||||
}
|
||||
}, 0)
|
||||
}, 0, context.Background())
|
||||
|
||||
assertCallbackCounts := func(test func(), totalCalls, additions, deletions int) {
|
||||
beforeTotal := callbackCalls
|
||||
@ -174,11 +175,13 @@ func TestNewMonitoredLocalStorage_Callback(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewMonitoredLocalStorage_Periodically(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
callbackCalls := 0
|
||||
NewMonitoredLocalStorage[string]("testMeasurement", func(p *write.Point, o string, eventType EventType) {
|
||||
callbackCalls++
|
||||
assert.Equal(t, Periodically, eventType)
|
||||
}, 200*time.Millisecond)
|
||||
}, 200*time.Millisecond, ctx)
|
||||
|
||||
time.Sleep(tests.ShortTimeout)
|
||||
assert.Equal(t, 1, callbackCalls)
|
||||
|
Reference in New Issue
Block a user