GODT-1431 Prevent watcher when not using disk on cache

- change: Rename Cacher -> MsgCachePool
- change: Do not run watcher when using memory cache
- add: Allow to cancel cacher jobs (added context)
- change: Fix behavior on context cancel (was causing no internet)
This commit is contained in:
Jakub
2021-11-22 15:12:15 +01:00
parent b82e2ca176
commit d7c5ace8e4
12 changed files with 81 additions and 58 deletions

View File

@ -25,7 +25,7 @@ import (
"github.com/pkg/errors"
)
func withTestCacher(t *testing.T, doTest func(storer *storemocks.MockStorer, cacher *Cacher)) {
func withTestCacher(t *testing.T, doTest func(storer *storemocks.MockStorer, cacher *MsgCachePool)) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
@ -33,7 +33,7 @@ func withTestCacher(t *testing.T, doTest func(storer *storemocks.MockStorer, cac
storer := storemocks.NewMockStorer(ctrl)
// Create a new cacher pointing to the fake store.
cacher := newCacher(storer)
cacher := newMsgCachePool(storer)
// Start the cacher and wait for it to stop.
cacher.start()
@ -44,16 +44,16 @@ func withTestCacher(t *testing.T, doTest func(storer *storemocks.MockStorer, cac
func TestCacher(t *testing.T) {
// If the message is not yet cached, we should expect to try to build and cache it.
withTestCacher(t, func(storer *storemocks.MockStorer, cacher *Cacher) {
withTestCacher(t, func(storer *storemocks.MockStorer, cacher *MsgCachePool) {
storer.EXPECT().IsCached("messageID").Return(false)
storer.EXPECT().BuildAndCacheMessage("messageID").Return(nil)
storer.EXPECT().BuildAndCacheMessage(cacher.ctx, "messageID").Return(nil)
cacher.newJob("messageID")
})
}
func TestCacherAlreadyCached(t *testing.T) {
// If the message is already cached, we should not try to build it.
withTestCacher(t, func(storer *storemocks.MockStorer, cacher *Cacher) {
withTestCacher(t, func(storer *storemocks.MockStorer, cacher *MsgCachePool) {
storer.EXPECT().IsCached("messageID").Return(true)
cacher.newJob("messageID")
})
@ -61,9 +61,9 @@ func TestCacherAlreadyCached(t *testing.T) {
func TestCacherFail(t *testing.T) {
// If building the message fails, we should not try to cache it.
withTestCacher(t, func(storer *storemocks.MockStorer, cacher *Cacher) {
withTestCacher(t, func(storer *storemocks.MockStorer, cacher *MsgCachePool) {
storer.EXPECT().IsCached("messageID").Return(false)
storer.EXPECT().BuildAndCacheMessage("messageID").Return(errors.New("failed to build message"))
storer.EXPECT().BuildAndCacheMessage(cacher.ctx, "messageID").Return(errors.New("failed to build message"))
cacher.newJob("messageID")
})
}
@ -76,14 +76,14 @@ func TestCacherStop(t *testing.T) {
storer := storemocks.NewMockStorer(ctrl)
// Create a new cacher pointing to the fake store.
cacher := newCacher(storer)
cacher := newMsgCachePool(storer)
// Start the cacher.
cacher.start()
// Send a job -- this should succeed.
storer.EXPECT().IsCached("messageID").Return(false)
storer.EXPECT().BuildAndCacheMessage("messageID").Return(nil)
storer.EXPECT().BuildAndCacheMessage(cacher.ctx, "messageID").Return(nil)
cacher.newJob("messageID")
// Stop the cacher.