-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
121 lines (102 loc) · 2.59 KB
/
Copy pathexample_test.go
File metadata and controls
121 lines (102 loc) · 2.59 KB
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
114
115
116
117
118
119
120
121
package sse_test
import (
"bytes"
"context"
"fmt"
"net/http/httptest"
"strings"
"time"
"lowbit.dev/sse"
)
// Example demonstrates basic usage of the sse package: writing and reading an event.
func Example() {
buf := &bytes.Buffer{}
w := sse.NewWriter(buf)
w.Write(sse.Event{
ID: "42",
Name: "message",
Data: "hello world",
Retry: 2 * time.Second,
Extensions: map[string]string{"foo": "bar"},
})
r := sse.NewReader(buf)
evt, err := r.Read()
if err != nil {
panic(err)
}
fmt.Println(evt.ID, evt.Name, evt.Data, evt.Retry, evt.Extensions["foo"])
// Output: 42 message hello world 2s bar
}
// ExampleEvent demonstrates the Event struct.
func ExampleEvent() {
e := sse.Event{
ID: "abc",
Name: "notice",
Data: "payload",
Retry: 500 * time.Millisecond,
Extensions: map[string]string{"x": "1"},
}
fmt.Println(e.ID, e.Name, e.Data, e.Retry, e.Extensions["x"])
// Output: abc notice payload 500ms 1
}
// ExampleReader demonstrates reading an SSE event from a stream.
func ExampleReader() {
data := "id: 7\nevent: ping\ndata: pong\nretry: 1000\nfoo: bar\n\n"
r := sse.NewReader(strings.NewReader(data))
evt, err := r.Read()
if err != nil {
panic(err)
}
fmt.Println(evt.ID, evt.Name, evt.Data, evt.Retry, evt.Extensions["foo"])
// Output: 7 ping pong 1s bar
}
// ExampleWriter demonstrates writing an SSE event to a stream.
func ExampleWriter() {
buf := &bytes.Buffer{}
w := sse.NewWriter(buf)
w.Write(sse.Event{
ID: "99",
Name: "update",
Data: "done",
Retry: 3 * time.Second,
Extensions: map[string]string{"extra": "val"},
})
fmt.Print(buf.String())
// Output:
// id: 99
// event: update
// retry: 3000
// extra: val
// data: done
//
}
// ExampleEmitter demonstrates using Emitter to send events and heartbeats.
func ExampleEmitter() {
rw := httptest.NewRecorder()
emitter, err := sse.NewEmitter(rw)
if err != nil {
panic(err)
}
emitter.Emit(sse.Event{ID: "1", Name: "tick", Data: "tock"})
emitter.WriteHeartbeat()
fmt.Print(rw.Body.String())
// Output:
// id: 1
// event: tick
// data: tock
//
// :
//
}
// ExampleEmitter_ServeHeartbeats demonstrates ServeHeartbeats with context cancellation.
func ExampleEmitter_ServeHeartbeats() {
rw := httptest.NewRecorder()
emitter, _ := sse.NewEmitter(rw)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
go emitter.ServeHeartbeats(ctx, 5*time.Millisecond)
time.Sleep(15 * time.Millisecond)
out := rw.Body.String()
fmt.Print(strings.Count(out, ":\n\n")) // count heartbeats
// Output: 2
}