Skip to content

Commit

Permalink
change handlers map to slice
Browse files Browse the repository at this point in the history
  • Loading branch information
negasus committed Sep 16, 2024
1 parent d82ce2e commit fd88801
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 76 deletions.
26 changes: 11 additions & 15 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ type Bot struct {
errorsHandler ErrorsHandler
debugHandler DebugHandler

middlewaresMx *sync.RWMutex
middlewaresMx sync.RWMutex

Check failure on line 47 in bot.go

View workflow job for this annotation

GitHub Actions / test

field `middlewaresMx` is unused (unused)
middlewares []Middleware

handlersMx *sync.RWMutex
handlers map[string]handler
handlersMx sync.RWMutex
handlers []handler

client HttpClient
lastUpdateID int64
Expand All @@ -67,13 +67,9 @@ func New(token string, options ...Option) (*Bot, error) {
}

b := &Bot{
url: "https://api.telegram.org",
token: token,
pollTimeout: defaultPollTimeout,
middlewaresMx: &sync.RWMutex{},
middlewares: []Middleware{},
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
url: "https://api.telegram.org",
token: token,
pollTimeout: defaultPollTimeout,
client: &http.Client{
Timeout: defaultPollTimeout,
},
Expand Down Expand Up @@ -105,26 +101,26 @@ func New(token string, options ...Option) (*Bot, error) {

// StartWebhook starts the Bot with webhook mode
func (b *Bot) StartWebhook(ctx context.Context) {
wg := &sync.WaitGroup{}
wg := sync.WaitGroup{}

wg.Add(b.workers)
for i := 0; i < b.workers; i++ {
go b.waitUpdates(ctx, wg)
go b.waitUpdates(ctx, &wg)
}

wg.Wait()
}

// Start the bot
func (b *Bot) Start(ctx context.Context) {
wg := &sync.WaitGroup{}
wg := sync.WaitGroup{}

wg.Add(1)
go b.getUpdates(ctx, wg)
go b.getUpdates(ctx, &wg)

wg.Add(b.workers)
for i := 0; i < b.workers; i++ {
go b.waitUpdates(ctx, wg)
go b.waitUpdates(ctx, &wg)
}

wg.Wait()
Expand Down
17 changes: 13 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
)

type handler struct {
id string
handlerType HandlerType
matchType MatchType
handler HandlerFunc
Expand Down Expand Up @@ -80,12 +81,13 @@ func (b *Bot) RegisterHandlerMatchFunc(matchFunc MatchFunc, f HandlerFunc, m ...
id := RandomString(16)

h := handler{
id: id,
matchType: matchTypeFunc,
matchFunc: matchFunc,
handler: applyMiddlewares(f, m...),
}

b.handlers[id] = h
b.handlers = append(b.handlers, h)

return id
}
Expand All @@ -97,13 +99,14 @@ func (b *Bot) RegisterHandlerRegexp(handlerType HandlerType, re *regexp.Regexp,
id := RandomString(16)

h := handler{
id: id,
handlerType: handlerType,
matchType: matchTypeRegexp,
re: re,
handler: applyMiddlewares(f, m...),
}

b.handlers[id] = h
b.handlers = append(b.handlers, h)

return id
}
Expand All @@ -115,13 +118,14 @@ func (b *Bot) RegisterHandler(handlerType HandlerType, pattern string, matchType
id := RandomString(16)

h := handler{
id: id,
handlerType: handlerType,
matchType: matchType,
pattern: pattern,
handler: applyMiddlewares(f, m...),
}

b.handlers[id] = h
b.handlers = append(b.handlers, h)

return id
}
Expand All @@ -130,5 +134,10 @@ func (b *Bot) UnregisterHandler(id string) {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()

delete(b.handlers, id)
for i, h := range b.handlers {
if h.id == id {
b.handlers = append(b.handlers[:i], b.handlers[i+1:]...)
return
}
}
}
76 changes: 32 additions & 44 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@ package bot

import (
"regexp"
"sync"
"testing"

"github.com/go-telegram/bot/models"
)

func Test_match_func(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
func findHandler(b *Bot, id string) *handler {
b.handlersMx.RLock()
defer b.handlersMx.RUnlock()

for _, h := range b.handlers {
if h.id == id {
return &h
}
}

return nil
}

func Test_match_func(t *testing.T) {
b := &Bot{}

var called bool

id := b.RegisterHandlerMatchFunc(func(update *models.Update) bool {
Expand All @@ -24,7 +33,7 @@ func Test_match_func(t *testing.T) {
return true
}, nil)

h := b.handlers[id]
h := findHandler(b, id)

res := h.match(&models.Update{ID: 42})
if !called {
Expand All @@ -36,14 +45,11 @@ func Test_match_func(t *testing.T) {
}

func Test_match_exact(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
b := &Bot{}

id := b.RegisterHandler(HandlerTypeMessageText, "xxx", MatchTypeExact, nil)

h := b.handlers[id]
h := findHandler(b, id)

res := h.match(&models.Update{Message: &models.Message{Text: "zzz"}})
if res {
Expand All @@ -57,14 +63,11 @@ func Test_match_exact(t *testing.T) {
}

func Test_match_prefix(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
b := &Bot{}

id := b.RegisterHandler(HandlerTypeCallbackQueryData, "abc", MatchTypePrefix, nil)

h := b.handlers[id]
h := findHandler(b, id)

res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xabcdef"}})
if res {
Expand All @@ -78,14 +81,11 @@ func Test_match_prefix(t *testing.T) {
}

func Test_match_contains(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
b := &Bot{}

id := b.RegisterHandler(HandlerTypeCallbackQueryData, "abc", MatchTypeContains, nil)

h := b.handlers[id]
h := findHandler(b, id)

res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xxabxx"}})
if res {
Expand All @@ -99,16 +99,13 @@ func Test_match_contains(t *testing.T) {
}

func Test_match_regexp(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
b := &Bot{}

re := regexp.MustCompile("^[a-z]+")

id := b.RegisterHandlerRegexp(HandlerTypeCallbackQueryData, re, nil)

h := b.handlers[id]
h := findHandler(b, id)

res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}})
if res {
Expand All @@ -122,14 +119,11 @@ func Test_match_regexp(t *testing.T) {
}

func Test_match_invalid_type(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
b := &Bot{}

id := b.RegisterHandler(-1, "", -1, nil)

h := b.handlers[id]
h := findHandler(b, id)

res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}})
if res {
Expand All @@ -138,45 +132,39 @@ func Test_match_invalid_type(t *testing.T) {
}

func TestBot_RegisterUnregisterHandler(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
b := &Bot{}

id1 := b.RegisterHandler(HandlerTypeCallbackQueryData, "", MatchTypeExact, nil)
id2 := b.RegisterHandler(HandlerTypeCallbackQueryData, "", MatchTypeExact, nil)

if len(b.handlers) != 2 {
t.Fatalf("unexpected handlers len")
}
if _, ok := b.handlers[id1]; !ok {
if h := findHandler(b, id1); h == nil {
t.Fatalf("handler not found")
}
if _, ok := b.handlers[id2]; !ok {
if h := findHandler(b, id2); h == nil {
t.Fatalf("handler not found")
}

b.UnregisterHandler(id1)
if len(b.handlers) != 1 {
t.Fatalf("unexpected handlers len")
}
if _, ok := b.handlers[id1]; ok {
if h := findHandler(b, id1); h != nil {
t.Fatalf("handler found")
}
if _, ok := b.handlers[id2]; !ok {
if h := findHandler(b, id2); h == nil {
t.Fatalf("handler not found")
}
}

func Test_match_exact_game(t *testing.T) {
b := &Bot{
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}
b := &Bot{}

id := b.RegisterHandler(HandlerTypeCallbackQueryGameShortName, "xxx", MatchTypeExact, nil)

h := b.handlers[id]
h := findHandler(b, id)
u := models.Update{
ID: 42,
CallbackQuery: &models.CallbackQuery{
Expand Down
16 changes: 3 additions & 13 deletions process_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bot

import (
"context"
"sync"
"testing"

"github.com/go-telegram/bot/models"
Expand Down Expand Up @@ -38,8 +37,6 @@ func TestProcessUpdate(t *testing.T) {
bot := &Bot{
defaultHandlerFunc: h,
middlewares: []Middleware{},
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}

ctx := context.Background()
Expand All @@ -66,8 +63,6 @@ func TestProcessUpdate_WithMiddlewares(t *testing.T) {
bot := &Bot{
defaultHandlerFunc: h,
middlewares: []Middleware{middleware},
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}

ctx := context.Background()
Expand All @@ -93,8 +88,6 @@ func TestProcessUpdate_WithMatchTypeFunc(t *testing.T) {

bot := &Bot{
defaultHandlerFunc: h1,
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}

bot.RegisterHandlerMatchFunc(m, h2)
Expand All @@ -116,17 +109,16 @@ func Test_findHandler(t *testing.T) {

bot := &Bot{
defaultHandlerFunc: h,
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}

// Register a handler
bot.handlers["test"] = handler{
bot.handlers = append(bot.handlers, handler{
id: "test",
handlerType: HandlerTypeMessageText,
matchType: MatchTypeExact,
pattern: "test",
handler: h,
}
})

ctx := context.Background()
upd := &models.Update{Message: &models.Message{Text: "test"}}
Expand All @@ -147,8 +139,6 @@ func Test_findHandler_Default(t *testing.T) {

bot := &Bot{
defaultHandlerFunc: h,
handlersMx: &sync.RWMutex{},
handlers: map[string]handler{},
}

ctx := context.Background()
Expand Down

0 comments on commit fd88801

Please sign in to comment.