-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_manager_test.go
57 lines (47 loc) · 1.13 KB
/
channel_manager_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
package roc
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestChannelManagerNew(t *testing.T) {
manager := NewChannelManager()
packer := &Packer{}
generator := &IdGenerator{}
id := generator.Generate()
channel := manager.Get(id, true)
go func(id uint32) {
packet := &Packet{id, "Hello World"}
ret := packer.Pack(packet)
channel <- ret
}(id)
ret, _ := <-channel
packet := packer.UnPack(ret[4:])
assert.Equal(t, packet.GetId(), id)
}
func TestChannelManagerFlush(t *testing.T) {
manager := NewChannelManager()
generator := &IdGenerator{}
id := generator.Generate()
channel := manager.Get(id, true)
go func(manager *ChannelManager) {
time.Sleep(time.Second)
manager.Flush()
}(manager)
ret, ok := <-channel
assert.False(t, ok)
assert.Nil(t, ret)
}
func TestChannelManagerClose(t *testing.T) {
manager := NewChannelManager()
generator := &IdGenerator{}
id := generator.Generate()
channel := manager.Get(id, true)
go func(manager *ChannelManager, id uint32) {
time.Sleep(time.Second)
manager.Close(id)
}(manager, id)
ret, ok := <-channel
assert.False(t, ok)
assert.Nil(t, ret)
}