Skip to content

Commit a6b1561

Browse files
committed
Redo and Clean up some tests
1 parent 6c780b5 commit a6b1561

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed
File renamed without changes.

sasl_test.go renamed to irc_sasl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestConnectionSASL(t *testing.T) {
1414
SASLPassword := os.Getenv("SASLPassword")
1515

1616
if SASLLogin == "" {
17-
t.SkipNow()
17+
t.Skip("Define SASLLogin and SASLPasword environment varables to test SASL")
1818
}
1919
irccon := IRC("go-eventirc", "go-eventirc")
2020
irccon.VerboseCallbackHandler = true

irc_test.go

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ package irc
22

33
import (
44
"crypto/tls"
5+
"math/rand"
56
"testing"
67
"time"
78
)
89

10+
const server = "irc.freenode.net:6667"
11+
const serverssl = "irc.freenode.net:7000"
12+
const channel = "#go-eventirc-test"
13+
const dict = "abcdefghijklmnopqrstuvwxyz"
14+
915
func TestConnectionEmtpyServer(t *testing.T) {
1016
irccon := IRC("go-eventirc", "go-eventirc")
1117
err := irccon.Connect("")
@@ -178,59 +184,68 @@ func TestIRCemptyUser(t *testing.T) {
178184
}
179185
}
180186
func TestConnection(t *testing.T) {
181-
irccon1 := IRC("go-eventirc1", "go-eventirc1")
187+
rand.Seed(time.Now().UnixNano())
188+
ircnick1 := randStr(8)
189+
ircnick2 := randStr(8)
190+
irccon1 := IRC(ircnick1, "IRCTest1")
182191
irccon1.VerboseCallbackHandler = true
183192
irccon1.Debug = true
184-
irccon2 := IRC("go-eventirc2", "go-eventirc2")
193+
irccon2 := IRC(ircnick2, "IRCTest2")
185194
irccon2.VerboseCallbackHandler = true
186195
irccon2.Debug = true
187196

188-
irccon1.AddCallback("001", func(e *Event) { irccon1.Join("#go-eventirc") })
189-
irccon2.AddCallback("001", func(e *Event) { irccon2.Join("#go-eventirc") })
190-
con2ok := false
197+
teststr := randStr(20)
198+
testmsgok := false
199+
200+
irccon1.AddCallback("001", func(e *Event) { irccon1.Join(channel) })
201+
irccon2.AddCallback("001", func(e *Event) { irccon2.Join(channel) })
191202
irccon1.AddCallback("366", func(e *Event) {
192203
go func(e *Event) {
193-
t := time.NewTicker(1 * time.Second)
204+
tick := time.NewTicker(1 * time.Second)
194205
i := 10
195206
for {
196-
<-t.C
197-
irccon1.Privmsgf("#go-eventirc", "Test Message%d\n", i)
198-
if con2ok {
199-
i -= 1
200-
}
201-
if i == 0 {
202-
t.Stop()
207+
<-tick.C
208+
irccon1.Privmsgf(channel, "%s\n", teststr)
209+
if testmsgok {
210+
tick.Stop()
203211
irccon1.Quit()
212+
} else if i == 0 {
213+
t.Fatal("Timeout while wating for test message from the other thread.")
204214
}
215+
i -= 1
205216
}
206217
}(e)
207218
})
208219

209220
irccon2.AddCallback("366", func(e *Event) {
210-
irccon2.Privmsg("#go-eventirc", "Test Message\n")
211-
con2ok = true
212-
irccon2.Nick("go-eventnewnick")
221+
ircnick2 = randStr(8)
222+
irccon2.Nick(ircnick2)
213223
})
214224

215225
irccon2.AddCallback("PRIVMSG", func(e *Event) {
216226
t.Log(e.Message())
217-
if e.Message() == "Test Message5" {
218-
irccon2.Quit()
227+
if e.Message() == teststr {
228+
if e.Nick == ircnick1 {
229+
testmsgok = true
230+
irccon2.Quit()
231+
} else {
232+
t.Fatal("Test message came from an unexpected nickname")
233+
}
219234
}
220235
})
221236

222237
irccon2.AddCallback("NICK", func(e *Event) {
223-
if irccon2.nickcurrent == "go-eventnewnick" {
238+
if irccon2.nickcurrent == ircnick2 {
224239
t.Fatal("Nick change did not work!")
225240
}
226241
})
227242

228-
err := irccon1.Connect("irc.freenode.net:6667")
243+
err := irccon1.Connect(server)
229244
if err != nil {
230245
t.Log(err.Error())
231246
t.Fatal("Can't connect to freenode.")
232247
}
233-
err = irccon2.Connect("irc.freenode.net:6667")
248+
err = irccon2.Connect(server)
234249
if err != nil {
235250
t.Log(err.Error())
236251
t.Fatal("Can't connect to freenode.")
@@ -241,24 +256,33 @@ func TestConnection(t *testing.T) {
241256
}
242257

243258
func TestConnectionSSL(t *testing.T) {
244-
irccon := IRC("go-eventirc", "go-eventirc")
259+
ircnick1 := randStr(8)
260+
irccon := IRC(ircnick1, "IRCTestSSL")
245261
irccon.VerboseCallbackHandler = true
246262
irccon.Debug = true
247263
irccon.UseTLS = true
248264
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
249-
irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") })
265+
irccon.AddCallback("001", func(e *Event) { irccon.Join(channel) })
250266

251267
irccon.AddCallback("366", func(e *Event) {
252-
irccon.Privmsg("#go-eventirc", "Test Message\n")
253-
time.Sleep(2 * time.Second)
268+
irccon.Privmsg(channel, "Test Message from SSL\n")
254269
irccon.Quit()
255270
})
256271

257-
err := irccon.Connect("irc.freenode.net:7000")
272+
err := irccon.Connect(serverssl)
258273
if err != nil {
259274
t.Log(err.Error())
260275
t.Fatal("Can't connect to freenode.")
261276
}
262277

263278
irccon.Loop()
264279
}
280+
281+
// Helper Functions
282+
func randStr(n int) string {
283+
b := make([]byte, n)
284+
for i := range b {
285+
b[i] = dict[rand.Intn(len(dict))]
286+
}
287+
return string(b)
288+
}

0 commit comments

Comments
 (0)