feat: implemented test poller
This commit is contained in:
1 parent
6af79d8cda
commit
a78e9927bc
3 files changed
+29
-7
No files matched your search
+25
-3
@@ -4,12 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
maxbot "github.com/max-messenger/max-bot-api-client-go/v2"
|
maxbot "github.com/max-messenger/max-bot-api-client-go/v2"
|
||||||
"github.com/max-messenger/max-bot-api-client-go/v2/model"
|
"github.com/max-messenger/max-bot-api-client-go/v2/model"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,7 +36,10 @@ type LongPoller struct {
|
|||||||
pollInterval time.Duration
|
pollInterval time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestPoller struct{}
|
type TestPoller struct {
|
||||||
|
ch chan model.Update
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
type webhookOptions struct {
|
type webhookOptions struct {
|
||||||
addr string
|
addr string
|
||||||
@@ -185,8 +188,27 @@ func (lp *LongPoller) Start(ctx context.Context) (<-chan model.Update, error) {
|
|||||||
return ch, nil
|
return ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tp *TestPoller) Inject(ctx context.Context, updates []model.Update) {
|
||||||
|
ch := make(chan model.Update, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(ch)
|
||||||
|
|
||||||
|
for _, upd := range updates {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case ch <- upd:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
tp.mu.Lock()
|
||||||
|
tp.ch = ch
|
||||||
|
defer tp.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (tp *TestPoller) Start(_ context.Context) (<-chan model.Update, error) {
|
func (tp *TestPoller) Start(_ context.Context) (<-chan model.Update, error) {
|
||||||
return nil, fmt.Errorf("unimplemented")
|
return tp.ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithAddress(addr string) WebhookOption {
|
func WithAddress(addr string) WebhookOption {
|
||||||
|
|||||||
+3
-3
@@ -1,16 +1,16 @@
|
|||||||
package errors
|
package errors
|
||||||
|
|
||||||
type GomaxError struct {
|
type GoMaxError struct {
|
||||||
Message string
|
Message string
|
||||||
Err error
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ge *GomaxError) Error() string {
|
func (ge *GoMaxError) Error() string {
|
||||||
return ge.Err.Error()
|
return ge.Err.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(parent error, text string) error {
|
func New(parent error, text string) error {
|
||||||
return &GomaxError{
|
return &GoMaxError{
|
||||||
Message: text,
|
Message: text,
|
||||||
Err: parent,
|
Err: parent,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func MwErrorResponder(defaultMsg string) core.Middleware {
|
|||||||
return func(next core.HandlerFunc) core.HandlerFunc {
|
return func(next core.HandlerFunc) core.HandlerFunc {
|
||||||
return func(c *mcontext.Context) error {
|
return func(c *mcontext.Context) error {
|
||||||
err := next(c)
|
err := next(c)
|
||||||
var maxErr *merrors.GomaxError
|
var maxErr *merrors.GoMaxError
|
||||||
|
|
||||||
if errors.As(err, &maxErr) {
|
if errors.As(err, &maxErr) {
|
||||||
return c.Respond(maxErr.Message)
|
return c.Respond(maxErr.Message)
|
||||||
|
|||||||
Reference in new issue
Block a user