flamenco/internal/manager/timeout_checker/timeout_checker_test.go
Sybren A. Stüvel b219f9b1c2 Manager tests: replace assert.NoError() with require.NoError()
Back in the days when I wrote the code, I didn't know about the
`require` package yet. Using `require.NoError()` makes the test code
more straight-forward.

No functional changes, except that when tests fail, they now fail
without panicking.
2024-03-16 12:14:39 +01:00

91 lines
2.4 KiB
Go

package timeout_checker
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"context"
"sync"
"testing"
"time"
"github.com/benbjohnson/clock"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"projects.blender.org/studio/flamenco/internal/manager/timeout_checker/mocks"
)
type TimeoutCheckerMocks struct {
clock *clock.Mock
persist *mocks.MockPersistenceService
taskStateMachine *mocks.MockTaskStateMachine
logStorage *mocks.MockLogStorage
broadcaster *mocks.MockChangeBroadcaster
ctx context.Context
cancel context.CancelFunc
wg *sync.WaitGroup
}
// run starts a goroutine to call ttc.Run(mocks.ctx).
func (mocks *TimeoutCheckerMocks) run(ttc *TimeoutChecker) {
mocks.wg.Add(1)
go func() {
defer mocks.wg.Done()
ttc.Run(mocks.ctx)
}()
}
func timeoutCheckerTestFixtures(t *testing.T) (*TimeoutChecker, func(), *TimeoutCheckerMocks) {
mockCtrl := gomock.NewController(t)
mocks := &TimeoutCheckerMocks{
clock: clock.NewMock(),
persist: mocks.NewMockPersistenceService(mockCtrl),
taskStateMachine: mocks.NewMockTaskStateMachine(mockCtrl),
logStorage: mocks.NewMockLogStorage(mockCtrl),
broadcaster: mocks.NewMockChangeBroadcaster(mockCtrl),
wg: new(sync.WaitGroup),
}
mockedNow, err := time.Parse(time.RFC3339, "2022-06-09T12:00:00+00:00")
require.NoError(t, err)
mocks.clock.Set(mockedNow)
ctx, cancel := context.WithCancel(context.Background())
mocks.ctx = ctx
mocks.cancel = cancel
// This should be called at the end of each unit test.
finish := func() {
mocks.cancel()
mocks.wg.Wait()
mockCtrl.Finish()
}
sm := New(
taskTimeout,
workerTimeout,
mocks.clock,
mocks.persist,
mocks.taskStateMachine,
mocks.logStorage,
mocks.broadcaster,
)
return sm, finish, mocks
}
// canaryTest will abort the current test if timing constants do not have the
// expected value. Unit tests will fail rather cryptically by themselves if the
// timing of the timeout checker is not what is expected.
func canaryTest(t *testing.T) {
if assert.Equal(t, 5*time.Minute, timeoutInitialSleep, "timeoutInitialSleep does not have the expected value") &&
assert.Equal(t, 1*time.Minute, timeoutCheckInterval, "timeoutCheckInterval does not have the expected value") {
return
}
t.Fatal("timing-related constants are not as expected by the unit test, preemptively aborting.")
t.FailNow()
}