forked from alexandrevicenzi/go-sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_test.go
63 lines (46 loc) · 818 Bytes
/
channel_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
package sse
import (
"fmt"
"sync"
"testing"
)
func unused(i interface{}) {}
func readMsg(c *Client) {
for msg := range c.send {
unused(msg)
}
}
func TestSendMessage(t *testing.T) {
ch := newChannel("channel")
defer ch.Close()
wg := sync.WaitGroup{}
wg.Add(1)
lastId := make(chan string, 1)
msgCount := make(chan int, 1)
c := newClient("", "client")
go func() {
i := 0
id := ""
for msg := range c.send {
i++
id = msg.id
}
lastId <- id
msgCount <- i
}()
ch.addClient(c)
go func() {
for i := 0; i < 100; i++ {
ch.SendMessage(NewMessage(fmt.Sprintf("id_%d", i+1), "msg", "channel"))
}
wg.Done()
}()
wg.Wait()
ch.removeClient(c)
if 100 != <-msgCount {
t.Fatal("Wrong message count.")
}
if ch.LastEventID() != <-lastId {
t.Fatal("Wrong Last ID.")
}
}