func randMsg() Msg { topic := rand.RandStr(100, "alphanum") bodyStr := rand.RandStr(1000, "alphanum") body := []byte(bodyStr) msg := NewMsg(topic, body) return msg }
func TestMsgCreate(t *testing.T) { topic := rand.RandStr(100, "alphanum") bodyStr := rand.RandStr(1000, "alphanum") body := []byte(bodyStr) msg := NewMsg(topic, body) if msg.Topic() != topic { t.Errorf("expected %s, got %s", topic, msg.Topic()) } if !bytes.Equal(body, msg.Body()) { t.Errorf("expected %s, got %s", body, msg.Body()) } }
func run(pipe *os.File, topic string, threads int, count int) { n, err := pipe.Write([]byte(rand.RandStr(RANDSTRLEN, "alphanum"))) fmt.Println(n, err) //var wg sync.WaitGroup //for i := 0; i < threads; i++ { // wg.Add(1) // go func(thread int) { // defer wg.Done() // for j := 0; j < count; j++ { // _, err := pipe.WriteString(rand.RandStr(RANDSTRLEN, "alphanum")+ "\n") // if err != nil { // log.Printf("%d write error: %s\n", thread, err) // return // } // } // }(i) //} //for i := 0; i < COUNT; i++ { // wg.Add(1) // go func() { // defer wg.Done() // for j := 0; j < COUNT; j++ { // _, err := http.Get("http://localhost:8080/events/topic2") // if err != nil { // log.Printf("GET error: %s\n", err) // } // } // }() //} //wg.Wait() }
func TestPubSub(t *testing.T) { nCPU := runtime.NumCPU() log.Println("nCPU: ", nCPU) //runtime.GOMAXPROCS(nCPU) hub, _ := NewHub() done := make(chan struct{}) log.Print("making topics...") topics := make([]string, 0) for i := 0; i < TOPICS; i++ { topics = append(topics, fmt.Sprintf("TOPIC.%d", i)) } log.Println("...making topics done") log.Print("making subscriptions...") deliveries := make([]<-chan Msg, 0) for _, topic := range topics { for i := 0; i < SUBSCRIBERS_PER_TOPIC; i++ { sub, err := hub.Subscribe(topic) if err != nil { t.Errorf("hub.Subscribe returned err: %s\n", err) } else { deliveries = append(deliveries, sub.Delivery()) } } } log.Println("...making subscriptions done") sinkC := sink(done, merge(done, deliveries...)) log.Print("publishing topics...") for _, topic := range topics { for i := 0; i < MSG_PER_TOPIC; i++ { bodyStr := rand.RandStr(1000, "alphanum") body := []byte(bodyStr) msg := NewMsg(topic, body) hub.Publish(msg) } } log.Print("...publishing topics done") count := <-sinkC log.Print("halting...") close(done) hub.Halt() log.Print("...halted") log.Printf("stats: %s, messages back: %d\n", hub.Stats(), count) }
func TestTopic(t *testing.T) { var wg sync.WaitGroup for i := 0; i < COUNT; i++ { wg.Add(1) go func() { defer wg.Done() for j := 0; j < COUNT; j++ { _, err := http.Post("http://localhost:8080/events/topic2", "text/plain", strings.NewReader(rand.RandStr(RANDSTRLEN, "alphanum"))) if err != nil { log.Printf("POST error: %s\n", err) } } }() } for i := 0; i < COUNT; i++ { wg.Add(1) go func() { defer wg.Done() for j := 0; j < COUNT; j++ { _, err := http.Get("http://localhost:8080/events/topic2") if err != nil { log.Printf("GET error: %s\n", err) } } }() } wg.Wait() }