Redo and Clean up some tests

This commit is contained in:
Thomas Jager 2016-07-25 13:27:37 +02:00
parent 6c780b5b70
commit a6b1561a01
3 changed files with 51 additions and 27 deletions

View File

@ -14,7 +14,7 @@ func TestConnectionSASL(t *testing.T) {
SASLPassword := os.Getenv("SASLPassword") SASLPassword := os.Getenv("SASLPassword")
if SASLLogin == "" { if SASLLogin == "" {
t.SkipNow() t.Skip("Define SASLLogin and SASLPasword environment varables to test SASL")
} }
irccon := IRC("go-eventirc", "go-eventirc") irccon := IRC("go-eventirc", "go-eventirc")
irccon.VerboseCallbackHandler = true irccon.VerboseCallbackHandler = true

View File

@ -2,10 +2,16 @@ package irc
import ( import (
"crypto/tls" "crypto/tls"
"math/rand"
"testing" "testing"
"time" "time"
) )
const server = "irc.freenode.net:6667"
const serverssl = "irc.freenode.net:7000"
const channel = "#go-eventirc-test"
const dict = "abcdefghijklmnopqrstuvwxyz"
func TestConnectionEmtpyServer(t *testing.T) { func TestConnectionEmtpyServer(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc") irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("") err := irccon.Connect("")
@ -178,59 +184,68 @@ func TestIRCemptyUser(t *testing.T) {
} }
} }
func TestConnection(t *testing.T) { func TestConnection(t *testing.T) {
irccon1 := IRC("go-eventirc1", "go-eventirc1") rand.Seed(time.Now().UnixNano())
ircnick1 := randStr(8)
ircnick2 := randStr(8)
irccon1 := IRC(ircnick1, "IRCTest1")
irccon1.VerboseCallbackHandler = true irccon1.VerboseCallbackHandler = true
irccon1.Debug = true irccon1.Debug = true
irccon2 := IRC("go-eventirc2", "go-eventirc2") irccon2 := IRC(ircnick2, "IRCTest2")
irccon2.VerboseCallbackHandler = true irccon2.VerboseCallbackHandler = true
irccon2.Debug = true irccon2.Debug = true
irccon1.AddCallback("001", func(e *Event) { irccon1.Join("#go-eventirc") }) teststr := randStr(20)
irccon2.AddCallback("001", func(e *Event) { irccon2.Join("#go-eventirc") }) testmsgok := false
con2ok := false
irccon1.AddCallback("001", func(e *Event) { irccon1.Join(channel) })
irccon2.AddCallback("001", func(e *Event) { irccon2.Join(channel) })
irccon1.AddCallback("366", func(e *Event) { irccon1.AddCallback("366", func(e *Event) {
go func(e *Event) { go func(e *Event) {
t := time.NewTicker(1 * time.Second) tick := time.NewTicker(1 * time.Second)
i := 10 i := 10
for { for {
<-t.C <-tick.C
irccon1.Privmsgf("#go-eventirc", "Test Message%d\n", i) irccon1.Privmsgf(channel, "%s\n", teststr)
if con2ok { if testmsgok {
i -= 1 tick.Stop()
}
if i == 0 {
t.Stop()
irccon1.Quit() irccon1.Quit()
} else if i == 0 {
t.Fatal("Timeout while wating for test message from the other thread.")
} }
i -= 1
} }
}(e) }(e)
}) })
irccon2.AddCallback("366", func(e *Event) { irccon2.AddCallback("366", func(e *Event) {
irccon2.Privmsg("#go-eventirc", "Test Message\n") ircnick2 = randStr(8)
con2ok = true irccon2.Nick(ircnick2)
irccon2.Nick("go-eventnewnick")
}) })
irccon2.AddCallback("PRIVMSG", func(e *Event) { irccon2.AddCallback("PRIVMSG", func(e *Event) {
t.Log(e.Message()) t.Log(e.Message())
if e.Message() == "Test Message5" { if e.Message() == teststr {
irccon2.Quit() if e.Nick == ircnick1 {
testmsgok = true
irccon2.Quit()
} else {
t.Fatal("Test message came from an unexpected nickname")
}
} }
}) })
irccon2.AddCallback("NICK", func(e *Event) { irccon2.AddCallback("NICK", func(e *Event) {
if irccon2.nickcurrent == "go-eventnewnick" { if irccon2.nickcurrent == ircnick2 {
t.Fatal("Nick change did not work!") t.Fatal("Nick change did not work!")
} }
}) })
err := irccon1.Connect("irc.freenode.net:6667") err := irccon1.Connect(server)
if err != nil { if err != nil {
t.Log(err.Error()) t.Log(err.Error())
t.Fatal("Can't connect to freenode.") t.Fatal("Can't connect to freenode.")
} }
err = irccon2.Connect("irc.freenode.net:6667") err = irccon2.Connect(server)
if err != nil { if err != nil {
t.Log(err.Error()) t.Log(err.Error())
t.Fatal("Can't connect to freenode.") t.Fatal("Can't connect to freenode.")
@ -241,20 +256,20 @@ func TestConnection(t *testing.T) {
} }
func TestConnectionSSL(t *testing.T) { func TestConnectionSSL(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc") ircnick1 := randStr(8)
irccon := IRC(ircnick1, "IRCTestSSL")
irccon.VerboseCallbackHandler = true irccon.VerboseCallbackHandler = true
irccon.Debug = true irccon.Debug = true
irccon.UseTLS = true irccon.UseTLS = true
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true} irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) irccon.AddCallback("001", func(e *Event) { irccon.Join(channel) })
irccon.AddCallback("366", func(e *Event) { irccon.AddCallback("366", func(e *Event) {
irccon.Privmsg("#go-eventirc", "Test Message\n") irccon.Privmsg(channel, "Test Message from SSL\n")
time.Sleep(2 * time.Second)
irccon.Quit() irccon.Quit()
}) })
err := irccon.Connect("irc.freenode.net:7000") err := irccon.Connect(serverssl)
if err != nil { if err != nil {
t.Log(err.Error()) t.Log(err.Error())
t.Fatal("Can't connect to freenode.") t.Fatal("Can't connect to freenode.")
@ -262,3 +277,12 @@ func TestConnectionSSL(t *testing.T) {
irccon.Loop() irccon.Loop()
} }
// Helper Functions
func randStr(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = dict[rand.Intn(len(dict))]
}
return string(b)
}