-
Notifications
You must be signed in to change notification settings - Fork 167
expose Config.Test with Time setting
#754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package rivertest | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/riverqueue/river/rivertype" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // 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()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package rivertype | ||
|
|
||
| import "time" | ||
|
|
||
| // TimeGenerator generates a current time in UTC. In test environments it's | ||
| // implemented by riverinternaltest.timeStub which lets the current time be | ||
| // stubbed. Otherwise, it's implemented as UnStubbableTimeGenerator which | ||
| // doesn't allow stubbing. | ||
| type TimeGenerator interface { | ||
| // NowUTC returns the current time. This may be a stubbed time if the time | ||
| // has been actively stubbed in a test. | ||
| NowUTC() time.Time | ||
|
|
||
| // 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. | ||
| NowUTCOrNil() *time.Time | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could just call this
Stub? It doesn't even need to match the internalbaseservice.TImeGeneratorWithStubbecause there's a circular dependency and that one has to exist separately. Your call!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, unless we're going to rename the other two functions, I'd probably leave it. Just seems more consistent.