You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
838 B

package pubsub
import (
"fmt"
"strings"
"testing"
"time"
)
func Test_NewPublisher(t *testing.T) {
//初始化一个发布者对象
publisher := NewPublisher(3, 5*time.Second)
//创建一个订阅所有主题的订阅者
all := publisher.SubscriberAllTopic()
//创建一个订阅golang主题的订阅者
golang := publisher.SubscriberTopic(func(v interface{}) bool {
if s, ok := v.(string); ok {
return strings.Contains(s, "golang")
}
return false
})
//发布2条主题
publisher.Publish("hello world")
publisher.Publish("hello golang")
go func() {
for i := range all {
fmt.Println("all:", i)
}
}()
go func() {
for i := range golang {
fmt.Println("golang:", i)
}
}()
time.Sleep(5 * time.Second)
publisher.Close()
fmt.Println(<-all, <-golang) //发布者对象关闭后读取的都是nil
}