Usage Stats: Introduce an interface for usage stats service (#29882)

Adding an interface type for usage stats service allows us to not depend on the implementation outside of the package, for example when testing we can easily mock the service
pull/29889/head
Vardan Torosyan 5 years ago committed by GitHub
parent 56b8124afb
commit de22374751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      pkg/infra/usagestats/service.go
  2. 11
      pkg/infra/usagestats/usage_stats_test.go

@ -21,6 +21,12 @@ func init() {
registry.RegisterService(&UsageStatsService{})
}
type UsageStats interface {
GetUsageReport() (UsageReport, error)
RegisterMetric(name string, fn MetricFunc)
}
type MetricFunc func() (interface{}, error)
type UsageStatsService struct {

@ -25,6 +25,17 @@ import (
"github.com/stretchr/testify/assert"
)
// This is to ensure that the interface contract is held by the implementation
func Test_InterfaceContractValidity(t *testing.T) {
newUsageStats := func() UsageStats {
return &UsageStatsService{}
}
v, ok := newUsageStats().(*UsageStatsService)
assert.NotNil(t, v)
assert.True(t, ok)
}
func TestMetrics(t *testing.T) {
t.Run("When sending usage stats", func(t *testing.T) {
uss := &UsageStatsService{

Loading…
Cancel
Save