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 authored and GitHub committed 2020-12-16 16:12:02 +01:00
1 parent 56b8124afb
commit de22374751
2 files changed
+17

No files matched your search

+6
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 {
+11
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{