-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
143 lines (108 loc) · 2.94 KB
/
client_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
package carrot
import (
log "github.com/sirupsen/logrus"
"math"
"sync"
"testing"
"time"
)
func sampleClient() *Client {
sessions := NewDefaultSessionManager()
_, session, _ := sessions.NewSession()
client := &Client{
session: session,
send: make(chan []byte, sendMsgBufferSize),
start: make(chan struct{}),
openMutex: &sync.RWMutex{},
open: true,
}
return client
}
func TestClientExpired(t *testing.T) {
client := sampleClient()
waitTime := time.Now()
if client.Expired() == true {
t.Fatalf("client expired when it shouldn't be")
}
client.session.expireTime = waitTime
client.softClose()
time.Sleep(time.Second) // needed for test to pass on Windows
if client.Expired() == false {
t.Fatalf("client should be expired but it isn't")
}
}
func TestClientOpen(t *testing.T) {
client := sampleClient()
client.softOpen()
if client.Open() == false {
t.Fatal("client is closed when it should be open")
}
client.softClose()
if client.Open() == true {
t.Fatal("client is open when it should be closed")
}
}
func TestClientValid(t *testing.T) {
client := sampleClient()
if client.Valid() != true {
t.Fatal("client is invalid when it should be valid")
}
client = nil
if client.Valid() != false {
t.Fatal("client is valid when it should not be valid")
}
}
func TestClientcheckBufferRedzone(t *testing.T) {
client := &Client{
send: make(chan []byte, sendMsgBufferSize),
logger: log.WithField("module", "client_test"),
}
res := client.checkBufferRedZone()
if res == true {
t.Fatalf("buffer was signaling redzone when it shouldn't have been")
}
for i := 0; i < int(math.Floor(sendMsgBufferSize*0.95)); i++ {
client.send <- ([]byte("test message!"))
}
res = client.checkBufferRedZone()
if res != true {
t.Fatalf("buffer was not signaling redzone when it should have been ")
}
}
func TestClientcheckBufferFull(t *testing.T) {
client := &Client{
send: make(chan []byte, sendMsgBufferSize),
logger: log.WithField("module", "client_test"),
}
res := client.checkBufferFull()
if res == true {
t.Fatalf("buffer was signaling full when it shouldn't have been")
}
for i := 0; i < sendMsgBufferSize; i++ {
client.send <- ([]byte("test message!"))
}
res = client.checkBufferFull()
if res != true {
t.Fatalf("buffer was not full redzone when it should have been ")
}
}
func TestClientIsRecipient(t *testing.T) {
client := &Client{}
sm := NewDefaultSessionManager()
sessionToken, session, _ := sm.NewSession()
client.session = session
recipients := make([]string, 0)
for i := 0; i < 15; i++ {
uuid, _ := generateUUID()
recipients = append(recipients, uuid)
}
result := client.IsRecipient(recipients)
if result != false {
t.Error("client was a recipient when it should not have been")
}
recipients = append(recipients, string(sessionToken))
result = client.IsRecipient(recipients)
if result != true {
t.Error("client was not a recipient when it should have been")
}
}