func TestNats4(t *testing.T) { //t.SkipNow() // Connection nc, _ := nats.Connect(nats.DefaultURL) defer nc.Close() // Replies nc.Subscribe("help", func(m *nats.Msg) { nc.Publish(m.Reply, []byte("I can help!")) }) wg := &sync.WaitGroup{} wg.Add(1) // Requests go func() { count := 0 for { msg, err := nc.Request("help", []byte("help me"), 10*time.Millisecond) if err == nil { lg.Debugf("message is %s", msg.Data) } count++ if count == 100 { wg.Done() break } } }() //wait wg.Wait() }
func TestNats3(t *testing.T) { t.SkipNow() var msg *nats.Msg //msg := &nats.Msg{} // Connection nc, _ := nats.Connect(nats.DefaultURL) defer nc.Close() // Channel Subscriber ch := make(chan *nats.Msg, 64) sub, err := nc.ChanSubscribe(subjectName3, ch) if err != nil { t.Errorf("ChanSubscribe() error: %s", err) } wg := &sync.WaitGroup{} wg.Add(1) // Publisher go func() { count := 0 for { nc.Publish(subjectName3, []byte("Hello World")) count++ if count == 100 { lg.Debug("finished publish") break } } }() // Subscriber go func() { count := 0 for { //invalid operation: msg <- ch (send to non-chan type *"github.com/nats-io/nats".Msg) msg = <-ch lg.Debugf("message is %s", msg.Data) count++ if count == 100 { wg.Done() break } } }() //wait wg.Wait() // Unsubscribe sub.Unsubscribe() }
func TestNats2(t *testing.T) { t.SkipNow() // Connection nc, _ := nats.Connect(nats.DefaultURL) defer nc.Close() // Sync Subscriber sub, err := nc.SubscribeSync(subjectName2) if err != nil { t.Errorf("SubscribeSync() error: %s", err) } wg := &sync.WaitGroup{} wg.Add(1) // Publisher go func() { count := 0 for { nc.Publish(subjectName2, []byte("Hello World")) count++ if count == 100 { break } } }() // Subscriber go func() { count := 0 for { m, err := sub.NextMsg(time.Second * 1) if err == nil { lg.Debugf("message is %s", m.Data) count++ if count == 100 { wg.Done() break } } } }() //wait wg.Wait() }