65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
|
package mq
|
|||
|
|
|||
|
import (
|
|||
|
"context"
|
|||
|
"fmt"
|
|||
|
"sync/atomic"
|
|||
|
"testing"
|
|||
|
"time"
|
|||
|
)
|
|||
|
|
|||
|
func TestConsumer_Start(t *testing.T) {
|
|||
|
//initTracer("http://192.168.6.194:14268/api/traces", 1, "go_unit_test_for_util_consumer")
|
|||
|
|
|||
|
manager := NewConsumerManager(nil)
|
|||
|
ctx := context.Background()
|
|||
|
consumerConf := &ConsumerConfig{
|
|||
|
TopicName: "test_transfer_rs",
|
|||
|
GroupName: "test_transfer_rs_consumer",
|
|||
|
PerCoroutineCnt: 2,
|
|||
|
}
|
|||
|
connConf := &ConsumerConnConfig{
|
|||
|
NameServers: "http://rmq-cn-j4g3sem5i05.cn-chengdu.rmq.aliyuncs.com:8080",
|
|||
|
AccessKey: "1i3vEsceLzcYD26p",
|
|||
|
SecretKey: "192602NYDuAHQ76a",
|
|||
|
}
|
|||
|
i := atomic.Int32{}
|
|||
|
|
|||
|
err := manager.Subscribe(ctx, connConf, consumerConf, func(ctx context.Context, message *ConsumerMessage) error {
|
|||
|
cnt := i.Add(1)
|
|||
|
|
|||
|
fmt.Printf("%d开始消费:%s ,body=%s \n", cnt, message.MsgId, message.Body)
|
|||
|
fmt.Println(message.Properties)
|
|||
|
// mock 业务耗时
|
|||
|
time.Sleep(1 * time.Second)
|
|||
|
|
|||
|
fmt.Printf("%d已完成:%s ,body=%s \n", cnt, message.MsgId, message.Body)
|
|||
|
return nil
|
|||
|
})
|
|||
|
fmt.Println(err)
|
|||
|
consumerConf = &ConsumerConfig{
|
|||
|
TopicName: "test_transfer_rs",
|
|||
|
GroupName: "test_transfer_rs_consumer",
|
|||
|
PerCoroutineCnt: 2,
|
|||
|
}
|
|||
|
err = manager.Subscribe(ctx, connConf, consumerConf, func(ctx context.Context, message *ConsumerMessage) error {
|
|||
|
cnt := i.Add(1)
|
|||
|
|
|||
|
fmt.Printf("%d开始消费:%s ,body=%s \n", cnt, message.MsgId, message.Body)
|
|||
|
fmt.Println(message.Properties)
|
|||
|
// mock 业务耗时
|
|||
|
time.Sleep(1 * time.Second)
|
|||
|
|
|||
|
fmt.Printf("%d已完成:%s ,body=%s \n", cnt, message.MsgId, message.Body)
|
|||
|
return nil
|
|||
|
})
|
|||
|
|
|||
|
fmt.Println(err)
|
|||
|
_ = manager.Start(context.Background())
|
|||
|
|
|||
|
time.Sleep(90 * time.Second)
|
|||
|
|
|||
|
// mock 退出
|
|||
|
_ = manager.Stop(context.Background())
|
|||
|
}
|