diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index a22610c..36792e5 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -173,9 +173,13 @@ func (s *localStorage[T]) sendMonitoringData(id string, o T, eventType EventType } func (s *localStorage[T]) periodicallySendMonitoringData(d time.Duration, ctx context.Context) { - for ctx.Err() == nil { - stub := new(T) - s.sendMonitoringData("", *stub, Periodically, s.Length()) - <-time.After(d) + for { + select { + case <-ctx.Done(): + return + case <-time.After(d): + stub := new(T) + s.sendMonitoringData("", *stub, Periodically, s.Length()) + } } } diff --git a/pkg/storage/storage_test.go b/pkg/storage/storage_test.go index 46b5e6e..86f9572 100644 --- a/pkg/storage/storage_test.go +++ b/pkg/storage/storage_test.go @@ -181,10 +181,12 @@ func TestNewMonitoredLocalStorage_Periodically(t *testing.T) { NewMonitoredLocalStorage[string]("testMeasurement", func(p *write.Point, o string, eventType EventType) { callbackCalls++ assert.Equal(t, Periodically, eventType) - }, 200*time.Millisecond, ctx) + }, 2*tests.ShortTimeout, ctx) - time.Sleep(tests.ShortTimeout) + <-time.After(tests.ShortTimeout) + assert.Equal(t, 0, callbackCalls) + <-time.After(2 * tests.ShortTimeout) assert.Equal(t, 1, callbackCalls) - time.Sleep(200 * time.Millisecond) + <-time.After(2 * tests.ShortTimeout) assert.Equal(t, 2, callbackCalls) }