github.com/zly-app/zapp@v1.3.3/component/msgbus/topic_test.go (about) 1 /* 2 ------------------------------------------------- 3 Author : zlyuancn 4 dateļ¼ 2021/3/19 5 Description : 6 ------------------------------------------------- 7 */ 8 9 package msgbus 10 11 import ( 12 "sync" 13 "testing" 14 15 "go.uber.org/zap" 16 17 "github.com/zly-app/zapp/core" 18 ) 19 20 func TestTopic(t *testing.T) { 21 var wg sync.WaitGroup 22 wg.Add(10) 23 24 topic1 := newMsgTopic() 25 defer topic1.Close() 26 27 topic2 := newMsgTopic() 28 defer topic2.Close() 29 30 subscribe1 := topic1.Subscribe(1, func(ctx core.IMsgbusContext) error { 31 ctx.Info("subscribe.topic1", zap.Any("msg", ctx.Msg())) 32 wg.Done() 33 return nil 34 }) 35 36 subscribe2 := topic2.Subscribe(1, func(ctx core.IMsgbusContext) error { 37 ctx.Info("subscribe.topic2", zap.Any("msg", ctx.Msg())) 38 wg.Done() 39 return nil 40 }) 41 42 for i := 0; i < 5; i++ { 43 topic1.Publish("topic1", i) 44 topic2.Publish("topic2", i) 45 } 46 47 topic1.Unsubscribe(subscribe1) 48 topic2.Unsubscribe(subscribe2) 49 50 wg.Wait() 51 }