Exemple #1
0
func TestServerSentEvents(t *testing.T) {
	ts := httptest.NewServer(NewHandler())
	defer ts.Close()

	cli, err := client.NewSSEClient(ts.URL + "/sse")
	if err != nil {
		t.Fatal(err)
	}
	defer cli.Close()

	ch := make(chan bool, 1)
	go func() {
		data := <-cli.Listener
		log.Println("TestServerSentEvents:received:", data)

		ch <- true
	}()

	// send realtime message
	res, err := post(ts.URL, pham.JSON{
		"channel": `test`,
		"data": pham.JSON{
			"type": "ping",
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	log.Println("TestServerSentEvents:sent:", res)

	<-ch
}
Exemple #2
0
func BenchmarkServerSentEvents(b *testing.B) {
	ts := httptest.NewServer(NewHandler())
	defer ts.Close()

	wg := new(sync.WaitGroup)
	for i := 0; i < b.N; i++ {
		cli, err := client.NewSSEClient(ts.URL + "/sse")
		if err != nil {
			panic(err)
		}
		defer cli.Close()

		wg.Add(1)

		go func() {
			data := <-cli.Listener
			log.Println("TestServerSentEvents:received:", data)

			wg.Done()
		}()
	}

	// send realtime message
	res, err := post(ts.URL, pham.JSON{
		"channel": `test`,
		"data": pham.JSON{
			"type": "ping",
		},
	})
	if err != nil {
		panic(err)
	}
	log.Println("TestServerSentEvents:sent:", res)

	wg.Wait()
}