sourcegraph/lib/background/mocks_test.go
Joe Chen 2589fef13e
lib/background: upgrade Routine interface with context and errors (#62136)
This PR is a result/followup of the improvements we've made in the [SAMS repo](https://github.com/sourcegraph/sourcegraph-accounts/pull/199) that allows call sites to pass down a context (primarily to indicate deadline, and of course, cancellation if desired) and collects the error returned from `background.Routine`s `Stop` method.

Note that I did not adopt returning error from `Stop` method because I realize in monorepo, the more common (and arguably the desired) pattern is to hang on the call of `Start` method until `Stop` is called, so it is meaningless to collect errors from `Start` methods as return values anyway, and doing that would also complicate the design and semantics more than necessary.

All usages of the the `background.Routine` and `background.CombinedRoutines` are updated, I DID NOT try to interpret the code logic and make anything better other than fixing compile and test errors.

The only file that contains the core change is the [`lib/background/background.go`](https://github.com/sourcegraph/sourcegraph/pull/62136/files#diff-65c3228388620e91f8c22d91c18faac3f985fc67d64b08612df18fa7c04fafcd).
2024-05-24 10:04:55 -04:00

381 lines
10 KiB
Go
Generated

// Code generated by go-mockgen 1.3.7; DO NOT EDIT.
//
// This file was generated by running `sg generate` (or `go-mockgen`) at the root of
// this repository. To add additional mocks to this or another package, add a new entry
// to the mockgen.yaml file in the root of this repository.
package background
import (
"context"
"sync"
)
// MockRoutine is a mock implementation of the Routine interface (from the
// package github.com/sourcegraph/sourcegraph/lib/background) used for unit
// testing.
type MockRoutine struct {
// NameFunc is an instance of a mock function object controlling the
// behavior of the method Name.
NameFunc *RoutineNameFunc
// StartFunc is an instance of a mock function object controlling the
// behavior of the method Start.
StartFunc *RoutineStartFunc
// StopFunc is an instance of a mock function object controlling the
// behavior of the method Stop.
StopFunc *RoutineStopFunc
}
// NewMockRoutine creates a new mock of the Routine interface. All methods
// return zero values for all results, unless overwritten.
func NewMockRoutine() *MockRoutine {
return &MockRoutine{
NameFunc: &RoutineNameFunc{
defaultHook: func() (r0 string) {
return
},
},
StartFunc: &RoutineStartFunc{
defaultHook: func() {
return
},
},
StopFunc: &RoutineStopFunc{
defaultHook: func(context.Context) (r0 error) {
return
},
},
}
}
// NewStrictMockRoutine creates a new mock of the Routine interface. All
// methods panic on invocation, unless overwritten.
func NewStrictMockRoutine() *MockRoutine {
return &MockRoutine{
NameFunc: &RoutineNameFunc{
defaultHook: func() string {
panic("unexpected invocation of MockRoutine.Name")
},
},
StartFunc: &RoutineStartFunc{
defaultHook: func() {
panic("unexpected invocation of MockRoutine.Start")
},
},
StopFunc: &RoutineStopFunc{
defaultHook: func(context.Context) error {
panic("unexpected invocation of MockRoutine.Stop")
},
},
}
}
// NewMockRoutineFrom creates a new mock of the MockRoutine interface. All
// methods delegate to the given implementation, unless overwritten.
func NewMockRoutineFrom(i Routine) *MockRoutine {
return &MockRoutine{
NameFunc: &RoutineNameFunc{
defaultHook: i.Name,
},
StartFunc: &RoutineStartFunc{
defaultHook: i.Start,
},
StopFunc: &RoutineStopFunc{
defaultHook: i.Stop,
},
}
}
// RoutineNameFunc describes the behavior when the Name method of the parent
// MockRoutine instance is invoked.
type RoutineNameFunc struct {
defaultHook func() string
hooks []func() string
history []RoutineNameFuncCall
mutex sync.Mutex
}
// Name delegates to the next hook function in the queue and stores the
// parameter and result values of this invocation.
func (m *MockRoutine) Name() string {
r0 := m.NameFunc.nextHook()()
m.NameFunc.appendCall(RoutineNameFuncCall{r0})
return r0
}
// SetDefaultHook sets function that is called when the Name method of the
// parent MockRoutine instance is invoked and the hook queue is empty.
func (f *RoutineNameFunc) SetDefaultHook(hook func() string) {
f.defaultHook = hook
}
// PushHook adds a function to the end of hook queue. Each invocation of the
// Name method of the parent MockRoutine instance invokes the hook at the
// front of the queue and discards it. After the queue is empty, the default
// hook function is invoked for any future action.
func (f *RoutineNameFunc) PushHook(hook func() string) {
f.mutex.Lock()
f.hooks = append(f.hooks, hook)
f.mutex.Unlock()
}
// SetDefaultReturn calls SetDefaultHook with a function that returns the
// given values.
func (f *RoutineNameFunc) SetDefaultReturn(r0 string) {
f.SetDefaultHook(func() string {
return r0
})
}
// PushReturn calls PushHook with a function that returns the given values.
func (f *RoutineNameFunc) PushReturn(r0 string) {
f.PushHook(func() string {
return r0
})
}
func (f *RoutineNameFunc) nextHook() func() string {
f.mutex.Lock()
defer f.mutex.Unlock()
if len(f.hooks) == 0 {
return f.defaultHook
}
hook := f.hooks[0]
f.hooks = f.hooks[1:]
return hook
}
func (f *RoutineNameFunc) appendCall(r0 RoutineNameFuncCall) {
f.mutex.Lock()
f.history = append(f.history, r0)
f.mutex.Unlock()
}
// History returns a sequence of RoutineNameFuncCall objects describing the
// invocations of this function.
func (f *RoutineNameFunc) History() []RoutineNameFuncCall {
f.mutex.Lock()
history := make([]RoutineNameFuncCall, len(f.history))
copy(history, f.history)
f.mutex.Unlock()
return history
}
// RoutineNameFuncCall is an object that describes an invocation of method
// Name on an instance of MockRoutine.
type RoutineNameFuncCall struct {
// Result0 is the value of the 1st result returned from this method
// invocation.
Result0 string
}
// Args returns an interface slice containing the arguments of this
// invocation.
func (c RoutineNameFuncCall) Args() []interface{} {
return []interface{}{}
}
// Results returns an interface slice containing the results of this
// invocation.
func (c RoutineNameFuncCall) Results() []interface{} {
return []interface{}{c.Result0}
}
// RoutineStartFunc describes the behavior when the Start method of the
// parent MockRoutine instance is invoked.
type RoutineStartFunc struct {
defaultHook func()
hooks []func()
history []RoutineStartFuncCall
mutex sync.Mutex
}
// Start delegates to the next hook function in the queue and stores the
// parameter and result values of this invocation.
func (m *MockRoutine) Start() {
m.StartFunc.nextHook()()
m.StartFunc.appendCall(RoutineStartFuncCall{})
return
}
// SetDefaultHook sets function that is called when the Start method of the
// parent MockRoutine instance is invoked and the hook queue is empty.
func (f *RoutineStartFunc) SetDefaultHook(hook func()) {
f.defaultHook = hook
}
// PushHook adds a function to the end of hook queue. Each invocation of the
// Start method of the parent MockRoutine instance invokes the hook at the
// front of the queue and discards it. After the queue is empty, the default
// hook function is invoked for any future action.
func (f *RoutineStartFunc) PushHook(hook func()) {
f.mutex.Lock()
f.hooks = append(f.hooks, hook)
f.mutex.Unlock()
}
// SetDefaultReturn calls SetDefaultHook with a function that returns the
// given values.
func (f *RoutineStartFunc) SetDefaultReturn() {
f.SetDefaultHook(func() {
return
})
}
// PushReturn calls PushHook with a function that returns the given values.
func (f *RoutineStartFunc) PushReturn() {
f.PushHook(func() {
return
})
}
func (f *RoutineStartFunc) nextHook() func() {
f.mutex.Lock()
defer f.mutex.Unlock()
if len(f.hooks) == 0 {
return f.defaultHook
}
hook := f.hooks[0]
f.hooks = f.hooks[1:]
return hook
}
func (f *RoutineStartFunc) appendCall(r0 RoutineStartFuncCall) {
f.mutex.Lock()
f.history = append(f.history, r0)
f.mutex.Unlock()
}
// History returns a sequence of RoutineStartFuncCall objects describing the
// invocations of this function.
func (f *RoutineStartFunc) History() []RoutineStartFuncCall {
f.mutex.Lock()
history := make([]RoutineStartFuncCall, len(f.history))
copy(history, f.history)
f.mutex.Unlock()
return history
}
// RoutineStartFuncCall is an object that describes an invocation of method
// Start on an instance of MockRoutine.
type RoutineStartFuncCall struct{}
// Args returns an interface slice containing the arguments of this
// invocation.
func (c RoutineStartFuncCall) Args() []interface{} {
return []interface{}{}
}
// Results returns an interface slice containing the results of this
// invocation.
func (c RoutineStartFuncCall) Results() []interface{} {
return []interface{}{}
}
// RoutineStopFunc describes the behavior when the Stop method of the parent
// MockRoutine instance is invoked.
type RoutineStopFunc struct {
defaultHook func(context.Context) error
hooks []func(context.Context) error
history []RoutineStopFuncCall
mutex sync.Mutex
}
// Stop delegates to the next hook function in the queue and stores the
// parameter and result values of this invocation.
func (m *MockRoutine) Stop(v0 context.Context) error {
r0 := m.StopFunc.nextHook()(v0)
m.StopFunc.appendCall(RoutineStopFuncCall{v0, r0})
return r0
}
// SetDefaultHook sets function that is called when the Stop method of the
// parent MockRoutine instance is invoked and the hook queue is empty.
func (f *RoutineStopFunc) SetDefaultHook(hook func(context.Context) error) {
f.defaultHook = hook
}
// PushHook adds a function to the end of hook queue. Each invocation of the
// Stop method of the parent MockRoutine instance invokes the hook at the
// front of the queue and discards it. After the queue is empty, the default
// hook function is invoked for any future action.
func (f *RoutineStopFunc) PushHook(hook func(context.Context) error) {
f.mutex.Lock()
f.hooks = append(f.hooks, hook)
f.mutex.Unlock()
}
// SetDefaultReturn calls SetDefaultHook with a function that returns the
// given values.
func (f *RoutineStopFunc) SetDefaultReturn(r0 error) {
f.SetDefaultHook(func(context.Context) error {
return r0
})
}
// PushReturn calls PushHook with a function that returns the given values.
func (f *RoutineStopFunc) PushReturn(r0 error) {
f.PushHook(func(context.Context) error {
return r0
})
}
func (f *RoutineStopFunc) nextHook() func(context.Context) error {
f.mutex.Lock()
defer f.mutex.Unlock()
if len(f.hooks) == 0 {
return f.defaultHook
}
hook := f.hooks[0]
f.hooks = f.hooks[1:]
return hook
}
func (f *RoutineStopFunc) appendCall(r0 RoutineStopFuncCall) {
f.mutex.Lock()
f.history = append(f.history, r0)
f.mutex.Unlock()
}
// History returns a sequence of RoutineStopFuncCall objects describing the
// invocations of this function.
func (f *RoutineStopFunc) History() []RoutineStopFuncCall {
f.mutex.Lock()
history := make([]RoutineStopFuncCall, len(f.history))
copy(history, f.history)
f.mutex.Unlock()
return history
}
// RoutineStopFuncCall is an object that describes an invocation of method
// Stop on an instance of MockRoutine.
type RoutineStopFuncCall struct {
// Arg0 is the value of the 1st argument passed to this method
// invocation.
Arg0 context.Context
// Result0 is the value of the 1st result returned from this method
// invocation.
Result0 error
}
// Args returns an interface slice containing the arguments of this
// invocation.
func (c RoutineStopFuncCall) Args() []interface{} {
return []interface{}{c.Arg0}
}
// Results returns an interface slice containing the results of this
// invocation.
func (c RoutineStopFuncCall) Results() []interface{} {
return []interface{}{c.Result0}
}