github.com/godaddy-x/freego@v1.0.156/rabbit_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/godaddy-x/freego/amqp"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  var exchange = "test.exchange"
    11  var queue = "test.monitor"
    12  var input = rabbitmq.AmqpConfig{
    13  	Username: "admin",
    14  	Password: "123456",
    15  	Host:     "172.31.25.1",
    16  	Port:     5672,
    17  }
    18  
    19  // 单元测试
    20  func TestMQPull(t *testing.T) {
    21  	mq, err := rabbitmq.NewPull()
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  	cli, _ := mq.Client()
    26  	receiver := &rabbitmq.PullReceiver{
    27  		Config: &rabbitmq.Config{Option: rabbitmq.Option{
    28  			Exchange: exchange,
    29  			Queue:    queue,
    30  		}},
    31  		Callback: func(msg *rabbitmq.MsgData) error {
    32  			fmt.Println("receive msg: ", msg)
    33  			return nil
    34  		},
    35  	}
    36  	cli.AddPullReceiver(receiver)
    37  	time.Sleep(10000 * time.Second)
    38  }
    39  
    40  func TestMQPublish(t *testing.T) {
    41  	mq, err := rabbitmq.NewPublish()
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  	cli, _ := mq.Client()
    46  	content := map[string]interface{}{"test": 1234}
    47  	for {
    48  		err := cli.Publish(exchange, queue, 1, content)
    49  		if err != nil {
    50  			fmt.Println("send msg failed: ", err)
    51  		} else {
    52  			fmt.Println("send msg success: ", content)
    53  		}
    54  		time.Sleep(5 * time.Second)
    55  	}
    56  
    57  	time.Sleep(10000 * time.Second)
    58  }