forked from alexandrevicenzi/go-sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sse_test.go
80 lines (60 loc) · 1.32 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
package sse
import (
"fmt"
"sync"
"testing"
)
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) {
srv := NewServer(&Options{
Logger: nil,
})
defer srv.Shutdown()
for n := 0; n < 2; n++ {
name := fmt.Sprintf("CH-%d", n+1)
srv.addChannel(name)
// fmt.Printf("Channel %s registed\n", name)
}
messages := make([]*Message, 0)
wgReg := sync.WaitGroup{}
wgCh := sync.WaitGroup{}
for n := 0; n < 5; n++ {
for name, ch := range srv.channels {
wgReg.Add(1)
wgCh.Add(1)
go func(id string) {
c := newClient("", name)
ch.addClient(c)
// fmt.Printf("Client %s registed to channel %s\n", id, name)
wgReg.Done()
for msg := range c.send {
// fmt.Printf("Channel: %s - Client: %s - Message: %s\n", name, id, msg.data)
messages = append(messages, msg)
}
wgCh.Done()
}(fmt.Sprintf("C-%d", n+1))
}
}
wgReg.Wait()
srv.SendMessage("", SimpleMessage("hello"))
srv.close()
wgCh.Wait()
if len(messages) != 10 {
t.Fail()
}
}