-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
client_test.go
204 lines (164 loc) · 4.7 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package girc
import (
"strings"
"testing"
"time"
)
func TestDisableTracking(t *testing.T) {
client := New(Config{
Server: "dummy.int",
Port: 6667,
Nick: "test",
User: "test",
Name: "Testing123",
})
if len(client.Handlers.internal) < 1 {
t.Fatal("Client.Handlers empty, though just initialized")
}
client.DisableTracking()
if _, ok := client.Handlers.internal[CAP]; ok {
t.Fatal("Client.Handlers contains capability tracking handlers, though disabled")
}
client.state.Lock()
defer client.state.Unlock()
if client.state.channels != nil {
t.Fatal("Client.DisableTracking() called but channel state still exists")
}
}
func TestConfigValid(t *testing.T) {
conf := Config{
Server: "irc.example.com", Port: 6667,
Nick: "test", User: "test", Name: "Realname",
}
var err error
if err = conf.isValid(); err != nil {
t.Fatalf("valid config failed Config.isValid() with: %s", err)
}
conf.Server = ""
if err = conf.isValid(); err == nil {
t.Fatalf("invalid server passed validation check: %s", err)
}
conf.Server = "irc.example.com"
conf.Port = 100000
if err = conf.isValid(); err == nil {
t.Fatalf("invalid port passed validation check: %s", err)
}
conf.Port = 0 // Assumes "default".
if err = conf.isValid(); err != nil {
t.Fatalf("valid default failed validation check: %s", err)
}
if conf.Port != 6667 {
t.Fatal("irc port was not defaulted to 6667")
}
conf.Nick = "invalid nick"
if err = conf.isValid(); err == nil {
t.Fatalf("invalid nick passed validation check: %s", err)
}
conf.User = "test"
conf.User = "invalid user"
if err = conf.isValid(); err == nil {
t.Fatalf("invalid user passed validation check: %s", err)
}
conf.User = "test"
}
func TestClientLifetime(t *testing.T) {
client := New(Config{
Server: "dummy.int",
Port: 6667,
Nick: "test",
User: "test",
Name: "Testing123",
})
tm := client.Lifetime()
if tm < 0 || tm > 2*time.Second {
t.Fatalf("Client.Lifetime() = %q, out of bounds", tm)
}
}
func TestClientUptime(t *testing.T) {
c, conn, server := genMockConn()
defer conn.Close()
defer server.Close()
go mockReadBuffer(conn)
done := make(chan struct{}, 1)
c.Handlers.Add(INITIALIZED, func(c *Client, e Event) { close(done) })
go c.MockConnect(server)
defer c.Close()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Client.Uptime() timed out")
}
uptime, err := c.Uptime()
if err != nil {
t.Fatalf("Client.Uptime() = %s, wanted time", err)
}
since := time.Since(*uptime)
connsince, err := c.ConnSince()
if err != nil {
t.Fatalf("Client.ConnSince() = %s, wanted time", err)
}
if since < 0 || since > 4*time.Second || *connsince < 0 || *connsince > 4*time.Second {
t.Fatalf("Client.Uptime() = %q (%q, connsince: %q), out of bounds", uptime, since, connsince)
}
// Verify the time we got from Client.Uptime() and Client.ConnSince() are
// within reach of eachother.
if *connsince-since > 2*time.Second {
t.Fatalf("Client.Uptime() (diff) = %q, Client.ConnSince() = %q, differ too much", since, connsince)
}
if !c.IsConnected() {
t.Fatal("Client.IsConnected() = false, though mock should be true")
}
}
func TestClientGet(t *testing.T) {
c, conn, server := genMockConn()
defer conn.Close()
defer server.Close()
go mockReadBuffer(conn)
done := make(chan struct{}, 1)
c.Handlers.Add(INITIALIZED, func(c *Client, e Event) { close(done) })
go c.MockConnect(server)
defer c.Close()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("timed out during connect")
}
if nick := c.GetNick(); nick != c.Config.Nick {
t.Fatalf("Client.GetNick() = %q though should be %q", nick, c.Config.Nick)
}
if user := c.GetIdent(); user != c.Config.User {
t.Fatalf("Client.GetIdent() = %q though should be %q", user, c.Config.User)
}
if !strings.Contains(c.String(), "connected:true") {
t.Fatalf("Client.String() == %q, doesn't contain 'connected:true'", c.String())
}
}
func TestClientClose(t *testing.T) {
c, conn, server := genMockConn()
defer server.Close()
defer conn.Close()
go mockReadBuffer(conn)
errchan := make(chan error, 1)
done := make(chan struct{}, 1)
c.Handlers.AddBg(CLOSED, func(c *Client, e Event) {
close(done)
})
c.Handlers.AddBg(INITIALIZED, func(c *Client, e Event) {
c.Close()
})
go func() { errchan <- c.MockConnect(server) }()
defer c.Close()
select {
case err := <-errchan:
if err == nil {
break
}
t.Fatalf("connect returned with error when close was invoked: %s", err)
case <-time.After(35 * time.Second):
t.Fatal("Client.Close() timed out")
case <-done:
}
}