Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ filename: "{{.InterfaceName | snakecase}}_mock.go"
outpkg: "mocks"
packages:
geeksonator/internal/observer:
interfaces:
BotProvider:
Cache:
geeksonator/internal/provider/telegram:
interfaces:
BotAPI:
6 changes: 3 additions & 3 deletions internal/app/geeksonator/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
cacheMaxSize = 1
cacheMaxSize = 100
cacheTTL = 24 * time.Hour
)

Expand Down Expand Up @@ -66,10 +66,10 @@ func Start() error {

updatesChan := botAPI.GetUpdatesChan(updateConfig)

cache, err := cacher.NewCacher[string, []tgbotapi.ChatMember](
cache, err := cacher.NewCacher[int64, []tgbotapi.ChatMember](
cacheMaxSize,
cacheTTL,
cacher.WithDebug[string, []tgbotapi.ChatMember](logger),
cacher.WithDebug[int64, []tgbotapi.ChatMember](logger),
)
if err != nil {
return fmt.Errorf("cacher.NewCacher: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/observer/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type BotProvider interface {
// Cache interface for cache.
type Cache interface {
// Get looks up a key's value from the cache.
Get(key string) (value []tgbotapi.ChatMember, ok bool)
Get(key int64) (value []tgbotapi.ChatMember, ok bool)

// Set adds a value to the cache.
Set(key string, value []tgbotapi.ChatMember) error
Set(key int64, value []tgbotapi.ChatMember) error
}
8 changes: 2 additions & 6 deletions internal/observer/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"go.uber.org/zap/zapcore"
)

const (
cacheKey = "admins"
)

// Manager is manager for observer.
type Manager struct {
bot BotProvider
Expand Down Expand Up @@ -169,7 +165,7 @@ func (m *Manager) log(msg string, fields ...zapcore.Field) {

// getAdmins returns admins.
func (m *Manager) getAdmins(chatCfg tgbotapi.ChatConfig) ([]tgbotapi.ChatMember, error) {
adminsFromCache, ok := m.cache.Get(cacheKey)
adminsFromCache, ok := m.cache.Get(chatCfg.ChatID)
if ok {
return adminsFromCache, nil
}
Expand All @@ -179,7 +175,7 @@ func (m *Manager) getAdmins(chatCfg tgbotapi.ChatConfig) ([]tgbotapi.ChatMember,
return nil, fmt.Errorf("m.bot.GetChatAdministrators: %v", err)
}

if err := m.cache.Set(cacheKey, admins); err != nil {
if err := m.cache.Set(chatCfg.ChatID, admins); err != nil {
m.log("Set admins in cache",
zap.Error(err),
)
Expand Down
38 changes: 25 additions & 13 deletions internal/observer/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestManager_processingUpdate(t *testing.T) {
cache := mocks.NewCacheMock(t)

cache.EXPECT().
Get(cacheKey).
Get(int64(300600)).
Return(
[]tgbotapi.ChatMember{
{
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestManager_processingMessage(t *testing.T) {
cache := mocks.NewCacheMock(t)

cache.EXPECT().
Get(cacheKey).
Get(int64(300600)).
Return([]tgbotapi.ChatMember{
{
User: &tgbotapi.User{
Expand All @@ -277,7 +277,9 @@ func TestManager_processingMessage(t *testing.T) {
},
args: args{
message: &tgbotapi.Message{
Chat: &tgbotapi.Chat{},
Chat: &tgbotapi.Chat{
ID: 300600,
},
From: &tgbotapi.User{
ID: 100500,
},
Expand All @@ -293,7 +295,7 @@ func TestManager_processingMessage(t *testing.T) {
cache := mocks.NewCacheMock(t)

cache.EXPECT().
Get(cacheKey).
Get(int64(300600)).
Return(
[]tgbotapi.ChatMember{
{
Expand All @@ -311,7 +313,9 @@ func TestManager_processingMessage(t *testing.T) {
},
args: args{
message: &tgbotapi.Message{
Chat: &tgbotapi.Chat{},
Chat: &tgbotapi.Chat{
ID: 300600,
},
From: &tgbotapi.User{
ID: 100500,
},
Expand All @@ -327,7 +331,7 @@ func TestManager_processingMessage(t *testing.T) {
cache := mocks.NewCacheMock(t)

cache.EXPECT().
Get(cacheKey).
Get(int64(300600)).
Return(
[]tgbotapi.ChatMember{
{
Expand All @@ -345,7 +349,9 @@ func TestManager_processingMessage(t *testing.T) {
},
args: args{
message: &tgbotapi.Message{
Chat: &tgbotapi.Chat{},
Chat: &tgbotapi.Chat{
ID: 300600,
},
From: &tgbotapi.User{
ID: 100501,
},
Expand Down Expand Up @@ -554,7 +560,7 @@ func TestManager_getAdmins(t *testing.T) {
cache := mocks.NewCacheMock(t)

cache.EXPECT().
Get(cacheKey).
Get(int64(300600)).
Return([]tgbotapi.ChatMember{
{
User: &tgbotapi.User{
Expand All @@ -568,7 +574,9 @@ func TestManager_getAdmins(t *testing.T) {
}
},
args: args{
chatCfg: tgbotapi.ChatConfig{},
chatCfg: tgbotapi.ChatConfig{
ChatID: 300600,
},
},
wantResult: []tgbotapi.ChatMember{
{
Expand All @@ -585,7 +593,9 @@ func TestManager_getAdmins(t *testing.T) {
botProvider := mocks.NewBotProviderMock(t)

botProvider.EXPECT().
GetChatAdministrators(tgbotapi.ChatConfig{}).
GetChatAdministrators(tgbotapi.ChatConfig{
ChatID: 300600,
}).
Return(
[]tgbotapi.ChatMember{
{
Expand All @@ -600,11 +610,11 @@ func TestManager_getAdmins(t *testing.T) {
cache := mocks.NewCacheMock(t)

cache.EXPECT().
Get(cacheKey).
Get(int64(300600)).
Return(nil, false)

cache.EXPECT().
Set(cacheKey, []tgbotapi.ChatMember{
Set(int64(300600), []tgbotapi.ChatMember{
{
User: &tgbotapi.User{
ID: 100500,
Expand All @@ -619,7 +629,9 @@ func TestManager_getAdmins(t *testing.T) {
}
},
args: args{
chatCfg: tgbotapi.ChatConfig{},
chatCfg: tgbotapi.ChatConfig{
ChatID: 300600,
},
},
wantResult: []tgbotapi.ChatMember{
{
Expand Down
28 changes: 14 additions & 14 deletions internal/observer/mocks/cache_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.