2017-04-18 08:22:06 +02:00
package middlewares
import (
2017-08-28 12:50:02 +02:00
"net/http"
"net/http/httptest"
2017-08-23 20:46:03 +02:00
"reflect"
2017-04-18 08:22:06 +02:00
"testing"
2018-01-26 11:58:03 +01:00
"github.com/containous/traefik/testhelpers"
2017-04-18 08:22:06 +02:00
"github.com/go-kit/kit/metrics"
)
func TestMetricsRetryListener ( t * testing . T ) {
2017-08-28 12:50:02 +02:00
req := httptest . NewRequest ( http . MethodGet , "/" , nil )
2017-08-23 20:46:03 +02:00
retryMetrics := newCollectingRetryMetrics ( )
retryListener := NewMetricsRetryListener ( retryMetrics , "backendName" )
2017-08-28 12:50:02 +02:00
retryListener . Retried ( req , 1 )
retryListener . Retried ( req , 2 )
2017-04-18 08:22:06 +02:00
wantCounterValue := float64 ( 2 )
2018-01-26 11:58:03 +01:00
if retryMetrics . retriesCounter . CounterValue != wantCounterValue {
2018-02-19 01:04:45 +01:00
t . Errorf ( "got counter value of %f, want %f" , retryMetrics . retriesCounter . CounterValue , wantCounterValue )
2017-04-18 08:22:06 +02:00
}
2017-08-23 20:46:03 +02:00
wantLabelValues := [ ] string { "backend" , "backendName" }
2018-01-26 11:58:03 +01:00
if ! reflect . DeepEqual ( retryMetrics . retriesCounter . LastLabelValues , wantLabelValues ) {
t . Errorf ( "wrong label values %v used, want %v" , retryMetrics . retriesCounter . LastLabelValues , wantLabelValues )
2017-08-23 20:46:03 +02:00
}
2017-04-18 08:22:06 +02:00
}
2017-08-23 20:46:03 +02: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 08:22:06 +02:00
type collectingRetryMetrics struct {
2018-01-26 11:58:03 +01:00
retriesCounter * testhelpers . CollectingCounter
2017-04-18 08:22:06 +02:00
}
2018-01-26 11:58:03 +01:00
func newCollectingRetryMetrics ( ) * collectingRetryMetrics {
return & collectingRetryMetrics { retriesCounter : & testhelpers . CollectingCounter { } }
2017-04-18 08:22:06 +02:00
}
2018-01-26 11:58:03 +01:00
func ( metrics * collectingRetryMetrics ) BackendRetriesCounter ( ) metrics . Counter {
return metrics . retriesCounter
2017-04-18 08:22:06 +02:00
}