chore: fix TestMeasureLatency/MeasureLatencyRecvTimeout flake (#13301)

This commit is contained in:
Colin Adler
2024-05-16 13:42:42 -05:00
committed by GitHub
parent cf91eff7cf
commit 85de0e966d
4 changed files with 117 additions and 8 deletions

View File

@ -0,0 +1,4 @@
// package psmock contains a mocked implementation of the pubsub.Pubsub interface for use in tests
package psmock
//go:generate mockgen -destination ./psmock.go -package psmock github.com/coder/coder/v2/coderd/database/pubsub Pubsub

View File

@ -0,0 +1,98 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/coder/coder/v2/coderd/database/pubsub (interfaces: Pubsub)
//
// Generated by this command:
//
// mockgen -destination ./psmock.go -package psmock github.com/coder/coder/v2/coderd/database/pubsub Pubsub
//
// Package psmock is a generated GoMock package.
package psmock
import (
reflect "reflect"
pubsub "github.com/coder/coder/v2/coderd/database/pubsub"
gomock "go.uber.org/mock/gomock"
)
// MockPubsub is a mock of Pubsub interface.
type MockPubsub struct {
ctrl *gomock.Controller
recorder *MockPubsubMockRecorder
}
// MockPubsubMockRecorder is the mock recorder for MockPubsub.
type MockPubsubMockRecorder struct {
mock *MockPubsub
}
// NewMockPubsub creates a new mock instance.
func NewMockPubsub(ctrl *gomock.Controller) *MockPubsub {
mock := &MockPubsub{ctrl: ctrl}
mock.recorder = &MockPubsubMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockPubsub) EXPECT() *MockPubsubMockRecorder {
return m.recorder
}
// Close mocks base method.
func (m *MockPubsub) Close() error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Close")
ret0, _ := ret[0].(error)
return ret0
}
// Close indicates an expected call of Close.
func (mr *MockPubsubMockRecorder) Close() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockPubsub)(nil).Close))
}
// Publish mocks base method.
func (m *MockPubsub) Publish(arg0 string, arg1 []byte) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Publish", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// Publish indicates an expected call of Publish.
func (mr *MockPubsubMockRecorder) Publish(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Publish", reflect.TypeOf((*MockPubsub)(nil).Publish), arg0, arg1)
}
// Subscribe mocks base method.
func (m *MockPubsub) Subscribe(arg0 string, arg1 pubsub.Listener) (func(), error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Subscribe", arg0, arg1)
ret0, _ := ret[0].(func())
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Subscribe indicates an expected call of Subscribe.
func (mr *MockPubsubMockRecorder) Subscribe(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Subscribe", reflect.TypeOf((*MockPubsub)(nil).Subscribe), arg0, arg1)
}
// SubscribeWithErr mocks base method.
func (m *MockPubsub) SubscribeWithErr(arg0 string, arg1 pubsub.ListenerWithErr) (func(), error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SubscribeWithErr", arg0, arg1)
ret0, _ := ret[0].(func())
ret1, _ := ret[1].(error)
return ret0, ret1
}
// SubscribeWithErr indicates an expected call of SubscribeWithErr.
func (mr *MockPubsubMockRecorder) SubscribeWithErr(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubscribeWithErr", reflect.TypeOf((*MockPubsub)(nil).SubscribeWithErr), arg0, arg1)
}

View File

@ -14,14 +14,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"golang.org/x/xerrors"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/database/pubsub"
"github.com/coder/coder/v2/coderd/database/pubsub/psmock"
"github.com/coder/coder/v2/testutil"
)
@ -339,15 +340,18 @@ func TestMeasureLatency(t *testing.T) {
t.Parallel()
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
ps, done := newPubsub()
defer done()
ctrl := gomock.NewController(t)
ps := psmock.NewMockPubsub(ctrl)
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Hour))
defer cancel()
ps.EXPECT().Subscribe(gomock.Any(), gomock.Any()).Return(func() {}, (error)(nil))
ps.EXPECT().Publish(gomock.Any(), gomock.Any()).Return((error)(nil))
ctx, cancel := context.WithCancel(context.Background())
cancel()
send, recv, err := pubsub.NewLatencyMeasurer(logger).Measure(ctx, ps)
require.ErrorContains(t, err, context.DeadlineExceeded.Error())
require.Greater(t, send.Seconds(), 0.0)
require.ErrorContains(t, err, context.Canceled.Error())
require.Greater(t, send.Nanoseconds(), int64(0))
require.EqualValues(t, recv, time.Duration(-1))
})