pkg/telemetry: use smaller mock in test (#370)

This commit is contained in:
Egon Elbre 2018-09-26 17:00:54 +03:00 committed by GitHub
parent 821d0b6f1d
commit 550858c5a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 135 deletions

View File

@ -60,6 +60,7 @@ type ClientOpts struct {
type Client struct {
interval time.Duration
opts admmonkit.Options
send func(context.Context, admmonkit.Options) error
}
// NewClient constructs a telemetry client that sends packets to remoteAddr
@ -88,6 +89,7 @@ func NewClient(remoteAddr string, opts ClientOpts) (rv *Client, err error) {
return &Client{
interval: opts.Interval,
send: admmonkit.Send,
opts: admmonkit.Options{
Application: opts.Application,
InstanceId: []byte(opts.Instance),
@ -106,6 +108,7 @@ func (c *Client) Run(ctx context.Context) {
if ctx.Err() != nil {
return
}
err := c.Report(ctx)
if err != nil {
zap.S().Errorf("failed sending telemetry report: %v", err)
@ -115,5 +118,5 @@ func (c *Client) Run(ctx context.Context) {
// Report bundles up all the current stats and writes them out as UDP packets
func (c *Client) Report(ctx context.Context) error {
return admmonkit.Send(ctx, c.opts)
return c.send(ctx, c.opts)
}

View File

@ -3,11 +3,13 @@
package telemetry
import (
"errors"
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zeebo/admission/admmonkit"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
)
@ -127,14 +129,21 @@ func TestNewClient_PacketSizeIsZero(t *testing.T) {
}
func TestRun_ReportNoCalled(t *testing.T) {
client := &MockClient{}
client, err := NewClient("127.0.0.1:0", ClientOpts{
Application: "qwe",
Instance: "",
Interval: time.Millisecond,
PacketSize: 0,
})
assert.NoError(t, err)
ctx := &MockContext{}
client.send = func(context.Context, admmonkit.Options) error {
t.Fatal("shouldn't be called")
return nil
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
ctx.On("Err").Return(errors.New("")).Once()
client.On("Report").Times(0)
client.On("Run", ctx).Once()
client.Run(ctx)
ctx.AssertExpectations(t)
}

View File

@ -1,83 +0,0 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
// Code generated by mockery v1.0.0. DO NOT EDIT.
package telemetry
import (
"time"
mock "github.com/stretchr/testify/mock"
)
// MockContext is an autogenerated mock type for the Cont type
type MockContext struct {
mock.Mock
}
// Deadline provides a mock function with given fields:
func (_m *MockContext) Deadline() (time.Time, bool) {
ret := _m.Called()
var r0 time.Time
if rf, ok := ret.Get(0).(func() time.Time); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(time.Time)
}
var r1 bool
if rf, ok := ret.Get(1).(func() bool); ok {
r1 = rf()
} else {
r1 = ret.Get(1).(bool)
}
return r0, r1
}
// Done provides a mock function with given fields:
func (_m *MockContext) Done() <-chan struct{} {
ret := _m.Called()
var r0 <-chan struct{}
if rf, ok := ret.Get(0).(func() <-chan struct{}); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(<-chan struct{})
}
}
return r0
}
// Err provides a mock function with given fields:
func (_m *MockContext) Err() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Value provides a mock function with given fields: key
func (_m *MockContext) Value(key interface{}) interface{} {
ret := _m.Called(key)
var r0 interface{}
if rf, ok := ret.Get(0).(func(interface{}) interface{}); ok {
r0 = rf(key)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(interface{})
}
}
return r0
}

View File

@ -1,43 +0,0 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
// Code generated by mockery v1.0.0. DO NOT EDIT.
package telemetry
import (
context "context"
mock "github.com/stretchr/testify/mock"
)
// MockClient is an autogenerated mock type for the client type
type MockClient struct {
mock.Mock
// client Client
}
// Report provides a mock function with given fields: ctx
func (_m *MockClient) Report(ctx context.Context) error {
ret := _m.Called(ctx)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context) error); ok {
r0 = rf(ctx)
} else {
r0 = ret.Error(0)
}
return r0
}
// Run provides a mock function with given fields: ctx
func (_m *MockClient) Run(ctx context.Context) {
client, _ := NewClient("", ClientOpts{
Application: "qwe",
Instance: "",
Interval: 2,
PacketSize: 0,
})
client.Run(ctx)
}