Skip to content

Commit

Permalink
Added ability switch to mocked broker outside of tests. Close #27. (#28)
Browse files Browse the repository at this point in the history
Added ability switch to mocked broker outside of tests. Close #27.
  • Loading branch information
cheshir authored Feb 15, 2019
1 parent 689de02 commit 3b5baaf
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Available options:
```yaml
dsn: "amqp://login:password@host:port/virtual_host"
reconnect_delay: 5s # Interval between connection tries. Check https://golang.org/pkg/time/#ParseDuration for details.
test_mode: false # Switches library to use mocked broker. Defaults to false.
exchanges:
- name: "exchange_name"
type: "direct"
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
type Config struct {
DSN string `mapstructure:"dsn" json:"dsn" yaml:"dsn"`
ReconnectDelay time.Duration `mapstructure:"reconnect_delay" json:"reconnect_delay" yaml:"reconnect_delay"`
TestMode bool `mapstructure:"test_mode" json:"test_mode" yaml:"test_mode"`
Exchanges Exchanges `mapstructure:"exchanges" json:"exchanges" yaml:"exchanges"`
Queues Queues `mapstructure:"queues" json:"queues" yaml:"queues"`
Producers Producers `mapstructure:"producers" json:"producers" yaml:"producers"`
Expand Down
13 changes: 0 additions & 13 deletions debug.go

This file was deleted.

5 changes: 1 addition & 4 deletions mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (mq *mq) connect() error {
}

func (mq *mq) createConnection() (conn conn, err error) {
if brokerIsMocked {
if brokerIsMocked || mq.config.TestMode {
return amqptest.Dial(mq.config.DSN)
}

Expand Down Expand Up @@ -402,13 +402,11 @@ func (mq *mq) initializeConsumersWorker(consumer *consumer, worker *worker) erro
// Reconnect stops current producers and consumers,
// recreates connection to the rabbit and than runs producers and consumers.
func (mq *mq) reconnect() {
Printer <- "call for reconnect"
startedReconnect := atomic.CompareAndSwapInt32(&mq.reconnectStatus, statusReadyForReconnect, statusReconnecting)
// There is no need to start a new reconnect if the previous one is not finished yet.
if !startedReconnect {
return
}
Printer <- "start reconnect"

defer func() {
atomic.StoreInt32(&mq.reconnectStatus, statusReadyForReconnect)
Expand All @@ -427,7 +425,6 @@ func (mq *mq) reconnect() {
if err := mq.setupAfterReconnect(); err != nil {
mq.internalErrorChannel <- err
}
Printer <- "successfully finish reconnect"
}

func (mq *mq) stopProducersAndConsumers() {
Expand Down

0 comments on commit 3b5baaf

Please sign in to comment.