Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
NSQ Producer testing abstraction
package nsqutils
import (
// Producer is an interface that nsq.Producer fulfills
type Producer interface {
Publish(string, []byte) error
// TestProducer implements Producer and just counts the number of messages
// optionally if it includes a nsq.Producer it will also send messages
type TestProducer struct {
Counters map[string]int32
Producer *nsq.Producer
// Reset the counters
func (p *TestProducer) Reset() {
p.Counters = nil
// Count the total number of events
func (p *TestProducer) Count() int32 {
defer p.Unlock()
var i int32
for _, c := range p.Counters {
i += c
return i
// Publish tracks publishing to a topic
func (p *TestProducer) Publish(topic string, body []byte) error {
defer p.Unlock()
if p.Counters == nil {
p.Counters = make(map[string]int32)
if p.Producer != nil {
return p.Producer.Publish(topic, body)
return nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.