github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/mq/nsq/nsq_test.go (about)

     1  package nsq
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/sereiner/library/mq"
     9  )
    10  
    11  func Test_NewNsqProducer(t *testing.T) {
    12  	p, err := NewNsqProducer("127.0.0.1:4150")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	p.Connect()
    17  	for {
    18  		time.Sleep(time.Microsecond * 500)
    19  		p.Send("hello", `{"name":"jack","age":22}`, time.Second)
    20  	}
    21  
    22  }
    23  
    24  func Test_NewNsqConsumer(t *testing.T) {
    25  	c, err := NewNsqConsumer("127.0.0.1:4150")
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	err = c.Consume("hello#ch", 1, M)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	time.Sleep(time.Second * 1)
    34  	c.Close()
    35  }
    36  
    37  func M(message mq.IMessage) {
    38  	fmt.Println(message.GetMessage())
    39  }