-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
sse_test.go
89 lines (70 loc) · 1.69 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
package sse
import (
"fmt"
"log"
"os"
"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) {
channelCount := 2
clientCount := 5
messageCount := 0
srv := NewServer(&Options{
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)
}
}