diff --git a/CHANGELOG.md b/CHANGELOG.md index e63da16c..f4bd0524 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Optimized the job completer's query `JobSetStateIfRunningMany`, resulting in an approximately 15% reduction in its duration when completing 2000 jobs, and around a 15-20% increase in `riverbench` throughput. [PR #904](https://github.com/riverqueue/river/pull/904). +- `TimeStub` has been removed from the `rivertest` package. Its original inclusion was entirely accidentally and it should be considered entirely an internal API. [PR #912](https://github.com/riverqueue/river/pull/912). ### Fixed diff --git a/rivertest/time_stub.go b/rivertest/time_stub.go deleted file mode 100644 index 860720b8..00000000 --- a/rivertest/time_stub.go +++ /dev/null @@ -1,48 +0,0 @@ -package rivertest - -import ( - "sync" - "time" -) - -// TimeStub implements rivertype.TimeGenerator to allow time to be stubbed in -// tests. It is implemented in a thread-safe manner with a mutex, allowing the -// current time to be stubbed at any time with StubNowUTC. -type TimeStub struct { - mu sync.RWMutex - nowUTC *time.Time -} - -// NowUTC returns the current time. This may be a stubbed time if the time has -// been actively stubbed in a test. -func (t *TimeStub) NowUTC() time.Time { - t.mu.RLock() - defer t.mu.RUnlock() - - if t.nowUTC == nil { - return time.Now().UTC() - } - - return *t.nowUTC -} - -// NowUTCOrNil returns if the currently stubbed time _if_ the current time -// is stubbed, and returns nil otherwise. This is generally useful in cases -// where a component may want to use a stubbed time if the time is stubbed, -// but to fall back to a database time default otherwise. -func (t *TimeStub) NowUTCOrNil() *time.Time { - t.mu.RLock() - defer t.mu.RUnlock() - - return t.nowUTC -} - -// StubNowUTC stubs the current time. It will panic if invoked outside of tests. -// Returns the same time passed as parameter for convenience. -func (t *TimeStub) StubNowUTC(nowUTC time.Time) time.Time { - t.mu.Lock() - defer t.mu.Unlock() - - t.nowUTC = &nowUTC - return nowUTC -} diff --git a/rivertest/time_stub_test.go b/rivertest/time_stub_test.go deleted file mode 100644 index 8bc7f5b8..00000000 --- a/rivertest/time_stub_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package rivertest - -import ( - "testing" - "time" - - "github.com/stretchr/testify/require" - - "github.com/riverqueue/river/rivertype" -) - -// Ensure that TimeStub implements rivertype.TimeGenerator. -var _ rivertype.TimeGenerator = &TimeStub{} - -func TestTimeStub(t *testing.T) { - t.Parallel() - - stub := &TimeStub{} - - now := time.Now().UTC() - - require.WithinDuration(t, now, stub.NowUTC(), 2*time.Second) - require.Nil(t, stub.NowUTCOrNil()) - - stub.StubNowUTC(now) - require.Equal(t, now, stub.NowUTC()) - require.Equal(t, &now, stub.NowUTCOrNil()) -} diff --git a/rivertest/worker_test.go b/rivertest/worker_test.go index 77816bf6..61ced076 100644 --- a/rivertest/worker_test.go +++ b/rivertest/worker_test.go @@ -255,7 +255,7 @@ func TestWorker_Work(t *testing.T) { bundle := setup(t) hourFromNow := time.Now().UTC().Add(1 * time.Hour) - timeStub := &TimeStub{} + timeStub := &riversharedtest.TimeStub{} timeStub.StubNowUTC(hourFromNow) bundle.config.Test.Time = timeStub