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
This commit is contained in:
Vardan Torosyan 2020-12-16 16:12:02 +01:00 committed by GitHub
parent 56b8124afb
commit de22374751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -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 {

View File

@ -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{