go-pattern-examples/gomore/01_messages/message_test.go

112 lines
2.2 KiB
Go
Raw Normal View History

2020-04-30 09:50:21 +03:00
package messaging
2020-04-28 17:48:25 +03:00
2020-04-29 12:27:46 +03:00
import (
2020-04-30 09:50:21 +03:00
"context"
2020-04-29 12:27:46 +03:00
"fmt"
2020-04-30 09:50:21 +03:00
"sync"
2020-04-29 12:27:46 +03:00
"testing"
"time"
)
2020-04-28 17:48:25 +03:00
2020-04-29 12:27:46 +03:00
////////////////////////////////
//通常意义上是,连接消息队列之后就可以发送消息
2020-04-29 17:44:12 +03:00
//当订阅著之后才会收到相关Topic消息的推送
2020-05-08 15:33:06 +03:00
//当前,省略连接队列的步骤和操作代码
2020-04-29 12:27:46 +03:00
////////////////////////////////
2020-04-28 17:48:25 +03:00
2020-04-30 09:50:21 +03:00
func TestMessageSubAndPubWithTopic(t *testing.T) {
var wg sync.WaitGroup
2020-04-28 17:48:25 +03:00
2020-04-30 09:50:21 +03:00
topicName := "seeking passengers"
2020-05-08 15:33:06 +03:00
//假设消息队列已经收到数据加下来由topic处理
2020-04-30 09:50:21 +03:00
topic := Topic{
Name: topicName,
UserQueueSize: 5,
}
ctx, cancel := context.WithCancel(context.Background())
2020-04-29 12:27:46 +03:00
2020-04-30 09:50:21 +03:00
wg.Add(1)
2020-04-29 12:27:46 +03:00
2020-05-08 15:33:06 +03:00
//user 1
2020-04-30 09:50:21 +03:00
//用户tom订阅拼车消息,订阅的是车主发布的拼车消息
if subScriberTom, ok := topic.Subscribe(123, topicName); ok {
2020-04-29 12:27:46 +03:00
go func() {
2020-04-30 09:50:21 +03:00
defer wg.Done()
2020-04-29 12:33:31 +03:00
EXIT:
2020-04-29 12:27:46 +03:00
for {
select {
2020-04-30 09:50:21 +03:00
case <-ctx.Done():
fmt.Println("tom receive cancel, exit")
2020-04-29 12:27:46 +03:00
break EXIT
default:
msg := Message{}
2020-04-30 09:50:21 +03:00
err := subScriberTom.Receive(&msg)
if err == nil {
fmt.Println("tom receive subscribed msg:", msg)
}
2020-04-29 12:27:46 +03:00
}
time.Sleep(200)
}
}()
}
2020-04-30 09:50:21 +03:00
wg.Add(1)
2020-05-08 15:33:06 +03:00
2020-04-30 09:50:21 +03:00
//订阅成功了
//发送一个消息
2020-04-29 12:27:46 +03:00
2020-04-30 09:50:21 +03:00
//用户Lily订阅拼车消息,订阅的是车主发布的拼车消息
if subSCriptionLily, ok := topic.Subscribe(456, topicName); ok {
2020-04-29 12:27:46 +03:00
go func() {
2020-04-30 09:50:21 +03:00
defer wg.Done()
2020-04-29 12:33:31 +03:00
EXIT:
2020-04-29 12:27:46 +03:00
for {
select {
2020-04-30 09:50:21 +03:00
case <-ctx.Done():
fmt.Println("lily receive cancel, exit")
2020-04-29 12:27:46 +03:00
break EXIT
default:
msg := Message{}
2020-04-30 09:50:21 +03:00
err := subSCriptionLily.Receive(&msg)
if err == nil {
fmt.Println("lily receive subscribed msg:", msg)
}
2020-04-29 12:27:46 +03:00
}
time.Sleep(200)
}
}()
2020-04-30 09:50:21 +03:00
}
2020-04-29 12:27:46 +03:00
2020-04-30 09:50:21 +03:00
go func() {
//模拟发送消息
2020-04-29 12:27:46 +03:00
msg := Message{
2020-04-30 09:50:21 +03:00
Text: "i am looking for 1 passenger",
From: Session{User{123, "lily"}, time.Now()},
}
topic.Publish(msg)
msg = Message{
Text: "i am looking for 2 passenger",
From: Session{User{123, "lucy"}, time.Now()},
2020-04-29 12:27:46 +03:00
}
2020-04-30 09:50:21 +03:00
topic.Publish(msg)
2020-04-29 12:27:46 +03:00
2020-04-30 09:50:21 +03:00
msg = Message{
Text: "i am looking for passenger as many as i can",
From: Session{User{123, "rose"}, time.Now()},
}
2020-04-29 12:27:46 +03:00
2020-04-30 09:50:21 +03:00
topic.Publish(msg)
time.Sleep(time.Second)
cancel()
2020-04-29 12:27:46 +03:00
2020-04-30 09:50:21 +03:00
}()
wg.Wait()
fmt.Println("all message done,exit it")
2020-04-28 17:48:25 +03:00
}