-
Notifications
You must be signed in to change notification settings - Fork 0
/
sse_test.go
113 lines (91 loc) · 1.73 KB
/
sse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package sse_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"ella.to/sse"
)
func TestParse(t *testing.T) {
r := strings.NewReader("data: hello\n\n")
ch := sse.Parse(r)
msg := <-ch
if msg.Id != nil {
t.Errorf("Expected Id to be nil, got %s", *msg.Id)
}
if msg.Event != "" {
t.Errorf("Expected Event to be empty, got %s", msg.Event)
}
if *msg.Data != "hello" {
t.Errorf("Expected Data to be hello, got %s", *msg.Data)
}
}
func TestParseLarge(t *testing.T) {
file, err := os.Open("./testdata/test01.txt")
if err != nil {
t.Fatal(err)
}
defer file.Close()
ch := sse.Parse(file)
for msg := range ch {
fmt.Printf("%s", msg)
}
}
func TestPusherReceiver(t *testing.T) {
n := 100000
c := 10
var wg sync.WaitGroup
wg.Add(c)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pusher, err := sse.NewPusher(w)
if err != nil {
t.Error(err)
return
}
var msg sse.Message
for i := range n {
id := fmt.Sprintf("id-%d", i)
event := "event"
data := fmt.Sprintf("data-%d", i)
msg.Id = &id
msg.Event = event
msg.Data = &data
err = pusher.Push(&msg)
if err != nil {
break
}
}
}))
defer server.Close()
client := http.Client{}
for range c {
go func() {
defer wg.Done()
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Error(err)
return
}
resp, err := client.Do(req)
if err != nil {
t.Error(err)
return
}
defer resp.Body.Close()
r := sse.NewReceiver(resp.Body)
for {
msg, err := r.Receive(context.Background())
if err != nil {
break
}
_ = msg
// fmt.Printf("%s", msg)
}
}()
}
wg.Wait()
}