//Simple nano puller func Pull(count int, finished chan int, port int) { var ( msg []byte err error ) socket, err := nano.NewPullSocket() if nil != err { log.Error(err) } defer socket.Close() sport := strconv.Itoa(port) _, err = socket.Bind("tcp://*:" + sport) if nil != err { log.Error(err) } log.Info("Connected and ready to receive data") tot := 0 for { msg, err = socket.Recv(0) //blocking if nil != err { log.Error(err) } else { // log.Trace(msg) if nil != msg { tot++ } } if tot >= count { break } } finished <- tot }
gom "github.com/onsi/gomega" nano "github.com/op/go-nanomsg" // "strings" "time" ) var _ = gi.Describe("Goreport", func() { var rep Reporter gi.BeforeEach(func() { ReporterConfig("ipc:///tmp/goreportertest.ipc", 1) rep = NewReporter() gom.Expect(rep).ShouldNot(gom.Equal(gom.BeNil())) }) gi.It("End to End integration test with stats", func() { pull, err := nano.NewPullSocket() gom.Expect(err).Should(gom.BeNil()) pull.SetRecvTimeout(6 * time.Second) pull.SetRecvBuffer(1000) pull.Bind("ipc:///tmp/goreportertest.ipc") key := "key" rep.RegisterStat(key) rep.RegisterStatWIndex(key, "index") rep.AddStat(key, 2) rep.AddStat(key, 2) rep.AddStatWIndex(key, 2, "index") rep.AddStatWIndex(key, 2, "index") msg, err := pull.Recv(0) gom.Expect(err).Should(gom.BeNil()) stats := new(protoStat.ProtoStats) stats.Unmarshal(msg)
//Injest data from queue and ship the data off to be swallowed func OpenWide(chewChan chan *messaging.Food, done chan interface{}, wg *sync.WaitGroup, port int) { var ( msg []byte err error ) defer close(chewChan) socket, err := nano.NewPullSocket() if nil != err { log.Error(err) } defer socket.Close() r := rep.NewReporter() //repeat stats with 0 if nothing is reported r.RegisterStatWIndex("lips", "timeout") r.RegisterStatWIndex("lips", "good") r.RegisterStatWIndex("lips", "bad") socket.SetRecvTimeout(1000 * time.Millisecond) sport := strconv.Itoa(port) _, err = socket.Bind("tcp://*:" + sport) if nil != err { log.Error(err) } log.Info("Connected and ready to receive data") main: for { select { case <-done: { log.Info("Got done signal") break main } default: { msg, err = socket.Recv(0) if nil != err { r.AddStatWIndex("lips", 1, "timeout") //we hit timeout } if nil != msg { food := new(messaging.Food) err = food.Unmarshal(msg) if nil != err { log.Error("Invalid message: ", err) r.AddStatWIndex("lips", 1, "bad") continue } r.AddStatWIndex("lips", 1, "good") chewChan <- food } } } } log.Info("Closing lips") log.Flush() wg.Done() }