forked from alexandrevicenzi/go-sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sse_test.go
145 lines (118 loc) · 3.13 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package sse
import (
"context"
"fmt"
"log"
"net/http"
"os"
"sync"
"testing"
"time"
)
var clientConnectedCallbackCalls = 0
var clientDisconnectedCallbackCalls = 0
func clientConnected(c *Client) {
clientConnectedCallbackCalls++
log.Printf("client connected: %v", c)
}
func clientDisconnected(c *Client) {
clientDisconnectedCallbackCalls++
log.Printf("client disconnected: %v", c)
}
func TestNewServerNilOptions(t *testing.T) {
srv := NewServer(nil)
defer srv.Shutdown()
if srv == nil || srv.options == nil || srv.options.Logger == nil {
t.Fail()
}
}
func TestNewServerNilLogger(t *testing.T) {
srv := NewServer(&Options{
Logger: nil,
})
defer srv.Shutdown()
if srv == nil || srv.options == nil || srv.options.Logger == nil {
t.Fail()
}
}
func TestServer(t *testing.T) {
channelCount := 2
clientCount := 5
messageCount := 0
srv := NewServer(&Options{
ClientConnectedFunc: clientConnected,
ClientDisconnectedFunc: clientDisconnected,
Logger: log.New(os.Stdout, "go-sse: ", log.Ldate|log.Ltime|log.Lshortfile),
})
defer srv.Shutdown()
// Create N channes
for n := 0; n < channelCount; n++ {
name := fmt.Sprintf("CH-%d", n+1)
srv.addChannel(name)
fmt.Printf("Channel %s registed\n", name)
}
wg := sync.WaitGroup{}
m := sync.Mutex{}
// Create N clients in all channes
for n := 0; n < clientCount; n++ {
for name, ch := range srv.channels {
wg.Add(1)
// Create new client
c := newClient("", name)
// Add client to current channel
ch.addClient(c)
id := fmt.Sprintf("C-%d", n+1)
fmt.Printf("Client %s registed to channel %s\n", id, name)
go func(id string) {
// Wait for messages in the channel
for msg := range c.send {
m.Lock()
messageCount++
m.Unlock()
fmt.Printf("Channel: %s - Client: %s - Message: %s\n", name, id, msg.data)
wg.Done()
}
}(id)
}
}
// Send hello message to all channels and all clients in it
srv.SendMessage("", SimpleMessage("hello"))
srv.close()
wg.Wait()
if messageCount != channelCount*clientCount {
t.Errorf("Expected %d messages but got %d", channelCount*clientCount, messageCount)
}
}
func TestServerAdditionalOptions(t *testing.T) {
srv := NewServer(&Options{
ClientConnectedFunc: clientConnected,
ClientDisconnectedFunc: clientDisconnected,
Logger: log.New(os.Stdout, "go-sse: ", log.Ldate|log.Ltime|log.Lshortfile),
})
defer srv.Shutdown()
http.Handle("/events/", srv)
go func() {
err := http.ListenAndServe("localhost:8081", nil)
if err != nil {
log.Printf("error running http server: %v", err)
}
}()
client := http.Client{}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost:8081/events/asd", nil)
res, err := client.Do(req)
if err != nil {
t.Fatalf("cannot get event url")
}
defer res.Body.Close()
<-ctx.Done()
time.Sleep(time.Millisecond * 300)
if clientConnectedCallbackCalls != 1 {
t.Errorf("clientConnectedCallbackCalls != 1")
}
if clientDisconnectedCallbackCalls != 1 {
t.Errorf("clientDisconnectedCallbackCalls != 1")
}
}