traefik/middlewares/metrics_test.go

43 lines
1.4 KiB
Go
Raw Normal View History

2017-04-18 06:22:06 +00:00
package middlewares
import (
"net/http"
"net/http/httptest"
"reflect"
2017-04-18 06:22:06 +00:00
"testing"
"github.com/containous/traefik/testhelpers"
2017-04-18 06:22:06 +00:00
"github.com/go-kit/kit/metrics"
)
func TestMetricsRetryListener(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
retryMetrics := newCollectingRetryMetrics()
retryListener := NewMetricsRetryListener(retryMetrics, "backendName")
retryListener.Retried(req, 1)
retryListener.Retried(req, 2)
2017-04-18 06:22:06 +00:00
wantCounterValue := float64(2)
if retryMetrics.retriesCounter.CounterValue != wantCounterValue {
t.Errorf("got counter value of %d, want %d", retryMetrics.retriesCounter.CounterValue, wantCounterValue)
2017-04-18 06:22:06 +00:00
}
wantLabelValues := []string{"backend", "backendName"}
if !reflect.DeepEqual(retryMetrics.retriesCounter.LastLabelValues, wantLabelValues) {
t.Errorf("wrong label values %v used, want %v", retryMetrics.retriesCounter.LastLabelValues, wantLabelValues)
}
2017-04-18 06:22:06 +00:00
}
// collectingRetryMetrics is an implementation of the retryMetrics interface that can be used inside tests to collect the times Add() was called.
2017-04-18 06:22:06 +00:00
type collectingRetryMetrics struct {
retriesCounter *testhelpers.CollectingCounter
2017-04-18 06:22:06 +00:00
}
func newCollectingRetryMetrics() *collectingRetryMetrics {
return &collectingRetryMetrics{retriesCounter: &testhelpers.CollectingCounter{}}
2017-04-18 06:22:06 +00:00
}
func (metrics *collectingRetryMetrics) BackendRetriesCounter() metrics.Counter {
return metrics.retriesCounter
2017-04-18 06:22:06 +00:00
}