mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-07 10:17:51 +00:00
Tests for periodicMetricLister.
This commit is contained in:
parent
bf9be4e53a
commit
726fc1effc
2 changed files with 72 additions and 1 deletions
|
|
@ -71,7 +71,9 @@ func (l *periodicMetricLister) updateMetrics() error {
|
||||||
//Cache the result.
|
//Cache the result.
|
||||||
l.mostRecentResult = result
|
l.mostRecentResult = result
|
||||||
//Let our listener know we've got new data ready for them.
|
//Let our listener know we've got new data ready for them.
|
||||||
l.callback(result)
|
if l.callback != nil {
|
||||||
|
l.callback(result)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
69
pkg/custom-provider/periodic_metric_lister_test.go
Normal file
69
pkg/custom-provider/periodic_metric_lister_test.go
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fakeLister struct {
|
||||||
|
callCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeLister) ListAllMetrics() (metricUpdateResult, error) {
|
||||||
|
f.callCount++
|
||||||
|
|
||||||
|
return metricUpdateResult{
|
||||||
|
series: [][]prom.Series{
|
||||||
|
[]prom.Series{
|
||||||
|
prom.Series{
|
||||||
|
Name: "a_series",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWhenNewMetricsAvailableCallbackIsInvoked(t *testing.T) {
|
||||||
|
fakeLister := &fakeLister{}
|
||||||
|
targetLister, _ := NewPeriodicMetricLister(fakeLister, time.Duration(1000))
|
||||||
|
periodicLister := targetLister.(*periodicMetricLister)
|
||||||
|
|
||||||
|
callbackInvoked := false
|
||||||
|
callback := func(r metricUpdateResult) {
|
||||||
|
callbackInvoked = true
|
||||||
|
}
|
||||||
|
|
||||||
|
periodicLister.SetNotificationReceiver(callback)
|
||||||
|
err := periodicLister.updateMetrics()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, callbackInvoked)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWhenListingMetricsReturnsCachedValues(t *testing.T) {
|
||||||
|
fakeLister := &fakeLister{}
|
||||||
|
targetLister, _ := NewPeriodicMetricLister(fakeLister, time.Duration(1000))
|
||||||
|
periodicLister := targetLister.(*periodicMetricLister)
|
||||||
|
|
||||||
|
//We haven't invoked the inner lister yet, so we should have no results.
|
||||||
|
resultBeforeUpdate, err := periodicLister.ListAllMetrics()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 0, len(resultBeforeUpdate.series))
|
||||||
|
require.Equal(t, 0, fakeLister.callCount)
|
||||||
|
|
||||||
|
//We can simulate waiting for the udpate interval to pass...
|
||||||
|
//which should result in calling the inner lister to get the metrics.
|
||||||
|
err = periodicLister.updateMetrics()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 1, fakeLister.callCount)
|
||||||
|
|
||||||
|
//If we list now, we should return the cached values.
|
||||||
|
//Make sure we got some results this time
|
||||||
|
//as well as that we didn't unnecessarily invoke the inner lister.
|
||||||
|
resultAfterUpdate, err := periodicLister.ListAllMetrics()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotEqual(t, 0, len(resultAfterUpdate.series))
|
||||||
|
require.Equal(t, 1, fakeLister.callCount)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue