-
Notifications
You must be signed in to change notification settings - Fork 274
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
Add clock interface to make testing and process a lot easier #31
Conversation
Hi, @pcman312 .
How about making customizable function like the above? |
That approach would definitely work, though it limits flexibility and forces the entire library to use a single Now function instead of making it specific to the cache instance.
That way you have the flexibility of using clockwork (or any other library that has a Thanks @bluele! |
@pcman312 cache.SetWithExpire("key", "value", 3*time.Second)
gcache.Now = func() time.Time {
return time.Now().Add(3*time.Second)
}
// expects KeyNotFound error
_, err := cache.Get("key") So, in our case, I think the exported Now function is easier to use. |
There are several serious problems with making a global
Here are 4 possible solutions (from the context of unit tests): Use global
|
Thank you for contributing. Imho, in the interest of better testing and maintainability (in addition to the reasons above mentioned) it is better to go with a pluggable fake clock object on a per cache basis. We lack tests for the Heads up, you have included your |
Thanks @aaronwinter! I left everything as is since the start of the conversation so we could settle on how we wanted to handle this. I'll get the PR updated and let you guys know when it's ready. |
Thank you very much for sorting out an issue.
I think this solution is best. What do you think of the definition like the following? type Clock interface {
func Now() time.Time
}
func (cb *CacheBuilder) Clock(clock Clock) *CacheBuilder |
@bluele That's basically what I had intended on building. I am closing this PR and opening another with those changes. I'll reference this PR in the description. |
No description provided.