github.com/zooyer/miskit@v1.0.71/smq/rabbit_test.go (about)

     1  package smq
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestNewRabbitMQ(t *testing.T) {
    10  	var err error
    11  	var exchange = Exchange{
    12  		Type:  "direct",
    13  		Name:  "smq.name",
    14  		Key:   "smq.key",
    15  		Queue: "smq.queue",
    16  	}
    17  	client := NewRabbitMQ("amqp://user:123456@127.0.0.1:5672", exchange)
    18  	var ch = make(chan struct{})
    19  	if err = client.Connect(); err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer client.Close()
    23  
    24  	go func() {
    25  		time.Sleep(time.Second * 5)
    26  		if err = client.Sub("", func(client Client, topic string, message []byte, options Options) (err error) {
    27  			fmt.Println("message:", string(message))
    28  			close(ch)
    29  			return
    30  		}); err != nil {
    31  			t.Fatal(err)
    32  		}
    33  	}()
    34  
    35  	if err = client.Pub("", []byte("hello,world"), TTL(6*time.Second)); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	<-ch
    40  }