Skip to content
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

Replace log to logger.Logger in Dispatcher.Dispatch #155

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion internal/channelmanager/channel_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewChannelManager(connManager *connectionmanager.ConnectionManager, log log
reconnectInterval: reconnectInterval,
reconnectionCount: 0,
reconnectionCountMux: &sync.Mutex{},
dispatcher: dispatcher.NewDispatcher(),
dispatcher: dispatcher.NewDispatcher(log),
}
go chanManager.startNotifyCancelOrClosed()
return &chanManager, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/connectionmanager/connection_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewConnectionManager(url string, conf amqp.Config, log logger.Logger, recon
ReconnectInterval: reconnectInterval,
reconnectionCount: 0,
reconnectionCountMux: &sync.Mutex{},
dispatcher: dispatcher.NewDispatcher(),
dispatcher: dispatcher.NewDispatcher(log),
}
go connManager.startNotifyClose()
return &connManager, nil
Expand Down
8 changes: 5 additions & 3 deletions internal/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dispatcher

import (
"log"
"github.com/wagslane/go-rabbitmq/internal/logger"
"math"
"math/rand"
"sync"
Expand All @@ -12,6 +12,7 @@ import (
type Dispatcher struct {
subscribers map[int]dispatchSubscriber
subscribersMux *sync.Mutex
logger logger.Logger
}

type dispatchSubscriber struct {
Expand All @@ -20,10 +21,11 @@ type dispatchSubscriber struct {
}

// NewDispatcher -
func NewDispatcher() *Dispatcher {
func NewDispatcher(logger logger.Logger) *Dispatcher {
return &Dispatcher{
subscribers: make(map[int]dispatchSubscriber),
subscribersMux: &sync.Mutex{},
logger: logger,
}
}

Expand All @@ -34,7 +36,7 @@ func (d *Dispatcher) Dispatch(err error) error {
for _, subscriber := range d.subscribers {
select {
case <-time.After(time.Second * 5):
log.Println("Unexpected rabbitmq error: timeout in dispatch")
d.logger.Errorf("Unexpected rabbitmq error: timeout in dispatch")
case subscriber.notifyCancelOrCloseChan <- err:
}
}
Expand Down
14 changes: 11 additions & 3 deletions internal/dispatcher/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import (
"time"
)

type lgr struct{}

func (l *lgr) Fatalf(string, ...interface{}) {}
func (l *lgr) Errorf(string, ...interface{}) {}
func (l *lgr) Warnf(string, ...interface{}) {}
func (l *lgr) Infof(string, ...interface{}) {}
func (l *lgr) Debugf(string, ...interface{}) {}

func TestNewDispatcher(t *testing.T) {
d := NewDispatcher()
d := NewDispatcher(&lgr{})
if d.subscribers == nil {
t.Error("Dispatcher subscribers is nil")
}
Expand All @@ -16,15 +24,15 @@ func TestNewDispatcher(t *testing.T) {
}

func TestAddSubscriber(t *testing.T) {
d := NewDispatcher()
d := NewDispatcher(&lgr{})
d.AddSubscriber()
if len(d.subscribers) != 1 {
t.Error("Dispatcher subscribers length is not 1")
}
}

func TestCloseSubscriber(t *testing.T) {
d := NewDispatcher()
d := NewDispatcher(&lgr{})
_, closeCh := d.AddSubscriber()
close(closeCh)
time.Sleep(time.Millisecond)
Expand Down
Loading