github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/data/queue/producer/nsq_test.go (about)

     1  package producer
     2  
     3  import (
     4  	"github.com/angenalZZZ/gofunc/data/queue/nsq"
     5  	. "github.com/smartystreets/goconvey/convey"
     6  	"testing"
     7  )
     8  
     9  // nsq:publish:messages(max:60/second)
    10  // to_nsq -nsqd-tcp-address 127.0.0.1:4150 -race 60 -topic TestTopic
    11  
    12  // nsq:stats
    13  // nsq_stat -nsqd-http-address 127.0.0.1:4151 -topic TestTopic -channel TestChannel
    14  
    15  func TestNSQPublish(t *testing.T) {
    16  	Convey("Given a json message to publish", t, func() {
    17  		Convey("It should not produce any error", func() {
    18  			p, err := NewNsqProducer(nsq.NSQdTCPAddr)
    19  			So(err, ShouldEqual, nil)
    20  			var messageBody = []byte{0x18}
    21  			err = p.Publish(nsq.TestTopic, messageBody)
    22  			So(err, ShouldEqual, nil)
    23  		})
    24  	})
    25  }
    26  
    27  func TestNSQPublishAsync(t *testing.T) {
    28  	Convey("Given a json message to publish asynchronously", t, func() {
    29  		Convey("It should not produce any error", func() {
    30  			p, err := NewNsqProducer(nsq.NSQdTCPAddr)
    31  			So(err, ShouldEqual, nil)
    32  			var messageBody = []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
    33  			err = p.PublishAsync(nsq.TestTopic, messageBody, nil)
    34  			So(err, ShouldEqual, nil)
    35  		})
    36  	})
    37  }
    38  
    39  func TestNSQMultiPublish(t *testing.T) {
    40  	Convey("Given a multiple message to publish", t, func() {
    41  		Convey("It should not produce any error", func() {
    42  			p, err := NewNsqProducer(nsq.NSQdTCPAddr)
    43  			So(err, ShouldEqual, nil)
    44  			var message1 = []byte{0x18}
    45  			var messageBody = [][]byte{message1}
    46  			err = p.MultiPublish(nsq.TestTopic, messageBody)
    47  			So(err, ShouldEqual, nil)
    48  		})
    49  	})
    50  }
    51  
    52  func TestNSQMultiPublishAsync(t *testing.T) {
    53  	Convey("Given a multiple message to publish asynchrnously", t, func() {
    54  		Convey("It should not produce any error", func() {
    55  			p, err := NewNsqProducer(nsq.NSQdTCPAddr)
    56  			So(err, ShouldEqual, nil)
    57  			var message1 = []byte{0x18}
    58  			var messageBody = [][]byte{message1}
    59  			err = p.MultiPublishAsync(nsq.TestTopic, messageBody, nil)
    60  			So(err, ShouldEqual, nil)
    61  		})
    62  	})
    63  }
    64  
    65  func TestNSQPublishJSONAsync(t *testing.T) {
    66  	Convey("Given a topic and a message to publish asynchronously", t, func() {
    67  		Convey("It should not produce any error", func() {
    68  			p, err := NewNsqProducer(nsq.NSQdTCPAddr)
    69  			So(err, ShouldEqual, nil)
    70  			var message interface{} = "testMessage"
    71  			err = p.PublishJSONAsync(nsq.TestTopic, message, nil)
    72  			So(err, ShouldEqual, nil)
    73  		})
    74  	})
    75  }
    76  
    77  func TestNSQPublishJSON(t *testing.T) {
    78  	Convey("Given topic to publish a json message", t, func() {
    79  		Convey("It should not produce any error", func() {
    80  			p, err := NewNsqProducer(nsq.NSQdTCPAddr)
    81  			So(err, ShouldEqual, nil)
    82  			var message interface{} = "testMessage"
    83  			err = p.PublishJSON(nsq.TestTopic, message)
    84  			So(err, ShouldEqual, nil)
    85  		})
    86  	})
    87  }
    88  
    89  func TestNSQConnect(t *testing.T) {
    90  	Convey("Given nsqd address to connect to", t, func() {
    91  		Convey("It should not produce any error", func() {
    92  			_, err := NewNsqProducer(nsq.NSQdTCPAddr)
    93  			So(err, ShouldEqual, nil)
    94  		})
    95  	})
    96  }